simple fixed point math question

Started by
6 comments, last by doynax 19 years, 1 month ago
Hi, Simple newbie question... Is it possible to represent 0.2 (a float or double) as a fixed point number? such as 2<<16? Or are fixed point numbers more like an int? Sorry for the beginner question, but math has never really been a strong point of mine... Thanks, Miranda
Advertisement
>Is it possible to represent 0.2 (a float or double) as a fixed point number? such as 2<<16? Or are fixed point numbers more like an int?

Yes, and no. You cannot represent 0.2 eactly in fixed-point. For example, as a 16.16 fixed-point number 0.2 is represented by the integer part of 0.2 * 2^16, that is int(13107.2) = 13107. (The fact that we rounded off the fractions is the reason we cannot represent it exactly.)

In general, a floating point number F is represented by the fixed-point number N in X.Y fixed-point format as N = int(F * (2 ^ Y)), where int() is the (correctly rounded) integer part. The X.Y notation means that you're using X bits to represent the integer part of the fixed-point number and Y bits to represent the fractional part. The sum of X and Y should equal the number of bits you're using for your fixed-point number (usually 32 or 64).
Also worth to note that 0.2 can also not be represented exactly as a floating point since it's (8/5)*2^-3, 8/5 being the mantissa which is also a binary 0.23 fixed point number. 5 and 2 are relatively prime (and purely prime BTW), the binary representation serie of 1/5 is thus infinite.

Still if you do x=0.2 and print it out, by some magic 0.2 will appear on your console or debugger screen as ... 0.2. But that's fake. That's because the value is converted to the decimal system, and then rounded to the nearest depending on the number of digits after the point that you request for printing. If you ask for enough decimal digits, you'll surely see that 0.2f is not 0.2 ! :p
"Coding math tricks in asm is more fun than Java"
Thanks guys, that helps explain it alot!
Quote:Original post by Christer Ericson
You cannot represent 0.2 eactly in fixed-point.
A fixed-point number doesn't have to use a power-of-two exponent. Use 10 as the scaling factor instead and 0.2 can be represented as 2.
In fact decimal representations are quite common (SQL's NUMERIC type is one example).
Quote:Original post by doynax
A fixed-point number doesn't have to use a power-of-two exponent. Use 10 as the scaling factor instead and 0.2 can be represented as 2.
In fact decimal representations are quite common (SQL's NUMERIC type is one example).


Of course. You could also encode fixed-point numbers in BCD. Those approaches are reserved for banking applications and the like, however. For games, you'd be a complete moron if you used a power of 10 exponent or BCD-encoded fixed-point numbers. In other words, non-power of two exponent fixed-point numbers isn't what the OP is interested in.
Of course. You could also encode fixed-point numbers in BCD. Those approaches are reserved for banking applications and the like, however. For games, you'd be a complete moron if you used a power of 10 exponent or BCD-encoded fixed-point numbers. In other words, non-power of two exponent fixed-point numbers isn't what the OP is interested in.


The reason behind this if that with power of 2 you can use bitshifts instead of mul and div, which is much faster. And this is what we need in games when we have a lot of computation to do at a decent frame rate.

Etienne
cyberg- cyberg_coder@hotmail.com- http://members.xoom.com/cybergsoft
Quote:Original post by Christer Ericson
For games, you'd be a complete moron if you used a power of 10 exponent or BCD-encoded fixed-point numbers. In other words, non-power of two exponent fixed-point numbers isn't what the OP is interested in.
I've used both decimal fixed-point numbers and BCD in games.
While I wouldn't consider them for storing coordinates they're often the perfect choice for various numeric display when you're starved for cycles on low-end hardware.
I don't know what the OP wanted but considering that it involved representing decimals precisely I think it could quite possibly have had something to do with a score display. And even if it isn't a performance issue it can still be a very nice thing to have precision guarantees.

This topic is closed to new replies.

Advertisement