# Mathematica WolframAlpha miscalculates a simple arithmetic expression

Tags:
1. May 25, 2015

### cryptist

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)

2. May 25, 2015

### gsal

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.

3. May 25, 2015

### Staff: Mentor

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!

4. May 25, 2015

### cryptist

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.

5. May 25, 2015

### cryptist

No. I tried what you said and I get wrong answer again.

6. May 25, 2015

### Staff: Mentor

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.

7. May 25, 2015

### Staff: Mentor

Interesting. For me running Mathematica 10.0.0.0 on 64-bit Windows
Code (Text):
Floor[Sqrt[1144/100 - 10]/(2/10)]
gives 6.

Edit: same code gives the same result on Wolfram Alpha.

8. May 25, 2015

### cryptist

When I wrote Floor[Sqrt[1144/100 - 10.]/(2/10)] it gives 5 again. (With dot next to 10).

9. May 25, 2015

### cryptist

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. May 25, 2015

### gsal

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. May 25, 2015

### cryptist

That's it! Wolfram should provide a similar package too I think. It shouldn't be so difficult.

12. May 25, 2015

### Staff: Mentor

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. May 25, 2015

### Staff: Mentor

Here you go.
Code (Text):
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.

14. May 26, 2015

### cryptist

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