Group Abstract Group Abstract

Message Boards Message Boards

0
|
7.9K Views
|
6 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Unexpected result from IntegerPart

Posted 7 years ago
POSTED BY: Dennis Lawler
6 Replies
POSTED BY: Daniel Lichtblau

Dennis,

I would report this to Wolfram Tech support (and post their answer!) because

FullForm[104.91/4.035]

25.999999999999996`

and

In[599]:= $MachinePrecision

Out[599]= 15.9546

but

N[104.91/4.035, 15]

gives 26.

While it is a "quirk" in MMA, Gianluca is correct that you must guard against this when using IntegerPart because for various reasons when using floats MMA results are machine precision and so answers can be slightly off. You would see this behavior for certain operations in C when casting as an integer (which may be the origin of the problem??).

Regards,

Neil

POSTED BY: Neil Singer

IntegerPart is a notoriously discontinuous function, and you are calling it very close to a jump discontinuity. The output 26. is rounded to the nearest integer for display purposes.

POSTED BY: Gianluca Gorni

But the EXACT answer really is 26. No rounding is necessary at all. Any hand calculator will get it right. Doing it by hand makes it absolutely clear. But IntegerPart still screws up? Why exactly would that be?

POSTED BY: Dennis Lawler

But the EXACT answer really is 26. ...

Reals do not have "exact answers"! And $$26. \neq 26 $$

If you want exact results you should use rational numbers, e.g.:

enter image description here

POSTED BY: Henrik Schachner

Run this ...

ToBoxes[104.91/4.035]

It comes out to 25.9999...6. So when the IntegerPart is taken, it rounds down. I am not saying this is good behavior by the Kernel, but it gives you a hint about what is going on: the issue has to do with something to do with floating point computations. I suspect there are others in the Community who will be able to speak to this in greater depth.

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