WolframAlpha miscalculates a simple arithmetic expression

Click For Summary
SUMMARY

The forum discussion highlights a significant bug in WolframAlpha and Mathematica regarding the calculation of the expression "Floor(sqrt(11.44-10)/0.2)", which incorrectly returns five (5) instead of the correct answer, six (6). The root cause is identified as the engine's inability to accurately compute the subtraction due to floating-point representation errors, resulting in an approximate value of 1.4399999999999995 instead of the exact 1.44. Users are advised to use exact inputs, such as "1144/100 - 10", to achieve accurate results and consider alternative functions like Round for specific scenarios.

PREREQUISITES
  • Understanding of floating-point arithmetic and its implications in programming.
  • Familiarity with WolframAlpha and Mathematica syntax and functions.
  • Knowledge of mathematical functions such as Floor and Round.
  • Basic understanding of number representation in computing (base-2 vs. base-10).
NEXT STEPS
  • Research "WolframAlpha input precision" to understand how input affects output accuracy.
  • Learn about "floating-point representation errors" in programming languages.
  • Explore the "decimal module in Python" for precise decimal arithmetic.
  • Investigate alternative mathematical libraries that handle decimal arithmetic more accurately.
USEFUL FOR

This discussion is beneficial for mathematicians, software developers, data analysts, and anyone utilizing WolframAlpha or Mathematica for precise calculations, particularly in scenarios requiring exact arithmetic results.

cryptist
Messages
121
Reaction score
1
There is a crucial bug on both WolframAlpha and Mathematica. When we give the input "Floor(sqrt(11.44-10)/0.2)" it gives wrong answer. The actual answer is six (6) but wolframalpha gives five (5). The problem is Wolframalpha calculates 11.44-10 incorrectly. Although the answer is exactly 1.44, engine calculates as 1.4399999999999995.

It is suprising that this program cannot give true answer for such an elementary arithmetic equation.

See:
https://www.wolframalpha.com/input/?i=Floor(sqrt(11.44-10)/0.2)
 
Physics news on Phys.org
Actually, it is not surprising at all...it is just that you need to be aware that when working with computers, your base-10 numbers will be converted into base-2 and represented with a finite number of bits...so, often, there a "lost in translation" problem you, as a computer user, need to be aware and program around if it affects you. For example, one common problem is to attempt a boolean expression in an if statement and test if a real number is zero or some other exact integer.

In this particular case, depending on what you wanted, maybe you should use Round, instead of Floor; or Floor( sqrt(11.44-10)/0.2 + 0.5 )...depending on what you REALLY want to achieve and if you KNOW what you are doing.
 
  • Like
Likes   Reactions: scottdave
cryptist said:
Although the answer is exactly 1.44,
The answer is not exactly 1.44, it is approximately 1.44 to within machine precision. Remember, a computer uses base 2 numbers, not base 10 numbers, so not all base 10 numbers can be exactly represented in base 2.

If you want exactly 1.44 then you need to input 1144/100 - 10. That will get you 144/100, or exactly 1.44

EDIT: gsal beat me to it!
 
It is NOT related to the thing that you mentioned, because with different numbers, I get right answers. For example 20.46-10 (substraction expression) gives the right result but 11.44-10 gives wrong result. So this prooves that there is a clear bug. Besides, 11.44 is an exact number. So it should not give such long decimals..

And actually I think it is a major problem. I cannot use Round because it changes my results completely, as you may understand. I am calculating something based on Floors and Ceils and because of this wrong calculation I get wrong results and I spend many hours to see where my mistake is, but eventually I understand that it was not my mistake.
 
DaleSpam said:
If you want exactly 1.44 then you need to input 1144/100 - 10. That will get you 144/100, or exactly 1.44

No. I tried what you said and I get wrong answer again.
 
cryptist said:
with different numbers, I get right answers
That actually indicates that it is exactly what gsal and I mentioned. That is a typical consequence of this issue, in fact it is almost a defining feature.
 
cryptist said:
No. I tried what you said and I get wrong answer again.
Interesting. For me running Mathematica 10.0.0.0 on 64-bit Windows
Code:
Floor[Sqrt[1144/100 - 10]/(2/10)]
gives 6.

Edit: same code gives the same result on Wolfram Alpha.
 
When I wrote Floor[Sqrt[1144/100 - 10.]/(2/10)] it gives 5 again. (With dot next to 10).
 
I think I am getting what you say. But don't you think Wolfram team should find a solution for this? Maybe they should modify their codes, because this is the reason I get wrong answers on my study and it is clearly wrong. I am writing an exact expression, I think it should understand it before turning it into a bunch of decimals. I also reported the problem to Wolfram but I didn't get an answer yet. (It was about a month ago)
 
  • #10
Maybe one day, we will have base-10 computers or maybe one day we (humans) will drop base-10 and start counting in binary...don't know. In the mean time, you need to be aware of this and, no, Wolfram is not going to re-write any software.

To be sure, different programs provide a way to do decimal arithmetic for when it is truly necessary, for example, Python provides the decimal package:

>>> from decimal import *
>>> a = 11.44 - 10.0
>>> a
1.4399999999999995
>>> b = Decimal('11.44') - Decimal('10.0')
>>> b
Decimal('1.44')
>>>
 
  • #11
That's it! Wolfram should provide a similar package too I think. It shouldn't be so difficult.

Thanks to you all for your answers.
 
  • #12
cryptist said:
When I wrote Floor[Sqrt[1144/100 - 10.]/(2/10)] it gives 5 again. (With dot next to 10).
Yes, because then the thing inside the square root is not an exact number. It is no longer exactly equal to 1.44, but only approximately equal to within machine precision.

If you write machine precision input then at most you can get machine precision output (and usually it is less). If you need exact output then you must give exact input. The result of adding an exact number to a machine precision number is a machine precision number.
 
  • #13
cryptist said:
Wolfram should provide a similar package too I think. It shouldn't be so difficult.
Here you go.
Code:
decimal[x_?NumberQ, dig_Integer: 2] := Round[x 10^dig]/10^dig

It defaults to considering two digit input exact, but you can do a different number of digits if you want.
 
  • Like
Likes   Reactions: cryptist
  • #14
DaleSpam said:
Here you go.
Code:
decimal[x_?NumberQ, dig_Integer: 2] := Round[x 10^dig]/10^dig

It defaults to considering two digit input exact, but you can do a different number of digits if you want.

Wow! That's it. Thank you for the code!
 

Similar threads

Replies
10
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 67 ·
3
Replies
67
Views
16K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
7K
Replies
6
Views
4K