sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
wane's Avatar

Avoiding float and double in monetary calculation


Question posted by: wane (Guest) on November 13th, 2005 05:35 PM
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

14 Answers Posted
Mike Wahler's Avatar
Guest - n/a Posts
#2: 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



Gordon Burditt's Avatar
Guest - n/a Posts
#3: 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
osmium's Avatar
Guest - n/a Posts
#4: 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.


Rouben Rostamian's Avatar
Rouben Rostamian November 13th, 2005 05:35 PM
Guest - n/a Posts
#5: 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>
E. Robert Tisdale's Avatar
E. Robert Tisdale November 13th, 2005 05:35 PM
Guest - n/a Posts
#6: 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 ;-)





Morris Dovey's Avatar
Guest - n/a Posts
#7: 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

Randy Howard's Avatar
Guest - n/a Posts
#8: 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.

Phil Tregoning's Avatar
Guest - n/a Posts
#9: 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.
Keith Thompson's Avatar
Guest - n/a Posts
#10: 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) Join Bytes! <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Malcolm's Avatar
Guest - n/a Posts
#11: 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.


Mike Wahler's Avatar
Guest - n/a Posts
#12: 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



Morris Dovey's Avatar
Guest - n/a Posts
#13: 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

Morris Dovey's Avatar
Guest - n/a Posts
#14: 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

Mark McIntyre's Avatar
Guest - n/a Posts
#15: 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 =---
 
Not the answer you were looking for? Post your question . . .
196,873 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,873 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors