This is a general problem across all software. A computer has finite memory but
a floating-point value can have an infinite number of digits. Consider trying
to store the exact value of pi. All computer software has a limit to available
precision, and that value is specific to each piece of software. You can play
games to find that limit. Ask the computer what is 1 + 0.1?
Then keep going
1 + 0.01
1 + 0.001
And so forth, until the answer is 1. In this game be careful in what the
computer displays (as a default) and what it calculates. My version of Excel
will display a result of 1 after 1 + 0.0000001. However by changing the display
you can see that Excel continues out to 1E-15 before everything equals 1. Keep
in mind that this is significant digits. So if I change the problem to 1000 +
0.1 then Excel will display everything equals 1000 after 1000.001, and keep
calculating until 1E-11.
You can correctly process the string. This is one solution:
install.packages("Rmpfr")
library(Rmpfr)
x <- "-177253333.333333343267441"
mpfr_x <- mpfr(x, precBits = 128) # Increase precision
sprintf("%0.15f", as.numeric(mpfr_x))
Mathematical operations on mpfr_x give an answer
-354506666.6666666865348819999999999999992 if I multiply by 2.
If I increase precBits=256 then I get -354506666.666666686534882
This approach will let you read the numbers in correctly. You will need to
revisit this issue if you use these numbers in data analyses or even simple
calculations. Also consider how much memory is being consumed by the increase
in precision versus the available memory. This will become more problematic in
using these values in matrix algebra necessary in many data analyses.
Tim
-----Original Message-----
From: R-help <[email protected]> On Behalf Of Christofer Bogaso
Sent: Sunday, March 9, 2025 1:13 PM
To: r-help <[email protected]>
Subject: [R] Number changed weirdly when converting to numeric
[External Email]
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
______________________________________________
[email protected] 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.
______________________________________________
[email protected] 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.