Message Boards Message Boards

1
|
682 Views
|
4 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Testing float point operations in programming languages

Posted 4 months ago

POSTED BY: Daniel Carvalho
4 Replies

Michael and Daniel, I appreciate your clarification and additional sources. I did a test with Mathematica by command line on Linux and it works too, out of the GUI notebook. I think the same configurations are in place.

$ math
Wolfram 14.1.0 Kernel for Linux x86 (64-bit)
Copyright 1988-2024 Wolfram Research, Inc.

In[1]:= .1 + .2                                                                                  

Out[1]= 0.3

In[2]:= .1 + .2 == .3                                                                            

Out[2]= True

In[3]:= Internal`$EqualTolerance                                                                 

Out[3]= 2.10721

In[4]:= $MachinePrecision                                                                        

Out[4]= 15.9546
POSTED BY: Daniel Carvalho

Restating slightly the comment by Michael Rogers, the difference between WL and the others is not in the arithmetic but rather in the comparison for equality.

POSTED BY: Daniel Lichtblau

Have you seen section 2 of this (on "cosmetic" rounding in Excel)?: https://people.eecs.berkeley.edu/~wkahan/Mindless.pdf


Re: The main post

To better understand WL, it should be noted that by default, Mathematica rounds the Front-End output to 6 digits, even though internally, the result has full precision (53 bits, or approximately 15.95 decimal digits). This rounding can be changed with PrintPrecision:

Style[.1 + .2, PrintPrecision -> 17]
(*
\!\(\*
StyleBox["0.30000000000000004`",  (* <-- Note the value in quotation marks *)
StripOnInput->False,
PrintPrecision->17]\)
*)

The Front End displays only the string, 0.30000000000000004. This is the same result as all IEEE 754 compliant systems compute.

Another point is that Equal compares with a small relative tolerance. This means that if a and b are nonzero numbers that differ by a very small amount, Equal[a, b] (the same as a == b) will evaluate to True. This is what happens in the first test below, but in the second, the right-hand side is zero:

.1 + .2 == .3
(*  True  *)

.1 + .2 - .3 == 0
(*  False  *)

You can turn off the tolerance to test exact equality by setting Internal`$EqualTolerance to zero:

Block[{Internal`$EqualTolerance = 0.},
 .1 + .2 == .3]
(*  False  *)

Another reference:

Goldberg, What Every Computer Scientist Should Know About Floating-Point Arithmetic (1991)

POSTED BY: Michael Rogers
Posted 2 months ago

MS Excel has precision numerical issues too:

Floating-point arithmetic may give inaccurate results in Excel https://learn.microsoft.com/en-us/office/troubleshoot/excel/floating-point-arithmetic-inaccurate-result

POSTED BY: Updating Name
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract