Re: Rounding error when converting from double to int



Do your calculation that generates the fractions in integers. I'm
assuming here that negative numbers are not involved. For example,
if you have $9.51 (represented as 951 cents) and you need to take
8.875% of it (suppose it's a sales tax calculation), multiply 8.875%
by one million and get 88750. (This presumes that sales tax rates
are always multiples of 0.0001%). Be sure *NOT* to put 8.875% into
a double. Represent it as 88750 / 1000000, and treat numerator and


first of doing this division, from 8.875 -> 88750 is there one rounding?

I told you above *NOT* to put the percentage into a double in the first
place, so there is no rounding.

if for example we have 8.795% and it is rappresented in double like
8.79499999999999999

DO NOT put that value into a double.

the result afther the "->" will be 87949 of 87950?

The multiplication should be done in mathematics, not C.

.



Relevant Pages

  • Re: Rounding error when converting from double to int
    ... if you have $9.51 (represented as 951 cents) and you need to take ... 8.875% of it (suppose it's a sales tax calculation), ... are always multiples of 0.0001%). ... from 8.875 -> 88750 is there one rounding? ...
    (comp.lang.c)
  • Re: Rounding error when converting from double to int
    ... 8.875% of it (suppose it's a sales tax calculation), ... are always multiples of 0.0001%). ... so there is no rounding. ... The multiplication should be done in mathematics, ...
    (comp.lang.c)
  • Re: sprintf rounding behavior
    ... first i guessed that this is an inherent problem due to the binary ... been no round-off errors at all, and this would place them exactly at the ... half way points when it comes to rounding. ... errors in originally calculating the odd multiples of .05. ...
    (comp.soft-sys.matlab)
  • Re: Rounding error when converting from double to int
    ... are always multiples of 0.0001%). ... Divide by one million, convert to an integer, and get 84 cents. ... round 84 up to 85. ... result, you are representing money briefly as millionths of cents, ...
    (comp.lang.c)
  • Re: Expressing fractions
    ... Dik> rounding to 5 cents has been standard in the Netherland from ... payments must always be in multiples of 10 cents. ... supermarket-shopping is a convenient way to get the 1/2-cent coins. ...
    (sci.lang)

Loading