Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.3K Views
|
6 Replies
|
1 Total Like
View groups...
Share
Share this post:

IntegerPart confusion

Posted 10 years ago

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.

POSTED BY: Arman S
6 Replies

@Arman Esmaili please do NOT use word "bug" in titles unless it was confirmed.

POSTED BY: EDITORIAL BOARD

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
POSTED BY: Ilian Gachevski
Posted 10 years ago

Thanks!

POSTED BY: Arman S

In addition you could use exact arithmetic by typing 113/100 (rather than 1.13). This way everything will be done exactly.

POSTED BY: Sander Huisman

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.

POSTED BY: Ilian Gachevski
Posted 10 years ago

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?

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