Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #16328 > unrolled thread
| Started by | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| First post | 2013-12-06 07:11 +0000 |
| Last post | 2013-12-08 09:35 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Re: gives 919.8359999999999?` Bill Rowe <readnews@sbcglobal.net> - 2013-12-06 07:11 +0000
Re: gives 919.8359999999999?` Richard Fateman <fateman@cs.berkeley.edu> - 2013-12-08 09:35 +0000
| From | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| Date | 2013-12-06 07:11 +0000 |
| Subject | Re: gives 919.8359999999999?` |
| Message-ID | <l7rtbp$o5b$1@smc.vnet.net> |
On 12/5/13 at 3:05 AM, guillermo.sanchez@hotmail.com (Guillermo Sanchez) wrote: >FullForm[916.536 + 3.3] gives 919.8359999999999 instead of 919.836? Whenever you use machine precision numbers the following sequence of events takes place. First, your input is converted to a binary value. Then the required operation is done. Finally, the result is converted to decimal for display. Neither of the two values you provided above can be represented precisely in a finite number of binary digits. So, the binary values used to represent your input are approximations to your input. They differ just slightly from the decimal values you entered. And that small difference accumulates every time you do an operation to combine values. So, the binary result converts to a decimal just a bit more different than what it would be if you were doing exact arithmetic. And this is characteristic is inherent in any modern computer. If you want to avoid this characteristic in Mathematica, simply use exact arithmetic. That is In[1]:= 916536/1000 + 33/10 Out[1]= 229959/250 In[2]:= N[%] Out[2]= 919.836 In[3]:= FullForm[%] Out[3]//FullForm= 919.836`
[toc] | [next] | [standalone]
| From | Richard Fateman <fateman@cs.berkeley.edu> |
|---|---|
| Date | 2013-12-08 09:35 +0000 |
| Message-ID | <l81ehe$8tc$1@smc.vnet.net> |
| In reply to | #16328 |
On 12/5/2013 11:11 PM, Bill Rowe wrote:
..... And this is characteristic is
> inherent in any modern computer.
At least when using double-precision binary floating-point arithmetic.
Which is what Mathematica happens to be using here. It doesn't always.
See below.
>
> If you want to avoid this characteristic in Mathematica, simply
> use exact arithmetic.
That may seem simple, but can lead to exponentially expensive arithmetic.
Compare Timing[Sum[1/n,{n,1,100000];]
to
Timing[Sum[1.0/n,{n,1,100000];]
As for your particular example, you could try this:
FullForm[916.53600000000000000 + 3.300000000000000000000]
which gets all the digits you expect.
Even better you (and other readers of this note) could read
David Goldberg, "What Every Computer Scientist Should Know About
Floating-Point Arithmetic".
That is
>
> In[1]:= 916536/1000 + 33/10
>
> Out[1]= 229959/250
>
> In[2]:= N[%]
>
> Out[2]= 919.836
>
> In[3]:= FullForm[%]
>
> Out[3]//FullForm= 919.836`
>
>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web