Connecting Tech Pros Worldwide Help | Site Map

Avoiding float and double in monetary calculation

  #1  
Old November 13th, 2005, 05:35 PM
wane
Guest
 
Posts: n/a
Hello,
I have heard that one should avoid using float and double in monetary
calculation because of the lack of preciseness.
What is a good alternative?

Thanks

  #2  
Old November 13th, 2005, 05:35 PM
Mike Wahler
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


wane <nospam@intechs.net> wrote in message
news:bi19ji$44ma5$1@ID-18576.news.uni-berlin.de...[color=blue]
> Hello,
> I have heard that one should avoid using float and double in monetary
> calculation because of the lack of preciseness.
> What is a good alternative?[/color]

Use an integer type, e.g. 'long'. E.g. if representing
U.S. currency, store pennies, not dollars, and do decimal point
detection and placement during i/o.

-Mike



  #3  
Old November 13th, 2005, 05:35 PM
Gordon Burditt
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


>calculation because of the lack of preciseness.

This depends, of course, on how much precision you need. If you're
estimating the cost of 1500 government toilet seats, "about 5 billion
dollars" might be good enough. If you're auditing the cost of 1500
toilet seats, you may need to worry about hundredths of cents.
[color=blue]
>What is a good alternative?[/color]

Integer quantities of the smallest monetary unit you need to deal
with (e.g. cents). If you've got an integer type bigger than 32
bits, try to use it. C89 doesn't guarantee that there is such a
type.

Gordon L. Burditt
  #4  
Old November 13th, 2005, 05:35 PM
osmium
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


wane writes:
[color=blue]
> I have heard that one should avoid using float and double in monetary
> calculation because of the lack of preciseness.
> What is a good alternative?[/color]

Some compilers offer a BCD (binary coded decimal) data type via an included
library.


  #5  
Old November 13th, 2005, 05:35 PM
Rouben Rostamian
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


In article <bi19ji$44ma5$1@ID-18576.news.uni-berlin.de>,
wane <nospam@intechs.net> wrote:
[color=blue]
>I have heard that one should avoid using float and double in monetary
>calculation because of the lack of preciseness.[/color]

Bill Gates has 100,000,000,000 in his savings account. His bank's
statement is printed using:

#include <stdio.h>
int main(void)
{
float y = 1e11F;

printf("Your balance is %.2f\n", y);
return 0;
}

The bank's computer prints the result as:

Your balance is 99999997952.00

Then Bill buys the bank...
[color=blue]
>What is a good alternative?[/color]

When dealing with Bill Gates' bank account, use an arbitrary precision
arithmetic library. Search the web for GNU MP library, but don't
tell Bill it's GNU.

--
Rouben Rostamian <rostamian@umbc.edu>
  #6  
Old November 13th, 2005, 05:35 PM
E. Robert Tisdale
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


wane wrote:
[color=blue]
> I have heard that one should avoid using float and double in monetary
> calculation because of the lack of preciseness.[/color]

That's *not* true.
Double precision floating-point numbers are very precise.
Monetary calculations aren't very precise.
It's just that special rules for rounding are used
in calculations involving money.

If, for example, you divide $1.00 among three people,
there will be an extra penny left over.
Banks always keep this extra penny ;-)





  #7  
Old November 13th, 2005, 05:35 PM
Morris Dovey
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


wane wrote:
[color=blue]
> I have heard that one should avoid using float and double in monetary
> calculation because of the lack of preciseness.[/color]

It depends on the range of values possible and the ability of the
programmers. The oldest stock exchange in the USA (PHLX) performs
monetary calculations using doubles for equity, option, and
currency trading - without precision problems.
[color=blue]
> What is a good alternative?[/color]

Again, it depends on the range of values. For a program on my
machine to keep track of my checking account, a char should be
sufficient to handle the full range in pennies ($1.27 through
-$1.28).

Your checking account may need to use long int; and your central
bank would probably need long long int values.

Question for you: What is the 1/100 part of a Euro called?
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

  #8  
Old November 13th, 2005, 05:36 PM
Randy Howard
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


In article <newscache$9ddyjh$wng$1@tomato.pcug.org.au>, kevin@-nospam-
pcug.org.au says...[color=blue]
> Gordon Burditt <gordonb.2bxh2@sneaky.lerctr.org> wrote:[color=green][color=darkred]
> >>calculation because of the lack of preciseness.[/color]
> >
> > This depends, of course, on how much precision you need. If you're
> > estimating the cost of 1500 government toilet seats, "about 5 billion
> > dollars" might be good enough. If you're auditing the cost of 1500
> > toilet seats, you may need to worry about hundredths of cents.[/color]
>
> At a bit over 3 million dollars a pop, those had better be solid gold
> toilet seats.[/color]

