delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/19/23:30:32

Message-Id: <199801170217.SAA27916@m3.sprynet.com>
Reply-To: <darnold02 AT sprynet DOT com>
From: "Don Arnold" <darnold02 AT sprynet DOT com>
To: <djgpp AT delorie DOT com>
Subject: Re: Definiton of fixed in Allegro
Date: Fri, 16 Jan 1998 20:07:57 -0600
MIME-Version: 1.0


----------
> From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
> To: djgpp AT delorie DOT com
> Subject: Re: Definiton of fixed in Allegro
> Date: Friday, January 16, 1998 13:26 PM
> 
> 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

there's a good article on fixed point math in the february 98 edition of
c/c++ uj. very interesting reading.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019