[R] [External] replacing a value

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Wed Apr 6 14:44:57 CEST 2022


But if I type a==b, all I get is TRUE. Is this behavior somewhat system dependent?

a <- c(20, 20, 14.2375646029948, 19.9999999999999, 20, 20, 16.3092078677214, 20, 20, 20, 20, 20, 20, 20, 20, 14.8590932408795, 16.178935255298, 20, 20, 20, 20, 27.6404077886079, 20, 20, 20, 20, 20, 21.9857063037444, 20, 20, 20, 20)
b <- c(19.999999999999982, 19.999999999999982, 14.237564602994826,
      19.999999999999940, 19.999999999999982,
      19.999999999999982, 16.309207867721351, 19.999999999999982,
      19.999999999999982, 19.999999999999982,
      19.999999999999982, 19.999999999999982, 19.999999999999982,
      19.999999999999982, 19.999999999999982,
      14.859093240879485, 16.178935255298033, 19.999999999999982,
      19.999999999999982, 20.000000000000028,
      19.999999999999982, 27.640407788607870, 19.999999999999982,
      19.999999999999982, 19.999999999999982,
      19.999999999999982, 19.999999999999982, 21.985706303744429,
      19.999999999999982, 19.999999999999982,
      20.000000000000028, 19.999999999999982)
a==b


Tim

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of ani jaya
Sent: Wednesday, April 6, 2022 12:58 AM
To: Richard M. Heiberger <rmh using temple.edu>; Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
Cc: r-help <r-help using r-project.org>
Subject: Re: [R] [External] replacing a value

[External Email]

Dear All,

I will remain sipping the seawater until it changes its taste if this community does not exist.
Thank you.

> print(a,digit=18)
[1] 19.999999999999982 19.999999999999982 14.237564602994826
19.999999999999940 19.999999999999982
[6] 19.999999999999982 16.309207867721351 19.999999999999982
19.999999999999982 19.999999999999982
[11] 19.999999999999982 19.999999999999982 19.999999999999982
19.999999999999982 19.999999999999982
[16] 14.859093240879485 16.178935255298033 19.999999999999982
19.999999999999982 20.000000000000028
[21] 19.999999999999982 27.640407788607870 19.999999999999982
19.999999999999982 19.999999999999982
[26] 19.999999999999982 19.999999999999982 21.985706303744429
19.999999999999982 19.999999999999982
[31] 20.000000000000028 19.999999999999982


Best,
Ani

On Wed, Apr 6, 2022 at 1:47 PM Richard M. Heiberger <rmh using temple.edu> wrote:
>
> I believe you are not remembering that floating point numbers are not 
> exactly represented in decimal.
> See FAQ 7.31
>
> Here I have rounded a[4] to 12 places before comparing to 20.
> The you get the answer you are looking for.
>
> > a <- c(20, 20, 14.2375646029948, 19.9999999999999, 20, 20, 
> > 16.3092078677214,
> + 20, 20, 20, 20, 20, 20, 20, 20, 14.8590932408795, 16.178935255298, 
> + 20, 20, 20, 20, 27.6404077886079, 20, 20, 20, 20, 20, 
> + 21.9857063037444, 20, 20, 20, 20)
> > a[1:6]
> [1] 20.00000 20.00000 14.23756 20.00000 20.00000 20.00000
> > print(a[1:6], digits=17)
> [1] 20.000000000000000 20.000000000000000 14.237564602994800 
> 19.999999999999901 20.000000000000000 20.000000000000000
> > a[1:6] == 20
> [1]  TRUE  TRUE FALSE FALSE  TRUE  TRUE
> > a[1:6] - 20
> [1]  0.000000e+00  0.000000e+00 -5.762435e+00 -9.947598e-14  
> 0.000000e+00  0.000000e+00
> > a[4] == 20
> [1] FALSE
> > round(a[1:6], digits=14) - 20
> [1]  0.000000e+00  0.000000e+00 -5.762435e+00 -9.947598e-14  
> 0.000000e+00  0.000000e+00
> > round(a[1:6], digits=13) - 20
> [1]  0.000000e+00  0.000000e+00 -5.762435e+00 -9.947598e-14  
> 0.000000e+00  0.000000e+00
> > round(a[1:6], digits=12) - 20
> [1]  0.000000  0.000000 -5.762435  0.000000  0.000000  0.000000
> > b <- a[1:6]
> > b[round(b,digits=12) == 20] <- 0
> > b
> [1]  0.00000  0.00000 14.23756  0.00000  0.00000  0.00000
> >
>
> > On Apr 06, 2022, at 00:18, ani jaya <gaaauul using gmail.com> wrote:
> >
> >>> c(20, 20, 14.2375646029948, 19.9999999999999, 20, 20, 
> >>> 16.3092078677214, 20, 20, 20, 20, 20, 20, 20, 20, 
> >>> 14.8590932408795, 16.178935255298, 20, 20, 20, 20, 
> >>> 27.6404077886079, 20, 20, 20, 20, 20, 21.9857063037444, 20, 20, 
> >>> 20, 20)
>

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=A4GRP2o-ZyuqJvgr9XsPfuhujKPAAdoUZhMkJ0fR1UAVnUMRFNJmIaHS7V1aotNE&s=fuF4ytG9jvStgygMp00bmdHVcovgKvbojK3bSFVJ6-E&e=
PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=A4GRP2o-ZyuqJvgr9XsPfuhujKPAAdoUZhMkJ0fR1UAVnUMRFNJmIaHS7V1aotNE&s=y6027ELiLvmRy2xbH_A8sxKnEIsXNvUOwv7934md2tA&e=
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list