A Crash Course in Floating Point Numbers (and why XNA is stupid)

March 29, 2009 at 2:50 pm | In Blogroll | Leave a Comment

(Note: If the following makes no sense, then I’m not very good at explaining the concept and hence I will most likely fail at least part of my exams this year, anyway…)

Floating point number are how a computer can represent a decimal number in memory.   As most of you should know, a computer works with everything in 1s and 0s, which are bits.  When a floating point number is created, the bits used to create the number are split into two groups, an exponent and a mantissa.  The mantissa creates a little binary decimal.  It’s very fiddley because of the way bits but 0.1 = 1/2, 0.01 = 1/4, 0.11 = 3/4.  If that doesn’t make sense to you, don’t worry, I can’t be asked to go into a huge amount of detail here. The mantissa is then multiplyed by 2 to the power of the exponent, giving you a wide range of numbers to work with.

Let’s focus on the mantissa, the more bits you have to work with, the more precise a number will be.  Think about it, let’s try to represent 0.1 (normal 0.1 as in a tenth) in binary

with 4 bits, the closest you can get is 0.0010 (i.e. an eigth, 0.125)

however, if you use twice that, the closest is 0.00011001 (25/256, 0.09765625)

As you can see, using twice as many bits allows you to be far more accurate.

There are two main types of floating bit numbers used in computers, single and double precision.  Single precision uses 32 bits (split between the mantissa and the exponenet) and double precision uses 64 bits.  As numbers take up such a small amount of space and memory isn’t an issue in this day and age, we have been advised to use double precision numbers at every opportunity
Why am I bringing this up?  Well, XNA Game Creator doesn’t seem to understand some simple concepts and constantly require you to have to add extra chunks of code to make it convert numbers from single to double precision floating point numbers (or vica versa) because it will moan if you use the wrong one.  Not only that, but it asks you to use each one for specific things on different occassions.

For example, a vector requires two values of double precision

For drawing something at a particular angle, you need it to be single precision, however if you want to apply any trigonometry to it, it has to double precision.  Also, any inverse trig rules will output a double precision number so to use it in drawing an object, you need to convert.

In fact pretty much anything involving a calculation involves double precision and every drawing uses single.  Now, I know that’s the sensible way around to have it, but I don’t see what the problem would be is everything was designated to be a double precision number?

Add that to the fact you need to keep some things as integers and…GAH!

Today is a new day, just like yesterday was…

March 4, 2009 at 4:14 pm | In Blogroll | Leave a Comment

I’m in a generally pretty good mood right now, I mean I most certainly haven’t fixed any of the “issues” I seem to have constantly in my life, but at I seem to have moved into another phase of it not bothering me quite so much.

There’s just a few small things that have cheered me up a bit too, despite having a ridiculously constant stream of work right now (and as a result not even starting to write my smoker audition). I seem to be doing a lot more random small events, like squash, having friends from other colleges round for formal and going to see some more comedy tonight (Derek). For once, I’ve actually managed to be organised for once and am setting up a halo doubles tournament. The great thing about this is that it’s probably going to be in the auditorium now, so yay! Big screen! Simple things please simple minds…

Also, I came back from town thinking I’d like something sweet to eat, but didn’t think I had anything, then found the fruit sherbets Nick left on my bed from last night :D

Finally, two random things that made me laugh, firstly from the bottom of the stand-up section of the bulletin:
“p.s if you are lacking in friendships its worth noting that funny people tend to have them-maybe this is your chance.”

and this, which I could easily see happening to me

http://22.media.tumblr.com/fSymsOGXOag40ilxEvObgKEA_500.gif

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.