Re: [R] numeric equality

From: David M Smith <david_at_revolution-computing.com>
Date: Wed, 18 Mar 2009 09:54:30 -0700

On Wed, Mar 18, 2009 at 8:58 AM, Yu, Changhong <YuC_at_ccf.org> wrote:
> Dear all,
> I am totally confused by the following R output, but don't have a clue
> for it.
>
>> a <- 1 - 0.2
>
>> a == 0.8
>
> [1] TRUE
>
>> a <- 1 - 0.8
>
>> a == 0.2
>
> [1] FALSE
This is expected behaviour. The bottom line is that you shouldn't rely on the equality operator when dealing with floating point values in any numerical software, including R.

I write about this in detail at:

http://blog.revolution-computing.com/2009/03/when-is-a-zero-not-a-zero.html

which will hopefully clear up your questions.

# David Smith

-- 
David M Smith <david_at_revolution-computing.com>
Director of Community, REvolution Computing www.revolution-computing.com
Tel: +1 (206) 577-4778 x3203 (Seattle, USA)

Check out our upcoming events schedule at www.revolution-computing.com/events

______________________________________________
R-help_at_r-project.org 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.
Received on Wed 18 Mar 2009 - 16:01:23 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 18 Mar 2009 - 21:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive