Re: [Rd] Floating point issue

2022-07-19 Thread Olivier Benz
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 | 100010110010101000101110101010010001
bitC(1905969664)
#> [1] 0 10001010010 | 100010110010101000101110101010010010
bitC(1905969664 - 10^25)
#> [1] 0 100 | 
bitC(1e25)
#> [1] 0 10001010010 | 100010110010101000101110101010010001
```

Created on 2022-07-19 by the [reprex 
package](https://reprex.tidyverse.org) (v2.0.1)



Session info


``` 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
#>  ctypeUTF-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)
#>  digest0.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.152022-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)
#>  fs1.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.392022-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)
#>  pillar1.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)
#>  reprex2.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.142022-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)
#>  styler1.7.0   2022-03-13 [1] CRAN (R 4.2.0)
#>  tibble3.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.312022-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
#> 
#> 
──
```



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.ep

Re: [Rd] Floating point issue

2022-07-19 Thread Taras Zakharko
> So in this case, the different results are not due to the architecture (M1 
> aka ARM64) but to the system/implementation.

I would say that your results demonstrate that R number parsing code relies on 
higher-than double precision to perform correct number parsing. Extended 
precision is not guaranteed by the C standard, so I would classify this as a 
bug in R. Unless of course R doesn’t want to make any  number parsing 
guarantees, but that would be an odd choice to make :)

Cheers, 

Taras 

> On 19 Jul 2022, at 14:59, Olivier Benz  wrote:
> 
> I found that quite puzzling indeed. Thanks for the references.
> 
> So in this case, the different results are not due to the architecture (M1 
> aka ARM64) but to the system/implementation.
> 
>> On 19 Jul 2022, at 14:22, Taras Zakharko > > wrote:
>> 
>> 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  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 | 100010110010101000101110101010010001
>>> bitC(1905969664)
>>> #> [1] 0 10001010010 | 100010110010101000101110101010010010
>>> bitC(1905969664 - 10^25)
>>> #> [1] 0 100 | 
>>> bitC(1e25)
>>> #> [1] 0 10001010010 | 100010110010101000101110101010010001
>>> ```
>>> 
>>> Created on 2022-07-19 by the [reprex 
>>> package](https://reprex.tidyverse.org) (v2.0.1)
>>> 
>>> 
>>> 
>>> Session info
>>> 
>>> 
>>> ``` 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
>>> #>  ctypeUTF-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)
>>> #>  digest0.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.152022-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)
>>> #>  fs1.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.392022-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)
>>> #>  pillar1.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.