Simon Fear
2003-May-28 15:39 UTC
[R] Numbers that look equal, should be equal, but if() doesn'tsee as equal (repost with code included)
Try the following function (the name is supposed to be a joke, by the way), which will also do the right thing with NAs and characters. Use it as if(equal.enough(x,y)) rather than if(x==y), e.g.> equal.enough(0.1+0.2, 0.3)[1] TRUE My default of 15 significant figures may be overkill in many applications; be prepared to reduce this. Simon Fear "equal.enough" <- function(x, y, sig.figs=15, d.p.zero=-Inf) { # set argument d.p.zero to a real but small number (e.g. 1e-15) to overide # the sig.figs setting in favour of decimal places for comparison of extremely small numbers # (if appropriate - which is not always the case) if (is.numeric(x)) { if (is.numeric(y)) { if (is.infinite(d.p.zero)) ifelse(is.na(x), is.na(y), ifelse(is.na(y), F, abs(signif(x, sig.figs) - signif(y, sig.figs)) == 0)) else ifelse(is.na(x), is.na(y), ifelse(is.na(y), F, abs(signif(x, sig.figs) - signif(y, sig.figs)) <d.p.zero)) } else is.na(y) # a vector of F as long as y } else x==y } Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and contains information which may be legally privileged. It is intended for the stated addressee(s) only. Access to this email by anyone else is unauthorised. If you are not the intended addressee, any action taken (or not taken) in reliance on it, or any disclosure or copying of the contents of it is unauthorised and unlawful. If you are not the addressee, please inform the sender immediately and delete the email from your system. This message and any associated attachments have been checked for viruses using an internationally recognised virus detection process. However, Internet communications cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete. Therefore, we do not accept responsibility for any errors or omissions that are present in this message, or any attachment, that have arisen as a result of e-mail transmission. If verification is required, please request a hard-copy version. Any views or opinions presented are solely those of the author and do not necessarily represent those of Syne qua non.
Barry Rowlingson
2003-May-28 16:03 UTC
[R] Numbers that look equal, should be equal, but if() doesn'tsee as equal (repost with code included)
Simon Fear wrote:> Try the following function (the name is supposed to be a joke, by the > way), > which will also do the right thing with NAs and characters. Use it as > if(equal.enough(x,y)) rather than if(x==y), e.g. > > >>equal.enough(0.1+0.2, 0.3) >Oh, why not just go one step further, and redefine the == operator! "==" <- function(x,y){equal.enough(x,y)} before: > (.1+.2)==.3 [1] FALSE after: > (.1+.2)==.3 [1] TRUE This requires a slight modification to equal.enough, which I will not list here, so that people dont _actually_ do this. It just ensures that equal.enough doesn't go all infinitely recursive on us. Next time on useless R tips: 1 + 1 = 3 Barry
Simon Fear
2003-May-28 16:17 UTC
[R] Numbers that look equal, should be equal, but if() doesn'tsee as equal (repost with code included)
No problem, equal.enough(1+1,3,d.p.zero=10) ! But seriously, this is WHY NOT do that substitution: it helps me remember that I am not in fact truly testing for numeric identity. S -----Original Message----- From: Barry Rowlingson [mailto:B.Rowlingson at lancaster.ac.uk] Sent: 28 May 2003 17:03 To: Simon Fear Cc: r-help at stat.math.ethz.ch Subject: Re: [R] Numbers that look equal, should be equal, but if() doesn'tsee as equal (repost with code included) Next time on useless R tips: 1 + 1 = 3 Barry Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and contains information which may be legally privileged. It is intended for the stated addressee(s) only. Access to this email by anyone else is unauthorised. If you are not the intended addressee, any action taken (or not taken) in reliance on it, or any disclosure or copying of the contents of it is unauthorised and unlawful. If you are not the addressee, please inform the sender immediately and delete the email from your system. This message and any associated attachments have been checked for viruses using an internationally recognised virus detection process. However, Internet communications cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete. Therefore, we do not accept responsibility for any errors or omissions that are present in this message, or any attachment, that have arisen as a result of e-mail transmission. If verification is required, please request a hard-copy version. Any views or opinions presented are solely those of the author and do not necessarily represent those of Syne qua non.