Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #16157 > unrolled thread

Re: Round-off error?

Started byBill Rowe <readnews@sbcglobal.net>
First post2013-11-02 06:21 +0000
Last post2013-11-05 04:11 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Round-off error? Bill Rowe <readnews@sbcglobal.net> - 2013-11-02 06:21 +0000
    Re: Round-off error? Richard Fateman <fateman@cs.berkeley.edu> - 2013-11-05 04:11 +0000

#16157 — Re: Round-off error?

FromBill Rowe <readnews@sbcglobal.net>
Date2013-11-02 06:21 +0000
SubjectRe: Round-off error?
Message-ID<l525kr$9ft$1@smc.vnet.net>
On 10/30/13 at 11:47 PM, darrylyong@gmail.com (D Yong) wrote:

>The following lines of code are stumping me.

>ClearAll[y] dt = 0.15; Do[y[i*dt] = "hi there", {i, 0, 20}]
>Table[{i, y[(i + 1)*dt], y[i*dt + dt]}, {i, 0, 19}]

>Here is the output:

<output snipped>

>It seems that y[i*dt+dt] does not match y[(i+1)*dt], perhaps due to
>numerical round-off?

The problem is machine arithmetic, the order in which things are
done and the fact 0.15 can not be represented exactly in a
finite number of binary digits. You can verify the issue is
caused by this by doing:

In[3]:= dt = .15;
n = 5;

In[5]:= RealDigits[(1 + n) dt] === RealDigits[n dt + dt]

Out[5]= False

>If you switch to dt=15/100, the problem goes away.  You might also try
>different values for dt to see what happens.

Right. By using 15/100 you are using exact arithmetic rather
than machine precision values and there is no round off.

>If it is numerical round-off, then why does this evaluate to true?

>6*.15 == 5*.15 + .15 True

Look at the documentation for Equal and note under details

Approximate numbers with machine precision or higher are
considered equal if they differ in at most their last seven
binary digits (roughly their last two decimal digits).

And

In[6]:= Pick[Range[Length@RealDigits[(1 + n) dt, 2][[1]]],
  Equal @@@
   Transpose[{RealDigits[(n + 1) dt, 2][[1]],
     RealDigits[n dt + dt, 2][[1]]}], False]

Out[6]= {53}

shows the two machine precision numbers differ by 1 bit and
consequently are considered equal by Equal as documented.

This type of issue is inherent in floating point arithmetic done
on any modern computer.

[toc] | [next] | [standalone]


#16163

FromRichard Fateman <fateman@cs.berkeley.edu>
Date2013-11-05 04:11 +0000
Message-ID<l59r5c$rd3$1@smc.vnet.net>
In reply to#16157
On 11/1/2013 11:21 PM, Bill Rowe wrote:

>
> shows the two machine precision numbers differ by 1 bit and
> consequently are considered equal by Equal as documented.
>
> This type of issue is inherent in floating point arithmetic done
> on any modern computer.
>
This is not at all true.  Mathematica chooses to implement a fuzzy
version of equality that does not correspond to any hardware
floating-point operation on any modern computer that I am aware of.


Mathematica's definition of Equal is a misfeature that leads to
many contradictions to ordinary arithmetic results.  This has
been pointed out numerous times and presumably will continue to
provoke questions indefinitely.

RJF



>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web