-1.1 - 0.1 + 1.2 is NOT null! Why?

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

-1.1 - 0.1 + 1.2 is NOT null! Why?

ollestrat
Hello, I fear its a stupid question,..but here it is: If I do this simple calculation with the R console, I surprisingly do not get a zero. Why? -1.1-0.1+1.2 [1] -2.220446e-16 greetings, Ole
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

Richard M. Heiberger
This is the behavior of the floating point number representation.
Decimal fractions do not come out even in binary notation.
Please see FAQ 7.31

On Tue, Jul 10, 2012 at 4:17 PM, ollestrat <[hidden email]> wrote:

> Hello,
>
> I fear its a stupid question,..but here it is:
>
> If I do this simple calculation with the R  console, I surprisingly do not
> get a zero. Why?
>
>  -1.1-0.1+1.2
> [1] -2.220446e-16
>
>
> greetings, Ole
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/1-1-0-1-1-2-is-NOT-null-Why-tp4636053.html
> Sent from the R help mailing list archive at Nabble.com.
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

William Dunlap
In reply to this post by ollestrat
It is for the same reason that if you must work with numbers
stored with 2 significant decimal digits 1-(1/3+1/3+1/3)
is 0.01 (== 10 ^ -2).

Double precision numbers, supported by your computer
hardware and used by R, are stored using 52 significant
binary digits and 2^-52 is about -2.220446e-16.

(By the way, in R zero and NULL are different things: the former
is numeric and the latter is not.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of ollestrat
> Sent: Tuesday, July 10, 2012 1:17 PM
> To: [hidden email]
> Subject: [R] -1.1 - 0.1 + 1.2 is NOT null! Why?
>
> Hello,
>
> I fear its a stupid question,..but here it is:
>
> If I do this simple calculation with the R  console, I surprisingly do not
> get a zero. Why?
>
>  -1.1-0.1+1.2
> [1] -2.220446e-16
>
>
> greetings, Ole
>
> --
> View this message in context: http://r.789695.n4.nabble.com/1-1-0-1-1-2-is-NOT-
> null-Why-tp4636053.html
> Sent from the R help mailing list archive at Nabble.com.
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

Rolf Turner-3
In reply to this post by ollestrat


In addition to taking cognisance of Richard Heiberger's reply you
should also learn to distinguish between the concept of "null" and
"zero".  They are not at all the same thing.

     cheers,

         Rolf Turner

On 11/07/12 08:17, ollestrat wrote:

> Hello,
>
> I fear its a stupid question,..but here it is:
>
> If I do this simple calculation with the R  console, I surprisingly do not
> get a zero. Why?
>
>   -1.1-0.1+1.2
> [1] -2.220446e-16
>
>
> greetings, Ole

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

Erdal Karaca-2
german "Null" == english "zero" :-)

2012/7/10 Rolf Turner <[hidden email]>

>
>
> In addition to taking cognisance of Richard Heiberger's reply you
> should also learn to distinguish between the concept of "null" and
> "zero".  They are not at all the same thing.
>
>     cheers,
>
>         Rolf Turner
>
>
> On 11/07/12 08:17, ollestrat wrote:
>
>> Hello,
>>
>> I fear its a stupid question,..but here it is:
>>
>> If I do this simple calculation with the R  console, I surprisingly do not
>> get a zero. Why?
>>
>>   -1.1-0.1+1.2
>> [1] -2.220446e-16
>>
>>
>> greetings, Ole
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

Rolf Turner-3

But R-ish NULL is *NOT* equal to R-ish zero, and that's
what counts here.

     cheers,

         Rolf Turner

On 11/07/12 09:19, Erdal Karaca wrote:

> german "Null" == english "zero" :-)
>
> 2012/7/10 Rolf Turner <[hidden email]
> <mailto:[hidden email]>>
>
>
>
>     In addition to taking cognisance of Richard Heiberger's reply you
>     should also learn to distinguish between the concept of "null" and
>     "zero".  They are not at all the same thing.
>
>         cheers,
>
>             Rolf Turner
>
>
>     On 11/07/12 08:17, ollestrat wrote:
>
>         Hello,
>
>         I fear its a stupid question,..but here it is:
>
>         If I do this simple calculation with the R  console, I
>         surprisingly do not
>         get a zero. Why?
>
>           -1.1-0.1+1.2
>         [1] -2.220446e-16
>
>
>         greetings, Ole
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

Massimo Di Stefano-2
In reply to this post by ollestrat
Il 7/10/12 4:17 PM, ollestrat ha scritto:

> Hello,
>
> I fear its a stupid question,..but here it is:
>
> If I do this simple calculation with the R  console, I surprisingly do not
> get a zero. Why?
>
>   -1.1-0.1+1.2
> [1] -2.220446e-16
>
>
> greetings, Ole
>
> --
> View this message in context: http://r.789695.n4.nabble.com/1-1-0-1-1-2-is-NOT-null-Why-tp4636053.html
> Sent from the R help mailing list archive at Nabble.com.
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
...

