Try the followings:
IntegerPart[3700*(1.13 - 1)]
3700*(1.13 - 1)
For the first one the answer is 480 and for the second one 481.!!! This happens just for some values; if you replace 1.13 with 1.12 the answers match.
Try the followings:
IntegerPart[3700*(1.13 - 1)]
3700*(1.13 - 1)
For the first one the answer is 480 and for the second one 481.!!! This happens just for some values; if you replace 1.13 with 1.12 the answers match.
It’s not a bug, of course, that’s how floating point arithmetic works.
Also not specific to Mathematica, for example consider the python result
Python 2.6.6 (r266:84292, Jul 22 2015, 16:47:47)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 3700*(1.13-1)
480.9999999999996
which makes the integer part 480 and not 481.
The Mathematica result is exactly the same,
In[1]:= 3700*(1.13-1) // InputForm
Out[1]//InputForm= 480.9999999999996
but is just printed as 481., because by default only 6 significant digits are displayed.
Thanks Ilian! Then, how one can predict for which values of x in 3700*(x-1) the answer is a bit smaller or larger than the exact value?
While such a prediction can probably be formulated by looking at the binary representation of x, note with Mathematica you can always use extended precision and arrive at the right result
In[1]:= 3700*(1.13`20 - 1)
Out[1]= 481.0000000000000000
In[2]:= IntegerPart[%]
Out[2]= 481
Thanks!
@Arman Esmaili please do NOT use word “bug” in titles unless it was confirmed.
In addition you could use exact arithmetic by typing 113/100 (rather than 1.13). This way everything will be done exactly.