[Rd] Floating point issue

Taras Zakharko t@r@@@z@kh@rko @end|ng |rom uzh@ch
Tue Jul 19 14:22:36 CEST 2022


Just a quick note of this in case someone finds Olivier's results puzzling. Standard ARM64 ABI (which Linux follows) defines long double as a 16-byte extended precision type. LLVM’s compiler-rt has a software implementation for doing operations on such types. Apple instead defines long double as identical to double in their ABIs. 

References

https://developer.arm.com/documentation/ka004751/1-0

https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms




> On 19 Jul 2022, at 13:04, Olivier Benz <olivier.benz using b-data.ch> wrote:
> 
> The difference between macOS 12.4 and Debian 11 (Docker, virtualization framework) running on a MacBook Pro (M1 Max).
> -> `.Machine$sizeof.longdouble` on macOS returns 8 whereas on Debian 11 it returns 16.
> 
> macOS 12.4 on MacBook Pro (M1 Max):
> ``` r
> .Machine
> #> $double.eps
> #> [1] 2.220446e-16
> #> 
> #> $double.neg.eps
> #> [1] 1.110223e-16
> #> 
> #> $double.xmin
> #> [1] 2.225074e-308
> #> 
> #> $double.xmax
> #> [1] 1.797693e+308
> #> 
> #> $double.base
> #> [1] 2
> #> 
> #> $double.digits
> #> [1] 53
> #> 
> #> $double.rounding
> #> [1] 5
> #> 
> #> $double.guard
> #> [1] 0
> #> 
> #> $double.ulp.digits
> #> [1] -52
> #> 
> #> $double.neg.ulp.digits
> #> [1] -53
> #> 
> #> $double.exponent
> #> [1] 11
> #> 
> #> $double.min.exp
> #> [1] -1022
> #> 
> #> $double.max.exp
> #> [1] 1024
> #> 
> #> $integer.max
> #> [1] 2147483647
> #> 
> #> $sizeof.long
> #> [1] 8
> #> 
> #> $sizeof.longlong
> #> [1] 8
> #> 
> #> $sizeof.longdouble
> #> [1] 8
> #> 
> #> $sizeof.pointer
> #> [1] 8
> 
> bitC <- function(x) noquote(vapply(as.double(x), function(x) { # split one
> double
>  b <- substr(as.character(rev(numToBits(x))), 2L, 2L)
>      paste0(c(b[1L], " ", b[2:12], " | ", b[13:64]), collapse = "")
>    }, ""))
> bitC(10^25)
> #> [1] 0 10001010010 | 0000100010110010101000101100001010000000001010010001
> bitC(10000000000000000905969664)
> #> [1] 0 10001010010 | 0000100010110010101000101100001010000000001010010010
> bitC(10000000000000000905969664 - 10^25)
> #> [1] 0 10000011110 | 0000000000000000000000000000000000000000000000000000
> bitC(1e25)
> #> [1] 0 10001010010 | 0000100010110010101000101100001010000000001010010001
> ```
> 
> <sup>Created on 2022-07-19 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1)</sup>
> 
> <details style="margin-bottom:10px;">
> <summary>
> Session info
> </summary>
> 
> ``` r
> sessioninfo::session_info()
> #> ─ Session info ───────────────────────────────────────────────────────────────
> #>  setting  value
> #>  version  R version 4.2.1 (2022-06-23)
> #>  os       macOS Monterey 12.4
> #>  system   aarch64, darwin20
> #>  ui       X11
> #>  language (EN)
> #>  collate  en_US.UTF-8
> #>  ctype    UTF-8
> #>  tz       Europe/Copenhagen
> #>  date     2022-07-19
> #>  pandoc   2.18 @ /opt/local/bin/ (via rmarkdown)
> #> 
> #> ─ Packages ───────────────────────────────────────────────────────────────────
> #>  package     * version date (UTC) lib source
> #>  cli           3.3.0   2022-04-25 [1] CRAN (R 4.2.0)
> #>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.2.0)
> #>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.2.0)
> #>  evaluate      0.15    2022-02-18 [1] CRAN (R 4.2.0)
> #>  fansi         1.0.3   2022-03-24 [1] CRAN (R 4.2.0)
> #>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.2.0)
> #>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.2.0)
> #>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
> #>  highr         0.9     2021-04-16 [1] CRAN (R 4.2.0)
> #>  htmltools     0.5.3   2022-07-18 [1] CRAN (R 4.2.1)
> #>  knitr         1.39    2022-04-26 [1] CRAN (R 4.2.0)
> #>  lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.2.0)
> #>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
> #>  pillar        1.8.0   2022-07-18 [1] CRAN (R 4.2.1)
> #>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.0)
> #>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.2.0)
> #>  R.cache       0.15.0  2021-04-30 [1] CRAN (R 4.2.0)
> #>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.2.0)
> #>  R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.2.0)
> #>  R.utils       2.12.0  2022-06-28 [1] CRAN (R 4.2.0)
> #>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.2.0)
> #>  rlang         1.0.4   2022-07-12 [1] CRAN (R 4.2.1)
> #>  rmarkdown     2.14    2022-04-25 [1] CRAN (R 4.2.0)
> #>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
> #>  stringi       1.7.8   2022-07-11 [1] CRAN (R 4.2.1)
> #>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.2.0)
> #>  styler        1.7.0   2022-03-13 [1] CRAN (R 4.2.0)
> #>  tibble        3.1.7   2022-05-03 [1] CRAN (R 4.2.0)
> #>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.2.0)
> #>  vctrs         0.4.1   2022-04-13 [1] CRAN (R 4.2.0)
> #>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
> #>  xfun          0.31    2022-05-10 [1] CRAN (R 4.2.0)
> #>  yaml          2.3.5   2022-02-21 [1] CRAN (R 4.2.0)
> #> 
> #>  [1] /Users/benz0li/Library/R/arm64/4.2/library
> #>  [2] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library
> #> 
> #> ──────────────────────────────────────────────────────────────────────────────
> ```
> 
> </details>
> 
> Debian 11 (Docker, virtualization framework) on MacBook Pro (M1 Max)
> ``` r
> .Machine
> #> $double.eps
> #> [1] 2.220446e-16
> #> 
> #> $double.neg.eps
> #> [1] 1.110223e-16
> #> 
> #> $double.xmin
> #> [1] 2.225074e-308
> #> 
> #> $double.xmax
> #> [1] 1.797693e+308
> #> 
> #> $double.base
> #> [1] 2
> #> 
> #> $double.digits
> #> [1] 53
> #> 
> #> $double.rounding
> #> [1] 5
> #> 
> #> $double.guard
> #> [1] 0
> #> 
> #> $double.ulp.digits
> #> [1] -52
> #> 
> #> $double.neg.ulp.digits
> #> [1] -53
> #> 
> #> $double.exponent
> #> [1] 11
> #> 
> #> $double.min.exp
> #> [1] -1022
> #> 
> #> $double.max.exp
> #> [1] 1024
> #> 
> #> $integer.max
> #> [1] 2147483647
> #> 
> #> $sizeof.long
> #> [1] 8
> #> 
> #> $sizeof.longlong
> #> [1] 8
> #> 
> #> $sizeof.longdouble
> #> [1] 16
> #> 
> #> $sizeof.pointer
> #> [1] 8
> #> 
> #> $longdouble.eps
> #> [1] 1.92593e-34
> #> 
> #> $longdouble.neg.eps
> #> [1] 9.62965e-35
> #> 
> #> $longdouble.digits
> #> [1] 113
> #> 
> #> $longdouble.rounding
> #> [1] 5
> #> 
> #> $longdouble.guard
> #> [1] 0
> #> 
> #> $longdouble.ulp.digits
> #> [1] -112
> #> 
> #> $longdouble.neg.ulp.digits
> #> [1] -113
> #> 
> #> $longdouble.exponent
> #> [1] 15
> #> 
> #> $longdouble.min.exp
> #> [1] -16382
> #> 
> #> $longdouble.max.exp
> #> [1] 16384
> 
> bitC <- function(x) noquote(vapply(as.double(x), function(x) { # split one
> double
>  b <- substr(as.character(rev(numToBits(x))), 2L, 2L)
>      paste0(c(b[1L], " ", b[2:12], " | ", b[13:64]), collapse = "")
>    }, ""))
> bitC(10^25)
> #> [1] 0 10001010010 | 0000100010110010101000101100001010000000001010010001
> bitC(10000000000000000905969664)
> #> [1] 0 10001010010 | 0000100010110010101000101100001010000000001010010001
> bitC(10000000000000000905969664 - 10^25)
> #> [1] 0 00000000000 | 0000000000000000000000000000000000000000000000000000
> bitC(1e25)
> #> [1] 0 10001010010 | 0000100010110010101000101100001010000000001010010001
> ```
> 
> <sup>Created on 2022-07-19 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1)</sup>
> 
> <details style="margin-bottom:10px;">
> <summary>
> Session info
> </summary>
> 
> ``` r
> sessioninfo::session_info()
> #> ─ Session info ───────────────────────────────────────────────────────────────
> #>  setting  value
> #>  version  R version 4.2.1 (2022-06-23)
> #>  os       Debian GNU/Linux 11 (bullseye)
> #>  system   aarch64, linux-gnu
> #>  ui       X11
> #>  language (EN)
> #>  collate  en_US.UTF-8
> #>  ctype    en_US.UTF-8
> #>  tz       Etc/UTC
> #>  date     2022-07-19
> #>  pandoc   2.18 @ /usr/bin/ (via rmarkdown)
> #> 
> #> ─ Packages ───────────────────────────────────────────────────────────────────
> #>  package     * version date (UTC) lib source
> #>  cli           3.3.0   2022-04-25 [2] CRAN (R 4.2.1)
> #>  crayon        1.5.1   2022-03-26 [2] CRAN (R 4.2.1)
> #>  digest        0.6.29  2021-12-01 [2] CRAN (R 4.2.1)
> #>  ellipsis      0.3.2   2021-04-29 [2] CRAN (R 4.2.1)
> #>  evaluate      0.15    2022-02-18 [2] CRAN (R 4.2.1)
> #>  fansi         1.0.3   2022-03-24 [2] CRAN (R 4.2.1)
> #>  fastmap       1.1.0   2021-01-25 [2] CRAN (R 4.2.1)
> #>  fs            1.5.2   2021-12-08 [2] CRAN (R 4.2.1)
> #>  glue          1.6.2   2022-02-24 [2] CRAN (R 4.2.1)
> #>  highr         0.9     2021-04-16 [2] CRAN (R 4.2.1)
> #>  htmltools     0.5.2   2021-08-25 [2] CRAN (R 4.2.1)
> #>  knitr         1.39    2022-04-26 [2] CRAN (R 4.2.1)
> #>  lifecycle     1.0.1   2021-09-24 [2] CRAN (R 4.2.1)
> #>  magrittr      2.0.3   2022-03-30 [2] CRAN (R 4.2.1)
> #>  pillar        1.7.0   2022-02-01 [2] CRAN (R 4.2.1)
> #>  pkgconfig     2.0.3   2019-09-22 [2] CRAN (R 4.2.1)
> #>  purrr         0.3.4   2020-04-17 [2] CRAN (R 4.2.1)
> #>  R.cache       0.15.0  2021-04-30 [2] CRAN (R 4.2.1)
> #>  R.methodsS3   1.8.2   2022-06-13 [2] CRAN (R 4.2.1)
> #>  R.oo          1.25.0  2022-06-12 [2] CRAN (R 4.2.1)
> #>  R.utils       2.12.0  2022-06-28 [2] CRAN (R 4.2.1)
> #>  reprex        2.0.1   2021-08-05 [2] CRAN (R 4.2.1)
> #>  rlang         1.0.4   2022-07-12 [2] CRAN (R 4.2.1)
> #>  rmarkdown     2.14    2022-04-25 [2] CRAN (R 4.2.1)
> #>  sessioninfo   1.2.2   2021-12-06 [2] CRAN (R 4.2.1)
> #>  stringi       1.7.8   2022-07-11 [2] CRAN (R 4.2.1)
> #>  stringr       1.4.0   2019-02-10 [2] CRAN (R 4.2.1)
> #>  styler        1.7.0   2022-03-13 [2] CRAN (R 4.2.1)
> #>  tibble        3.1.7   2022-05-03 [2] CRAN (R 4.2.1)
> #>  utf8          1.2.2   2021-07-24 [2] CRAN (R 4.2.1)
> #>  vctrs         0.4.1   2022-04-13 [2] CRAN (R 4.2.1)
> #>  withr         2.5.0   2022-03-03 [2] CRAN (R 4.2.1)
> #>  xfun          0.31    2022-05-10 [2] CRAN (R 4.2.1)
> #>  yaml          2.3.5   2022-02-21 [2] CRAN (R 4.2.1)
> #> 
> #>  [1] /home/benz0li/R/aarch64-unknown-linux-gnu-library/4.2
> #>  [2] /usr/local/lib/R/site-library
> #>  [3] /usr/local/lib/R/library
> #> 
> #> ──────────────────────────────────────────────────────────────────────────────
> ```
> 
> </details>
> 
>> On 10 Jul 2022, at 22:23, Bill Dunlap <williamwdunlap using gmail.com> wrote:
>> 
>> The following function, 'bitC' from ?numToBits, displays the bits in a
>> double precision number, separated into the sign bit, the 11 exponent bits,
>> and the 52 bits in the mantissa.  I've shown the results with your numbers
>> from R-2.4.0 on my Windows 11 Lenovo laptop: what do you get?
>> 
>>> bitC <- function(x) noquote(vapply(as.double(x), function(x) { # split
>> one double
>> +     b <- substr(as.character(rev(numToBits(x))), 2L, 2L)
>> +     paste0(c(b[1L], " ", b[2:12], " | ", b[13:64]), collapse = "")
>> +   }, ""))
>>> bitC(10^25)
>> # [1] 0 10001010010 | 0000100010110010101000101100001010000000001010010001
>>> bitC(10000000000000000905969664)
>> # [1] 0 10001010010 | 0000100010110010101000101100001010000000001010010001
>>> bitC(10000000000000000905969664 - 10^25)
>> # [1] 0 00000000000 | 0000000000000000000000000000000000000000000000000000
>>> bitC(1e25)
>> # [1] 0 10001010010 | 0000100010110010101000101100001010000000001010010001
>> 
>> -Bill
>> 
>> On Sun, Jul 10, 2022 at 7:00 AM Antoine Fabri <antoine.fabri using gmail.com>
>> wrote:
>> 
>>> Dear r-devel,
>>> 
>>> For some numbers, the printed value is not equivalent to the input :
>>> 
>>> options(scipen = 999)
>>> ## GOOD
>>> 1e24
>>> #> [1]  999999999999999983222784
>>> 1e24 == 999999999999999983222784
>>> #> [1] TRUE
>>> 
>>> ## BAD
>>> 1e25
>>> #> [1] 10000000000000000905969664
>>> 1e25 == 10000000000000000905969664
>>> #> [1] FALSE
>>> 
>>> ## STILL BAD
>>> 10000000000000000905969664
>>> #> [1] 10000000000000003053453312
>>> 
>>> ## GOOD AGAIN
>>> 10000000000000003053453312
>>> #> [1] 10000000000000003053453312
>>> 
>>> # Additionally
>>> 10000000000000000000000000 == 1e25
>>> #> [1] FALSE
>>> 
>>> Are these bugs ?
>>> 
>>>       [[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> R-devel using r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>> 
>> 
>> 	[[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-devel using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> --
> benz0li.b-data.io | @benz0li | olivier.benz using b-data.ch
> 
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


	[[alternative HTML version deleted]]



More information about the R-devel mailing list