Mail Archives: djgpp/1998/01/16/16:46:02
riotnrrd writes:
>Shawn : If I have a fixed type variable called fix_var or whatever,
>can I use -fix_var to get the negative value of it?
Sure. It is very easy to figure out what you can and can't do with a
fixed point number, once you work out the math behind it.
To store a fractional values in what is really an integer data type,
fixed point routines simply scale the value up by some multiplier, which
in the case of a 16.16 format is 2^16 = 65536. If you call this factor
f, you would represent the fractional value x as the integer x*f, and y
as y*f. After which it is just basic algebra...
x*f + y*f = (x+y)*f - correct, so you can add them
x*f - y*f = (x-y)*f - correct, so you can subtract them
x*f * y*f = (x*y)*f^2 - wrong! multiplication is different
x*f / y*f = x/y - wrong! division is also special
The fact that you have scaled up the values won't make any difference
when you want to compare or negate them, so it is safe to do that with
normal integer operators.
--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
"Pigs use it for a tambourine" - Frank Zappa
- Raw text -