[comp.lang.c] Fixed point numeric routines.

henry@utzoo.uucp (Henry Spencer) (04/08/90)

In article <3804NU013809@NDSUVM1> NU013809@NDSUVM1.BITNET (Greg Wettstein) writes:
>I assume that what I need are fixed point numeric routines which will
>handle only a finite number of decimal places of precision.  I am hoping
>that someone has some pointers to references describing such routines or
>perhaps a few snippets of C code...

If all amounts are guaranteed to be less than about $20 million, by far
the simplest method is to use `long int' and measure money in pennies.
Just because the input and output have to say `$123.45' doesn't mean you
can't store that internally as the integer `12345'.  And the best part is
that `long int' arithmetic is already provided by your compiler.
-- 
Apollo @ 8yrs: one small step.|     Henry Spencer at U of Toronto Zoology
Space station @ 8yrs:        .| uunet!attcan!utzoo!henry henry@zoo.toronto.edu

richard@aiai.ed.ac.uk (Richard Tobin) (04/11/90)

>>I assume that what I need are fixed point numeric routines which will
>>handle only a finite number of decimal places of precision.  

>If all amounts are guaranteed to be less than about $20 million, by far
>the simplest method is to use `long int' and measure money in pennies.

If you need larger amounts, you may find it possible to use floating
point *but still do everything in pennies*.  A double precision floating
point number should be able to store integers of around 50 bits without
loss of accuracy.

-- Richard
-- 
Richard Tobin,                       JANET: R.Tobin@uk.ac.ed             
AI Applications Institute,           ARPA:  R.Tobin%uk.ac.ed@nsfnet-relay.ac.uk
Edinburgh University.                UUCP:  ...!ukc!ed.ac.uk!R.Tobin