Re: Rounding error when converting from double to int



mark said:

Steve McConnel, in his book "Code Complete", suggests converting
money amounts from double to integer in order to avoid rounding
errors
during calculations. So we made a money class where the money
amount
is stored as an integer instead of a double. The cents are stored
in the 10's and 1's position of the integer.

For example: $1.23 is stored as the integer 123. The cents portion
is 23, and the rest is the dollar portion.

In other words, you're storing the number of cents.

However we are having trouble initializing this integer amount when
the original double amount contains fractions of a cent (due to some
interest calculations). We want to round any fractional cents up to
the nearest cent. We use a rounding scheme where values greater
than or equal to 5 are rounded up.

Below is our initialization function. Note that we use the type
__int64 so we can hold larger numbers but the same problems happen
with type int also. myCurrencyx100 is of the type __int64.

Then why not just store the number of millicents? Even assuming you
need a sign bit, a 64-bit integer type will still allow you to
represent $92,233,720,368,547 - which ought to be enough for anyone.

The code multiplies the double by 100 first, then it rounds to the
nearest 1, then it converts to int.
So if value = 9.495 we want myCurrencyx100 to be initialized to 950.
But the code sets it to 949.

The binary representation of 9.495 is bound to be a smidgen under that
- 9.494995... or so. 100 times this is 949.4995..., add 0.5 to get
949.9995, convert to int, hey presto, 949. Solution: add a smidgen
before multiplying. 0.0001 is probably about right.

9.494995... + 0.0001 lifts the value to a tiny bit above 9.495...,
after which multiplying by 100, adding (or subtracting) 0.5, and
converting to int will work as you expect.

(Note that this is a C newsgroup, not a C++ newsgroup.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
This line unintentionally left unblank
.



Relevant Pages

  • Re: Manipulating user input
    ... int, double, some custom money type? ... Integer variables are the best for manipulating money, ... in cents, and then convert to dollars when you have to output figures. ... incorrect input on the part of the user, it will accept some incorrect input ...
    (comp.lang.cpp)
  • Rounding error when converting from double to int
    ... Steve McConnel, in his book "Code Complete", suggests converting money ... So we made a money class where the money amount ... interest calculations). ... then it converts to int. ...
    (comp.lang.c)
  • Re: Plan to melt pennies fails to solidify
    ... consistently screw customers out of that amount without being discovered. ... grocery store and guess whether the total is over or under the next ... one or two cents one way or the other on a purchase of more than $10, ... get all the money, ...
    (rec.collecting.coins)
  • Re: Math Puzzle -- Dollars and Cents / Cents and Dollars
    ... > If I have a certain amount of money in Dollars & Cents and I spend half ... > Cents & Dollars. ... but it fits the problem statement. ...
    (sci.math)
  • OT Misplaced decimal points (was Re: Cost of Machine Quilting?)
    ... The problem was that the amount was originally ... eleven dollars and eleven cents. ... It doesn't matter if it's money or apples. ... If you specify denomination the math applies to that denomination. ...
    (rec.crafts.textiles.quilting)