Strange Bug in R

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

Strange Bug in R

Neverstop
Hi all.
I don't understand why R works this way:
> rm(list=ls())
> require(foreign)
> dataset <- read.dta("http://www.ats.ucla.edu/stat/data/ologit.dta")
> min(dataset$gpa)
[1] 1.9
> min(dataset$gpa)>=1.90
[1] FALSE
> min(dataset$gpa)>=1.9
[1] FALSE
> min(dataset$gpa)>1.89
[1] TRUE
Shouldn't I get 3 TRUEs?
Am I missing something?
Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: Strange Bug in R

Rolf Turner
On 06/10/15 20:24, Neverstop wrote:

> Hi all.
> I don't understand why R works this way:
>> rm(list=ls())
>> require(foreign)
>> dataset <- read.dta("http://www.ats.ucla.edu/stat/data/ologit.dta")
>> min(dataset$gpa)
> [1] 1.9
>> min(dataset$gpa)>=1.90
> [1] FALSE
>> min(dataset$gpa)>=1.9
> [1] FALSE
>> min(dataset$gpa)>1.89
> [1] TRUE
> Shouldn't I get 3 TRUEs?

No.

> Am I missing something?

Comprehension of significant digits and the storage of floating point
numbers.

Try:

    print(min(dataset$gpa),digits=10)

Please don't refer to phenomena as "bugs" unless you are really sure
that they are not simply instances of things that you don't understand.
R was designed and written by very clever people and has been used,
tested and pushed to its limits by a wide variety of users for over 20
years.  It is highly improbable that you would stumble upon a real bug
in such a simple context.

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Strange Bug in R

Berend Hasselman
In reply to this post by Neverstop

> On 6 Oct 2015, at 09:24, Neverstop <[hidden email]> wrote:
>
> Hi all.
> I don't understand why R works this way:
>> rm(list=ls())
>> require(foreign)
>> dataset <- read.dta("http://www.ats.ucla.edu/stat/data/ologit.dta")
>> min(dataset$gpa)
> [1] 1.9
>> min(dataset$gpa)>=1.90
> [1] FALSE
>> min(dataset$gpa)>=1.9
> [1] FALSE
>> min(dataset$gpa)>1.89
> [1] TRUE
> Shouldn't I get 3 TRUEs?
> Am I missing something?
> Thank you.
>
>

See R FAQ 7.31  in https://cran.r-project.org/doc/FAQ/R-FAQ.html
It should provide clarification for your puzzlement.

Berend


>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Strange-Bug-in-R-tp4713175.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: Strange Bug in R

Rolf Turner
On 06/10/15 21:28, Berend Hasselman wrote:

>
>> On 6 Oct 2015, at 09:24, Neverstop <[hidden email]> wrote:
>>
>> Hi all.
>> I don't understand why R works this way:
>>> rm(list=ls())
>>> require(foreign)
>>> dataset <- read.dta("http://www.ats.ucla.edu/stat/data/ologit.dta")
>>> min(dataset$gpa)
>> [1] 1.9
>>> min(dataset$gpa)>=1.90
>> [1] FALSE
>>> min(dataset$gpa)>=1.9
>> [1] FALSE
>>> min(dataset$gpa)>1.89
>> [1] TRUE
>> Shouldn't I get 3 TRUEs?
>> Am I missing something?
>> Thank you.
>>
>>
>
> See R FAQ 7.31  in https://cran.r-project.org/doc/FAQ/R-FAQ.html
> It should provide clarification for your puzzlement.

Not really.  The problem is one of the precision to which a floating
point number is *printed* rather than one of the way that floating point
numbers are *calculated*.  Hence it is not an instance of the
counter-intuitive nature of floating point arithmetic.  I.e. you could
have numbers a and b that were calculated and stored to *infinite*
precision, appear to be equal when printed to some default number of
significant figures, but are not actually equal.

The problems are related and both involve having some understanding of
floating point numbers, but they are not the same problem.

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Strange Bug in R

Pascal Oettli-2
In reply to this post by Neverstop
Hello,

1) Please don't put rm(list=ls()) in a script you submit to this list.
This is considered as bad manner.

2) Please read https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
and http://stackoverflow.com/a/9508558/3710546

Regards,
Pascal

On Tue, Oct 6, 2015 at 4:24 PM, Neverstop <[hidden email]> wrote:

> Hi all.
> I don't understand why R works this way:
>> rm(list=ls())
>> require(foreign)
>> dataset <- read.dta("http://www.ats.ucla.edu/stat/data/ologit.dta")
>> min(dataset$gpa)
> [1] 1.9
>> min(dataset$gpa)>=1.90
> [1] FALSE
>> min(dataset$gpa)>=1.9
> [1] FALSE
>> min(dataset$gpa)>1.89
> [1] TRUE
> Shouldn't I get 3 TRUEs?
> Am I missing something?
> Thank you.
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Strange-Bug-in-R-tp4713175.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.



--
Pascal Oettli
Project Scientist
JAMSTEC
Yokohama, Japan

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Strange Bug in R

Neverstop
In reply to this post by Neverstop
Thank you all very much for the explanations!
Reply | Threaded
Open this post in threaded view
|

Re: Strange Bug in R

David Carlson
In reply to this post by Rolf Turner
There is a simple way to get closer to how a floating point number is stored in R with dput():

> dput(min(dataset$gpa))
1.89999997615814
> dput(dataset$gpa[290])
1.89999997615814

So you can see, the minimum is not 1.9, just very close to 1.9.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Rolf Turner
Sent: Tuesday, October 6, 2015 3:53 AM
To: Berend Hasselman
Cc: [hidden email]; Neverstop
Subject: Re: [R] Strange Bug in R

On 06/10/15 21:28, Berend Hasselman wrote:

>
>> On 6 Oct 2015, at 09:24, Neverstop <[hidden email]> wrote:
>>
>> Hi all.
>> I don't understand why R works this way:
>>> rm(list=ls())
>>> require(foreign)
>>> dataset <- read.dta("http://www.ats.ucla.edu/stat/data/ologit.dta")
>>> min(dataset$gpa)
>> [1] 1.9
>>> min(dataset$gpa)>=1.90
>> [1] FALSE
>>> min(dataset$gpa)>=1.9
>> [1] FALSE
>>> min(dataset$gpa)>1.89
>> [1] TRUE
>> Shouldn't I get 3 TRUEs?
>> Am I missing something?
>> Thank you.
>>
>>
>
> See R FAQ 7.31  in https://cran.r-project.org/doc/FAQ/R-FAQ.html
> It should provide clarification for your puzzlement.

Not really.  The problem is one of the precision to which a floating
point number is *printed* rather than one of the way that floating point
numbers are *calculated*.  Hence it is not an instance of the
counter-intuitive nature of floating point arithmetic.  I.e. you could
have numbers a and b that were calculated and stored to *infinite*
precision, appear to be equal when printed to some default number of
significant figures, but are not actually equal.

The problems are related and both involve having some understanding of
floating point numbers, but they are not the same problem.

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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.