Avoiding float and double in monetary calculation

W

wane

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
 
M

Mike Wahler

wane said:
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?

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
 
G

Gordon Burditt

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.
What is a good alternative?

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
 
R

Rouben Rostamian

wane said:
I have heard that one should avoid using float and double in monetary
calculation because of the lack of preciseness.

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...
What is a good alternative?

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.
 
E

E. Robert Tisdale

wane said:
I have heard that one should avoid using float and double in monetary
calculation because of the lack of preciseness.

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 ;-)
 
O

osmium

wane said:
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?

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

Morris Dovey

wane said:
I have heard that one should avoid using float and double in monetary
calculation because of the lack of preciseness.

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.
What is a good alternative?

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?
 
R

Randy Howard

kevin@-nospam- said:
At a bit over 3 million dollars a pop, those had better be solid gold
toilet seats.

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

Phil Tregoning

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 ;-)

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.
 
K

Keith Thompson

Morris Dovey said:
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).

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

Malcolm

Rouben Rostamian said:
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.
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.
 
M

Mike Wahler

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

-Mike
 
M

Morris Dovey

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

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...
 
M

Mark McIntyre

Hello,
I have heard that one should avoid using float and double in monetary
calculation because of the lack of preciseness.

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.
What is a good alternative?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,077
Messages
2,570,568
Members
47,204
Latest member
abhinav72673

Latest Threads

Top