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 <massimodisasha@gmail.com>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]]
>>
>> ______________________________**________________
>> R-help@r-project.org 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
>
> >
>
> ______________________________**________________
> R-help@r-project.org 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]]

