Re: This calculation is just wrong / computer can't count!



It is the programmer's responsibility to do the rounding if a
repeating decimal results from division.

Given the OP's fixation on decimal I judged that at least he could see
that BCD was doing the same thing as his "accurate" calculator.

BCD also has the advantage of providing more significant decimal
digits.

-GB


On Mon, 08 Oct 2007 15:16:48 -0400, Joseph M. Newcomer
<newcomer@xxxxxxxxxxxx> wrote:

Unfortunately, packed decimal has exactly the same problem. While indeed you will get
0.83333333333333333333333333333 (that's 31 digits of accuracy, if I counted right) for
the intermediate result, when you multiply this by 30 you get

24.99999999999999999999999999999

which is not 25. OTOH, I prove, in a later message, that in binary floating point
arithemtic, AS IMPLEMENTED ON THE PENTIUM FPU, as demonstrated by my Floating Point
Explorer,

0.83333333333333337 * 30.00000000000000000 = 25.00000000000000000

that is

0x3FEAAAAAAAAAAAAB * 0x403E000000000000 = 0x4039000000000000

which is MORE ACCURATE than the purported "correct maths", whcih produce an ERRONEOUS
answer. So it makes me wonder about who is or is not in contact with reality here...
joe

On Mon, 08 Oct 2007 11:23:46 -0500, Geeky Badger <spamhater@xxxxxxxxxxxxxxxxxxxx> wrote:

GT,

If you insist on getting an accurate answer in the decimal system,
then you should find a processor that supports Binary Coded Decimal
(BCD) math. The 8-bit 6502 had a bit for this. The IBM 370 family of
mainframes has BCD instructions (Add/Subtract/Multiple/Divide in
PACKED mode).

If you can't understand that using binary to represent decimal digits
must always lead to partial decimal digits then you are doomed to
argue like this forever.

There are three solutions to your problem:

1. Handle the partial decimal digit problems in your code by using a
floating point variable with sufficient significant digits and then
properly rounding.

2. Find a machine which supports BCD and use that for your
calculations.

3. State your problem in binary. Then the binary answer will be
accurate. :) You can easily prove this by finding a calculator that
supports binary and running the numbers on it.

-GB

On Mon, 8 Oct 2007 10:48:59 +0100, "GT"
<ContactGT_remove_@xxxxxxxxxxx> wrote:

"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:rnhgg3tqi4f0np08ies2o9bviq4o4994jt@xxxxxxxxxx
See all my earlier posts. Your expectations are erroneous, and all your
problems stem
from a failure to understand reality.

Odd - reality states that 25/30, then * 30 is 25! My grasp on reality is
sound, yours seems to be away with the fairies!

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Bit representation of a float
    ... of floating point numbers must follow certain patterns as laid out in ... You must have a fixed base and precision for each floating ... in the underlying bits is not specified by the Standard, ... number of decimal digits, q, such that any floating point number ...
    (comp.lang.c)
  • Re: Deviation of double
    ... int iDigits); ... I believe you're confused about rounding. ... round to 90.62 if only two decimal digits are required. ... Why are you expecting 90.625? ...
    (comp.lang.cpp)
  • Re: Rounding Float in C and Remove those Zeros
    ... Beware numbers like 1000000000000042.0000, ... c.l.c posts seem to justify my statement that certain range of ... integers is indeed exactly representable in floating point types. ... the number of decimal digits of precision in a `float`, ...
    (comp.lang.c)
  • Re: FLT_DIG
    ... floating point with radix 100, the C99 formula says that it can represent ... any floating point number with 2 decimal digits while it is unable to ...
    (comp.std.c)
  • Re: Rounding error when doing float-to-integer
    ... Agree with Glenn here. ... significant figures" step. ... The rounding is to 24 binary digits. ... The "7 decimal digits" bit is only a rough approximation that allows ...
    (comp.lang.fortran)