They're $3 toilet seats. Come on, how do you think the NSA funds all
their super secret underground computers? $3million buck toilet seats.

  #9  
Old November 13th, 2005, 05:36 PM
Phil Tregoning
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote in
news:3F443B09.9000806@jpl.nasa.gov:
[color=blue]
> wane wrote:
>[color=green]
>> I have heard that one should avoid using float and double in monetary
>> calculation because of the lack of preciseness.[/color]
>
> That's *not* true.
> Double precision floating-point numbers are very precise.
> Monetary calculations aren't very precise.
> It's just that special rules for rounding are used
> in calculations involving money.
>
> If, for example, you divide $1.00 among three people,
> there will be an extra penny left over.
> Banks always keep this extra penny ;-)
>[/color]

I thought it was redirected to the bank account of the
programmer that wrote the code. That's how it used to
work in the good old days (*).

Phil T

(*) This is an urban legend.
  #10  
Old November 13th, 2005, 05:37 PM
Keith Thompson
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


Morris Dovey <mrdovey@iedu.com> writes:
[...][color=blue]
> Again, it depends on the range of values. For a program on my machine
> to keep track of my checking account, a char should be sufficient to
> handle the full range in pennies ($1.27 through -$1.28).[/color]

You should either explicitly use signed char, or be more careful about
bouncing checks.

--
Keith Thompson (The_Other_Keith) kst@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
  #11  
Old November 13th, 2005, 05:37 PM
Malcolm
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation



"Rouben Rostamian" <rouben@pc18.math.umbc.edu> wrote in message[color=blue]
>
> When dealing with Bill Gates' bank account, use an arbitrary precision
> arithmetic library. Search the web for GNU MP library, but don't
> tell Bill it's GNU.
>[/color]
This leads to the serious point that you could always have hyper-inflation,
which means that a program handling monetary values is likely to break
unless it uses arbitrary-precision integers.


  #12  
Old November 13th, 2005, 05:37 PM
Mike Wahler
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


Keith Thompson <kst@cts.com> wrote in message
news:yecy8xm68e6.fsf@king.cts.com...[color=blue]
> Morris Dovey <mrdovey@iedu.com> writes:
> [...][color=green]
> > Again, it depends on the range of values. For a program on my machine
> > to keep track of my checking account, a char should be sufficient to
> > handle the full range in pennies ($1.27 through -$1.28).[/color][/color]

ITYM " ... through -$1.27" Some implementations allow -128
in a signed char, but it's not required.

-Mike



  #13  
Old November 13th, 2005, 05:37 PM
Morris Dovey
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


Mike Wahler wrote:[color=blue]
> Keith Thompson <kst@cts.com> wrote in message
> news:yecy8xm68e6.fsf@king.cts.com...
>[color=green]
>>Morris Dovey <mrdovey@iedu.com> writes:
>>[...]
>>[color=darkred]
>>>Again, it depends on the range of values. For a program on my machine
>>>to keep track of my checking account, a char should be sufficient to
>>>handle the full range in pennies ($1.27 through -$1.28).[/color][/color]
>
> ITYM " ... through -$1.27" Some implementations allow -128
> in a signed char, but it's not required.[/color]

I guess that means that I'll just have to be more careful about
the checks I write (or start using my thumbs in personal
financial calculations [thus far I've reserved my thumbs as carry
and overflow bits] - I've considered using one thumb as sign and
the other as a ninth [high order] magnitude bit; but I'm not
certain that I actually have a need for the additional capacity -
and I have serious misgivings about undetected under-/overflow
conditions...
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

  #14  
Old November 13th, 2005, 05:38 PM
Morris Dovey
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


John L Fjellstad wrote:[color=blue]
> Morris Dovey wrote:
>[color=green]
>>Question for you: What is the 1/100 part of a Euro called?[/color]
>
> Cent.[/color]

Thanks. Should be easy (even for me) to remember.
--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c

  #15  
Old November 13th, 2005, 05:41 PM
Mark McIntyre
Guest
 
Posts: n/a

re: Avoiding float and double in monetary calculation


On Wed, 20 Aug 2003 22:08:40 -0400, in comp.lang.c , wane
<nospam@intechs.net> wrote:
[color=blue]
>Hello,
>I have heard that one should avoid using float and double in monetary
>calculation because of the lack of preciseness.[/color]

This is certainly a viewpoint. For what its worth, I work in
investment banking, and we find doubles quite adequate even for large
scale financial transactions. In over a decade in the buisness I have
come across only a couple of instances where it actually mattered.
[color=blue]
>What is a good alternative?[/color]

If you want perfect precision, use integer types, and a scaling factor
to turn back into pennies. Note that this can limit your range unless
you use special big number handling routines. The assets of many a
company, converted into say Turkish Lira, would overflow most builtin
types. It may also be slower, esp if you have to use a special
library.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Closed Thread