Python :
 >>> -1.1-0.1+1.2
-2.220446049250313e-16
 >>> -1.2-0.2+1.4
0.0
 >>>

R :
 > -1.1-0.1+1.2
[1] -2.220446e-16
 > -1.2-0.2+1.4
[1] 0
 >

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

arun kirshna
Hi,

Perl:
perl -e 'print -1.1-0.1+1.2. "\n";'
-2.22044604925031e-16
perl -e 'print -1.2-0.2+1.4. "\n";'
0


A.K.




----- Original Message -----
From: massimodisasha <[hidden email]>
To: ollestrat <[hidden email]>; [hidden email]
Cc:
Sent: Tuesday, July 10, 2012 6:23 PM
Subject: Re: [R] -1.1 - 0.1 + 1.2 is NOT null! Why?

Il 7/10/12 4:17 PM, ollestrat ha scritto:

> Hello,
>
> I fear its a stupid question,..but here it is:
>
> If I do this simple calculation with the R  console, I surprisingly do not
> get a zero. Why?
>
>   -1.1-0.1+1.2
> [1] -2.220446e-16
>
>
> greetings, Ole
>
> --
> View this message in context: http://r.789695.n4.nabble.com/1-1-0-1-1-2-is-NOT-null-Why-tp4636053.html
> Sent from the R help mailing list archive at Nabble.com.
>     [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
...

Python :
>>> -1.1-0.1+1.2
-2.220446049250313e-16
>>> -1.2-0.2+1.4
0.0
>>>

R :
> -1.1-0.1+1.2
[1] -2.220446e-16
> -1.2-0.2+1.4
[1] 0
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

Richard M. Heiberger
In reply to this post by Massimo Di Stefano-2
This is a fascinating example.  It is an accident that it looks like it
comes out even.

Here are both sets displayed using hex format where we see exactly what
bits are used
in the internal representation of the numbers.

> dphex <- function(x) {sprintf("%+.13a", x)}

> ones <- c(-1.1,
+           -0.1,
+           1.2,
+           -1.1-0.1+1.2)
>
> twos <- c(-1.2,
+           -0.2,
+           1.4,
+           -1.2-0.2+1.4)
>
> cbind(ones, dphex(ones))
     ones
[1,] "-1.1"                  "-0x1.199999999999ap+0"
[2,] "-0.1"                  "-0x1.999999999999ap-4"
[3,] "1.2"                   "+0x1.3333333333333p+0"
[4,] "-2.22044604925031e-16" "-0x1.0000000000000p-52"
>
> cbind(twos, dphex(twos))
     twos
[1,] "-1.2" "-0x1.3333333333333p+0"
[2,] "-0.2" "-0x1.999999999999ap-3"
[3,] "1.4"  "+0x1.6666666666666p+0"
[4,] "0"    "+0x0.0000000000000p+0"
>



dphex <- function(x) {sprintf("%+.13a", x)}
ones <- c(-1.1,
          -0.1,
          1.2,
          -1.1-0.1+1.2)

twos <- c(-1.2,
          -0.2,
          1.4,
          -1.2-0.2+1.4)

cbind(ones, dphex(ones))

cbind(twos, dphex(twos))



As you can see, both sets of numbers are repeating hexadecimals.  When we
run out
of digits at the end at the 53 bit (13th hex digit after the hex point) the
repeat pattern is
broken.  The '9' is rounded up to 'a'.  It is an accident that the rounding
of the twos values
cancels to give an apparent sum of 0.


On Tue, Jul 10, 2012 at 6:23 PM, massimodisasha <[hidden email]>wrote:

> Il 7/10/12 4:17 PM, ollestrat ha scritto:
>
>  Hello,
>>
>> I fear its a stupid question,..but here it is:
>>
>> If I do this simple calculation with the R  console, I surprisingly do not
>> get a zero. Why?
>>
>>   -1.1-0.1+1.2
>> [1] -2.220446e-16
>>
>>
>> greetings, Ole
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/**
>> 1-1-0-1-1-2-is-NOT-null-Why-**tp4636053.html<http://r.789695.n4.nabble.com/1-1-0-1-1-2-is-NOT-null-Why-tp4636053.html>
>> Sent from the R help mailing list archive at Nabble.com.
>>         [[alternative HTML version deleted]]
>>
>> ______________________________**________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
> ...
>
> Python :
> >>> -1.1-0.1+1.2
> -2.220446049250313e-16
> >>> -1.2-0.2+1.4
> 0.0
> >>>
>
> R :
> > -1.1-0.1+1.2
> [1] -2.220446e-16
> > -1.2-0.2+1.4
> [1] 0
>
> >
>
> ______________________________**________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide http://www.R-project.org/**
> posting-guide.html <http://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

Patrick Connolly-4
In reply to this post by Erdal Karaca-2
On Tue, 10-Jul-2012 at 11:19PM +0200, Erdal Karaca wrote:

|> german "Null" == english "zero" :-)

German "Gift" == English "poison"  :-(







|>
|> 2012/7/10 Rolf Turner <[hidden email]>
|>
|> >
|> >
|> > In addition to taking cognisance of Richard Heiberger's reply you
|> > should also learn to distinguish between the concept of "null" and
|> > "zero".  They are not at all the same thing.
|> >
|> >     cheers,
|> >
|> >         Rolf Turner
|> >
|> >
|> > On 11/07/12 08:17, ollestrat wrote:
|> >
|> >> Hello,
|> >>
|> >> I fear its a stupid question,..but here it is:
|> >>
|> >> If I do this simple calculation with the R  console, I surprisingly do not
|> >> get a zero. Why?
|> >>
|> >>   -1.1-0.1+1.2
|> >> [1] -2.220446e-16
|> >>
|> >>
|> >> greetings, Ole
|> >>
|> >
|> > ______________________________________________
|> > [hidden email] mailing list
|> > https://stat.ethz.ch/mailman/listinfo/r-help
|> > PLEASE do read the posting guide
|> > http://www.R-project.org/posting-guide.html
|> > and provide commented, minimal, self-contained, reproducible code.
|> >
|>
|> [[alternative HTML version deleted]]
|>
|> ______________________________________________
|> [hidden email] mailing list
|> https://stat.ethz.ch/mailman/listinfo/r-help
|> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
|> and provide commented, minimal, self-contained, reproducible code.

--
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.  
   ___    Patrick Connolly  
 {~._.~}                   Great minds discuss ideas    
 _( Y )_           Average minds discuss events
(:_~*~_:)                  Small minds discuss people  
 (_)-(_)                        ..... Eleanor Roosevelt
         
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

Erdal Karaca-2
I was just trying to say that Ole might know the technical difference
between "NULL" and "ZERO", but it seemed to be rather a linguistic
confusion...
BTW. the subject contains "null", but the body of the mail correctly states
"zero" :-)


2012/7/12 Patrick Connolly <[hidden email]>

> On Tue, 10-Jul-2012 at 11:19PM +0200, Erdal Karaca wrote:
>
> |> german "Null" == english "zero" :-)
>
> German "Gift" == English "poison"  :-(
>
>
>
>
>
>
>
> |>
> |> 2012/7/10 Rolf Turner <[hidden email]>
> |>
> |> >
> |> >
> |> > In addition to taking cognisance of Richard Heiberger's reply you
> |> > should also learn to distinguish between the concept of "null" and
> |> > "zero".  They are not at all the same thing.
> |> >
> |> >     cheers,
> |> >
> |> >         Rolf Turner
> |> >
> |> >
> |> > On 11/07/12 08:17, ollestrat wrote:
> |> >
> |> >> Hello,
> |> >>
> |> >> I fear its a stupid question,..but here it is:
> |> >>
> |> >> If I do this simple calculation with the R  console, I surprisingly
> do not
> |> >> get a zero. Why?
> |> >>
> |> >>   -1.1-0.1+1.2
> |> >> [1] -2.220446e-16
> |> >>
> |> >>
> |> >> greetings, Ole
> |> >>
> |> >
> |> > ______________________________________________
> |> > [hidden email] mailing list
> |> > https://stat.ethz.ch/mailman/listinfo/r-help
> |> > PLEASE do read the posting guide
> |> > http://www.R-project.org/posting-guide.html
> |> > and provide commented, minimal, self-contained, reproducible code.
> |> >
> |>
> |>      [[alternative HTML version deleted]]
> |>
> |> ______________________________________________
> |> [hidden email] mailing list
> |> https://stat.ethz.ch/mailman/listinfo/r-help
> |> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> |> and provide commented, minimal, self-contained, reproducible code.
>
> --
> ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
>    ___    Patrick Connolly
>  {~._.~}                   Great minds discuss ideas
>  _( Y )_                 Average minds discuss events
> (:_~*~_:)                  Small minds discuss people
>  (_)-(_)                              ..... Eleanor Roosevelt
>
> ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

ollestrat
Thank you for the explanation. Good to know about the issue how double values are "constructed" by a bit system. This makes me handling double values with care in using it in R or aother languages control structures etc.

Thank you also for the hint concerning the Null vs. Zero vs.. issue. Yes, the subject title is misleading..
Reply | Threaded
Open this post in threaded view
|

Re: -1.1 - 0.1 + 1.2 is NOT null! Why?

ollestrat
In reply to this post by ollestrat
Someone pointed me to this paper:

http://www.validlab.com/goldberg/paper.pdf