Followup to: <JAAPJL.96Jun10232201@asmobj.larc.nasa.gov> By author: J Lee Jaap <JaapJL@ASMObj.LaRC.NASA.Gov> In newsgroup: comp.std.internat > > |>>In other words: for U.S. currency, you keep track of cents. Not > |>>dollars. Cents. > |>> > |>This approach works in simplistic terms, for instance, when you are adding > |>two amounts. But how do you figure tax using only integers? If you > |>use only integer types, how do you figure interest, which is an > |>exponential? > |>You are back to using floating point in either case, which never eliminates > |>the problem. > |> > |>All real world accounting systems have to deal with non-integer amounts at > |>some point in the calculation stream. >
Do they? Really? Do you submit $1036.284378628 in taxes to the Government? Most likely you have certain rounding rules that applies. Intermediate results may require additional precision, but floating point (of any kind) is lossy, so you may not want to use that even then.
To calculate 7.75% tax using integer math:
tax = ( value * 775 + ROUNDING_FACTOR ) / 10000;
... where ROUNDING_FACTOR is 0 for round down, 5000 for round to nearest, and 9999 for round up.
The most important this is to get enough digits, not the exponents.
Interest isn't an exponential (unless you're talking in theoretical terms -- i.e. you're talking approximates, not exacts, in which case floating-point is fine). It is an integer exponentiation; the exponent is always an integer.
-- PGP public key available - finger email@example.com I don't work for Yggdrasil, but they sponsor the linux.* hierarchy. "The earth is but one country, and mankind its citizens." -- BahÃÂÃÂ¡'u'llÃÂÃÂ¡h Just Say No to Morden * Save Babylon 5: http://www.babylon5.com/cmp/support/