[R] Number changed weirdly when converting to numeric

Ben Bolker
2025-03-09T17:44:48Z

I can't reproduce this locally.


sprintf("%0.15f", as.numeric("-177253333.333333343267441"))
[1] "-177253333.333333343267441"

R Under development (unstable) (2025-03-08 r87909)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.2 LTS


On 3/9/25 13:12, Christofer Bogaso wrote:
> Hi,
>
> I have below simple conversion
>
>> sprintf("%0.15f",
as.numeric("-177253333.333333343267441"))
>
> [1] "-177253333.333333373069763"
>
> I could not figure out why the input and output is different?
>
> Clearly this conversion is incorrect. Is there any way to convert to
> numerical properly?
>
>> sessionInfo()
>
> R version 4.4.0 (2024-04-24)
>
> Platform: aarch64-apple-darwin20
>
> Running under: macOS 15.3.1
>
>
> Matrix products: default
>
> BLAS:
/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
>
> LAPACK:
/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;
>   LAPACK version 3.12.0
>
>
> locale:
>
> [1] C/UTF-8/C/C/C/C
>
>
> time zone: Asia
>
> tzcode source: internal
>
>
> attached base packages:
>
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
>
> loaded via a namespace (and not attached):
>
> [1] compiler_4.4.0
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Dr. Benjamin Bolker
Professor, Mathematics & Statistics and Biology, McMaster University
Director, School of Computational Science and Engineering
* E-mail is sent at my convenience; I don't expect replies outside of
working hours.