Everything2
Near Matches
Ignore Exact
Full Text
Everything2

never store currency in a float

created by ariels

(idea) by ariels (1.4 d) (print)   ?   (I like it!) Tue Oct 24 2000 at 7:48:51

Another one of those oft-ignored rules for safe programming. You have a program that works in dollars and cents, say. The most natural mapping of the world to your program's variables is to store dollars in real numbers. So it would seem you'd want stuff like this:

  double price = 0.10;      /* Price is 10 cents */
Take a closer look at what happened here: we didn't store 0.1 in a real number, we stored it in a floating point number! And floating point is not a representation of real numbers (although it plays one on engineering TV shows).

But it's probably an academic issue anyway. And just in case it isn't, we're storing it in double precision. Let's just write the rest of a loop to check it, shall we?

  double sum;
  int i;

  for(i=0; i<100000; i++)
    sum += price;
On a Sun machine, this loop yields 100000.000001 in sum. And that's when using the default rounding imposed by printf() on floating point (in reality, it deviates from the real, correct value before you loop 100 times!)

When you're doing computations on money, you literally can't afford to lose money in the cracks in your program. And this is precisely what happens if you use floating point math to keep track of money.

The solutions exist and are well known. Pick one:

  1. Use BCD floating point, or some other decimal floating point representation. This can accurately represent any decimal fraction, so you don't lose money. Mainframes often support decimal floating point in hardware; "modern" machines don't, but it's possible to build (or buy!) a library and use it.

    But you can still lose money due to cancellation when your calculation involves more digits that the accuracy of your representation.

  2. Use some integer representation to count cents (or ha'pennies, if using). You still have to make sure you write 2 digits to the right of the decimal point (the integer division and modulo operations are your friends here), but you know you're not losing small sums.

    You still have to make sure your integers are large enough to hold any sum you may have in your calculations. Or you can use a bigint package. This can be really simple, as you're never going to multiply 2 bigints (since typically only money needs to be a bigint), and in any case your bigints are quite small (32 digits are far more than enough for any sums of money...).


printable version
chaos

Programming for a drug dealer floating point addiction Perl: Converting an integer or float to currency BCD
Harshad number I assure you, everything is 99 cents Floating point double precision
Dr. Phil's Words of Wisdom Symbolism in Benito Cereno PCB Skittle Brau
ha'penny Tobin Tax mainframe Currency of the World
Lian Li decimal Floater Anonymous
Horseshoes of a Zephyr Cancellation stock market integer
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Little presents from the Node Fairy:
Stained Glass Primer
Scalping
deliciously oily
Civilization
Singing Sand
Choosing a wine
The Soft Bulletin
Irish
Chinese Exclusion Act of 1882
Shall I Compare Thee to a Dead Portuguese Man-of-War Lying Bloated on a Polluted Beach?
Living up to your potential
Vasilopita
Brace Yourself
New Writeups
XWiz
Trism(review)
artman2003
Briefcase Full of Souls - Part I(fiction)
Dreamvirus
Alan Ladd(person)
waverider37
Harold Holt(person)
The Debutante
Until death do us part(fiction)
Ysardo
a brother to a sister(personal)
antigravpussy
your warm whispers(personal)
Clarke
Multiculturalism(idea)
aneurin
Earl of Landaff(person)
Heitah
Pseudocide(idea)
XWiz
Google Knol(lede)
Mythi
July 24, 2008(personal)
locke baron
The fall of Earth(fiction)
BookReader
Fear the Cold(dream)
Pavlovna
Kathleen MacInnes(person)
This affordable entertainment brought to you by The Everything Development Company