The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » sci.math.* » sci.math

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: bcd arithmetic
Replies: 1   Last Post: Jun 14, 1996 3:17 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View  
H. Peter Anvin

Posts: 1
Registered: 12/12/04
Re: bcd arithmetic
Posted: Jun 14, 1996 3:17 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Followup to: <>
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
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:

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.