Jump to content

"fixed" arithmetic


Recommended Posts

The actual data for a fixed object is an i32, right?  So why not have "typedef i32 fixed" and instead of using a CFixed class, just have utility functions for operating on i32 as fixed-precision numbers.

Edited by causative
Link to comment
Share on other sites

Because operations don't work. F.e. take multiplication. Take (as a small example, I won't write out 32 bits) it's stored in 8 bits, of which 1 bit is for the sign 4 are for the integer value, and 3 for the decimal places. Then 1 is represented as 1000 in binary, so 1*1 integer multiplication is 1000000 = 16. Doesn't really work out eh?

The functions made available in CFixed make sure the numbers get shifted to one side or the other so these operations do make sense (and take into account unexpected overflows). It's just easier when you can do a*b with fixed numbers, and let it give the correct result. Instead of calling some method mult() defined in an obscure place.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...