[R] [External] replacing a value

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Apr 6 19:20:48 CEST 2022


Hello,

The problem seems to be solved, btu maybe my answer to this thread [1] 
can be of help.

Using Bill's dput'ed data


equals <- function(x, y, tol = .Machine$double.eps^0.5) abs(x - y) < tol

x <- c(0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.4p+4,
        0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.3ffffffffffffp+4, 
0x1.3ffffffffffffp+4,
        0x1.3ffffffffffffp+4, 0x1.3ffffffffffffp+4)

x == 20
#>  [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE 
FALSE FALSE
#> [13] FALSE
equals(x, 20)
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE



[1] https://stat.ethz.ch/pipermail/r-help/2012-September/322938.html


Hope this helps,

Rui Barradas

Às 05:58 de 06/04/2022, ani jaya escreveu:
> 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://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.



More information about the R-help mailing list