View Single Post
  #1   Report Post  
Jerry W. Lewis
 
Posts: n/a
Default

"Bruno Campanini" wrote:

They are not affected by floating point bugs.


To call this a "bug" stretches the definition of "bug". Is it a "bug"
that you cannot write 1/3 exactly as a decimal fraction?


No, but there is somwhere a "bug" when adding 100 times
0.01 to 0 I can't get 1.


As I pointed out in my original post to this thread, "most decimal fractions
have no exact binary representation" and so must be approximated. The
approximation to 0.01 is
0.100000000000000002081668171172168513294309377670 2880859375, so it should
not be surprising that when you add 100 values which are each slightly larger
than you expected, that the total will also be slightly larger than you
expected. Taking account of the intermediate roundings (also covered by the
IEEE standard) that will occur, the sum of 100 such approximate values will be
1 + 6.661338147750939242541790008544921875E-16
The Excel formula =(total-1) will return 6.66133814775094E-16 (Excel's
documented 15-digit limit) indicating that the arithmetic is working exactly
as it should.

As I also said in my original post "When you have to approximate your
inputs, that the output is only approximate should be no surprise."

You can see the same phenomenon in decimal (where it may have more
intuition). If you use the VBA Currency data type (4 decimal places)
x = CCur(1 / 3)
total = x + x + x
The value of total will be 0.9999, not 1.0000. Surely you will agree that
there is no bug there.

If you don't want to use the word "bug" suggest me what is
the proper word.


I am open to suggestions, but calling correct math based on necessary
approximations to inputs a "bug" is rather like complaining that it shouldn't
rain. It seems a pointless waste of effort that would be better directed at
learning to predict when it will rain and how to protect yourself when it
does.

If you cannot tolerate slight approximations beyond the 15th significant
figure, then you should stick to integer calculations where those
approximations can be avoided. If you continue to work with decimal
fractions, you should be aware that digits beyond the 15th may not be what
you expect, and subtractions (including the MOD function) may remove some or
even all of the leading digits that originally prevented you from seeing the
approximation. Rounding calculated values to the number of places that you
can be sure of, will usually help you avoid surprises.

These are issues that have been around as long as computers have been doing
finite precision mathematics (long before there was a Microsoft), and are not
unique to Excel.

Jerry