#22148 - InflamedSpirit - Sun Jun 13, 2004 9:35 pm
I've been programming with gba for a couple months now but I've never really understood the fixed point variables. The definition of them is, as far as i can tell, the same as a long (s32).
Could you just use a long for fixed point variables or is the FIXED definition declaration stuff really needed?
Also, does anyone knows a good site with a tutorial about fixed points?
_________________
My confection, through natural selection, has reached a complexion of utter perfection. (well, not really... but...)
#22150 - alek - Sun Jun 13, 2004 9:49 pm
Yes, you can use long for fixed point math. What you do is you specify what bits represent the integer part and what bits represents the decimals. If you use long you might want the first 16 bits to represent decimals and the following 16 bits to represent the integer part.
Here's a link to a page that explains it a bit and gives a few defines for you to work with http://members.aol.com/form1/fixed.htm
#22151 - poslundc - Sun Jun 13, 2004 10:17 pm
There is no intrinsic difference to the compiler between fixed-point variables and the normal integer types (int, short, char, etc.). When and if you use typedef to create a "FIXED" data type, it is purely for the programmer's reference, so they can see and remember when looking at the variable declaration that they are supposed to interpret it as a fixed-point variable.
Fixed-point mathematics rely entirely on the programmer for the implementation, versus floats, which rely on the compiler.
From a practical perspective, most fixed-point variables are declared either as u32 or s32, since the other primitive integer types don't usually provide enough precision as the task demands for both the integer and fractional portions. I've occasionally used u16/s16 for 8.8 fixed-point numbers, though.
Dan.
#22163 - InflamedSpirit - Mon Jun 14, 2004 1:30 am
Thx for the help but now I'm getting a new error. All over, whenever I use the << or >> i seem to get the following error:
invalid operands to binary >>
When ever I comment the line with the shift out it compiles fine (but, of corse, I'm missing those lines).
_________________
My confection, through natural selection, has reached a complexion of utter perfection. (well, not really... but...)
#22164 - poslundc - Mon Jun 14, 2004 1:33 am
The error implies that you aren't using integer variables as your operands. If you still can't figure it out, try posting the code.
Dan.
#22173 - johnny_north - Mon Jun 14, 2004 3:24 am
You'll get the error you refer to if you are trying to use the shift operators with a float.
Instead of this:
Code: |
float x = 1.0;
Fixed24_8 y = x<<8; |
Do this:
Code: |
float x = 1.0
Fixed24_8 = x*256; |
#23178 - LeMich - Tue Jul 06, 2004 6:24 pm
Hi,
I am a bit confused with fixed point.
I don't see why addition is the same as for integer on an example.
ex :
11:100 3:4
+ 100:111 4:7
=111:1011 7:11
But in fact 3:4+4:7 = 8:1 and not 7:11
Can someone help me ? how can i code addition and substraction with define in c ?
thanks in advance
#23179 - poslundc - Tue Jul 06, 2004 6:34 pm
LeMich wrote: |
ex :
11:100 3:4
+ 100:111 4:7
=111:1011 7:11
But in fact 3:4+4:7 = 8:1 and not 7:11 |
The key to fixed point is that the decimal point stays at a fixed position. Your last line of arithmetic is incorrect. It will help if you put all of the zeros in rather than spaces:
Code: |
0011:100 3:(1/2)
+ 0100:111 4:(7/8)
= 1000:011 8:(3/8) |
Note that if you take the colon out and just perform simple binary addition on the two numbers, you get the same answer.
Quote: |
how can i code addition and substraction with define in c ? |
Code: |
#define FIXED_ADD(a, b) ((a) + (b))
#define FIXED_SUB(a, b) ((a) - (b)) |
Note that I am making a satirical point that addition and subtraction are the same thing in fixed-point as they are in regular integer math.
Dan.
#23182 - LeMich - Tue Jul 06, 2004 6:56 pm
thanks ;O)
I see now how it works