#41141 - rize - Mon Apr 25, 2005 10:28 pm
I'm wondering about the fixed point math and the math co processor. I will attempt to answer my own questions and if anyone knows that my answers are correct, please say so.
I see the typedef:
Should I be using f32 instead of double and/or float at all times? I'm thinking yes unless I need more precision than 12 bits on the right side of the decimal.
I don't have a lot of C experience. When I do division using the symbol / is that always software division or is it designed to invoke the hardware division typedefs in math.h? I'm thinking that / always uses software division.
What's the best way to make an f32 constant? Should I just make a float constant and convert to f32? using floatof32(n)? I'm thinking yes.
(f32)1234.56 doesn't make a useful f32 constant does it? I think it just makes a float using the value 1234.56 and changes the type without multiplying by 2^12 (giving me some garbage value).
I see that intof32 simply shifts left 12 spots. Does this work for negative numbers (i.e. does the format 1.19.12 use two's compliment or is it assumed that no one needs negative f32 values)? I'm assuming that it does work because the fixed point type is really just a different way of interpretted an integer (that is shifting left by 12 before doing math and shifting back before using the integer value so that the 12 low bits are used to retain precision on the right side of the decimal.
What happens if I use / on two f32's? I'm thinking it will do a software integer divide (or else give me a type error) which will produce accurate results because the format works as I mentioned above. If it gives a type error, I assume that typecasting as int and then back to f32 will work.
I see the typedef:
Code: |
typedef int f32; // 1.19.12 fixed point for matricies
#define intof32(n) ((n) << 12) #define f32toint(n) ((n) >> 12) #define floatof32(n) ((f32)((n) * (1 << 12))) #define f32tofloat(n) (((float)(n)) / (float)(1<<12)) |
Should I be using f32 instead of double and/or float at all times? I'm thinking yes unless I need more precision than 12 bits on the right side of the decimal.
I don't have a lot of C experience. When I do division using the symbol / is that always software division or is it designed to invoke the hardware division typedefs in math.h? I'm thinking that / always uses software division.
What's the best way to make an f32 constant? Should I just make a float constant and convert to f32? using floatof32(n)? I'm thinking yes.
(f32)1234.56 doesn't make a useful f32 constant does it? I think it just makes a float using the value 1234.56 and changes the type without multiplying by 2^12 (giving me some garbage value).
I see that intof32 simply shifts left 12 spots. Does this work for negative numbers (i.e. does the format 1.19.12 use two's compliment or is it assumed that no one needs negative f32 values)? I'm assuming that it does work because the fixed point type is really just a different way of interpretted an integer (that is shifting left by 12 before doing math and shifting back before using the integer value so that the 12 low bits are used to retain precision on the right side of the decimal.
What happens if I use / on two f32's? I'm thinking it will do a software integer divide (or else give me a type error) which will produce accurate results because the format works as I mentioned above. If it gives a type error, I assume that typecasting as int and then back to f32 will work.