4

I've run into what seems like a bug in Mathematica 8. I can't find anything related to it online, but I admit I'm not exactly sure what to search for.

If I run this statement:

0.05 + .10 /. {0.15 -> "pass"}
1.04 + .10 /. {1.14 -> "pass"}
1.05 + .10 /. {1.15 -> "pass"}
1.15 /. {1.15 -> "pass"}

I get this output:

pass

pass

1.15

pass

Am I just overlooking something?

Edit: After reading the helpful discussion below, I changed my dispatch table to use a Which statement instead:

f[x_] := Which[x == 1.05, -1.709847, x == 1.10, -1.373823, 
  x == 1.15, -1.119214, x == 1.20, -0.9160143, x == 1.25, -0.7470223, x == 1.30, -0.6015966]

This seems to do the trick.

3 Answers 3

14

Welcome to the world of machine precision. If you examine 1.05 +.10 and 1.15 more closely, you'll see that they're not quite the same:

1.05 + .10 // FullForm

==> 1.1500000000000001`

1.15 // FullForm

==> 1.15`
6
  • I thought I was ruling out a precision issue by showing those other cases. Why would only one particular case fail? Mar 3, 2011 at 19:25
  • +1. Just to complement the answer: instead of the original rules, one may define some kind of tolerance, and encode that into a rule, like say With[{eps = 0.001}, rule[value_] := x_ /; Abs[x - value] < eps -> "pass"]. In this way, your rules will work, say 1.05+.10/.{rule[1.15]}. Mar 3, 2011 at 19:25
  • 3
    @sapient network The point is, you can not rely on this, even if it occasionally works. Other rules worked by a coincidence. Doing it this way, you introduce what in other languages would be called "undefined behavior" - that is, the system gives you no guarantee what will happen. Mar 3, 2011 at 19:28
  • Funny. It is a long time since I've used Mathematica (>10years ago) and I thought this was always about not falling back to the limitations of the machine floating point libraries.
    – Heiko Rupp
    Mar 3, 2011 at 19:28
  • 1
    @Heiko Mathematica can work in arbitrary precision if you like, but the default in many cases is machine precision, because this is so much faster. Mar 3, 2011 at 19:38
6

In addition to incurring small errors when using MachinePrecision, the same floating-point calculation can produce slightly different results at different times of the day. This is not a bug, but rather a by-product of how modern hardware floating-point architectures work

This means you should not use operations like ReplaceAll that depend on knowing exact value of MachinePrecision numbers. Using == (and ===) might be OK because they ignore last 7 (respectively 1) binary digit of MachinePrecision numbers.

Using Mathematica arithmetic should give exact results regardless of time of day, you could use it for your example as follows (10 significant digits)

0.05`10 + .10`10 /. {0.15`10 -> "pass"}
1.04`10 + .10`10 /. {1.14`10 -> "pass"}
1.05`10 + .10`10 /. {1.15`10 -> "pass"}
1.15`10 /. {1.15`10 -> "pass"}

Update: What Every Computer Scientist Should Know About Floating-Point Arithmetic gives some more caveats about floating point arithmetic. For instance, page 80 gives examples of how different implementations of IEEE 754 give slightly different results, even despite being standard compliant

24
  • 2
    From the end of the note at your URL: "the [...] extended precision 80-bit register arithmetic [...] had its own "wobbling precision" problems for numerical code". This issue has not gone away. Different compilers and optimization levels might cause, or not, a storage from 80 bit register to 64 bit memory. This can lead to such oddities. We've looked into several in the past few years. Mar 3, 2011 at 22:55
  • 1
    @Daniel You mean that the old problem with non-deterministic numerical inaccuracies is not completely solved? Mar 3, 2011 at 23:46
  • 2
    @Alexey It is not in any way "solvable", at least not by means accessible to us (which in some sense defines it as "not a problem"). Depends too much on alignment-handling vagaries of MKL libraries, ordering of operations in BLAS, and usage, or not, of extended precision registers. See also IEEE 754: a careful reading may shed light on how different results for the same computation can arise in compliant hardware/software, even on the same machine. Mar 4, 2011 at 0:04
  • 1
    @Alexey I cannot imagine how that might be done with machine arithmetic; it is impossible with current hardware and software(). For significance arithmetic the approach mentioned by Yaroslav should suffice. () One might choose to regard this as a deplorable state of affairs, but it is a fact nonetheless. Mar 4, 2011 at 0:38
  • 2
    @Alexey Depriving you of certainty is probably the lesser of troubles that might arise with extended prec registers. A bigger issue, to me, is that exponent sizes are larger than for double prec. So an overflow might happen, or not, depending on optimization levels that influence whether an intermediate value in a string of computations is kept in a register or stored. Compared to this, relatively modest differences in well conditioned computations strike me as of less significance, if you will pardon the expression. Mar 4, 2011 at 19:41
4

Your replacements only work on exact matches, whereas your While statement uses Equal. You can make the rule based approach work by using Equal as well.

0.05 + .10 /. {x_ /; x == 0.15 -> "pass"}
1.04 + .10 /. {x_ /; x == 1.14 -> "pass"}
1.05 + .10 /. {x_ /; x == 1.15 -> "pass"}
1.15 /. {x_ /; x == 1.15 -> "pass"}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.