Re: Rounding error when converting from double to int
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Tue, 04 Aug 2009 22:29:16 +0000
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
.
- Follow-Ups:
- Re: Rounding error when converting from double to int
- From: Flash Gordon
- Re: Rounding error when converting from double to int
- References:
- Prev by Date: Rounding error when converting from double to int
- Next by Date: Re: Zero terminated strings
- Previous by thread: Rounding error when converting from double to int
- Next by thread: Re: Rounding error when converting from double to int
- Index(es):
Relevant Pages
|