An example would be performing a mathematical operation using the value of a string variable. So, bit64::as.integer64.character can be useful, since precision is not lost when you type it or read it in as a string: bit64::as.integer64("-8664354335142704128")īit64::as.integer64("-8664354335142704128") = bit64::as.integer64("-8664354335142703762")įYI, your number is already near the 64-bit boundary: -.Machine$integer.When working with a programming language, you may want to convert strings to integers. # as.integer64.integer as.integer64.integer64 as.integer64.logical # as.integer64.character as.integer64.double as.integer64.factor I haven't fished yet, but I'm guessing there's something meaningful about this with respect to floating point precision in 32-bit land.įortunately, bit64::as.integer64 has several S3 methods, useful for converting different formats/classes to a integer64 library(bit64) It is unlikely to be coincidence that the difference is 1024, or 2^10. In this case, it just *happens that the 64-bit version of that number is equal to what you intended. Only then does it send this numeric to the function, but by this point the precision has already been lost. (This is typically a compiler/language-parsing thing, not just an R thing.) In this case, it sees that it appears to be a (large) negative float, so it creates a class numeric (float). R first has to parse and "understand" everything inside the parentheses before it can be passed to the function. More appropriately, when you type it in as a number and then try to convert it, R processes the inside of the parentheses first. # Warning: non-integer value 8664354335142703762L qualified with L using numeric value Even if you had tried to create it as an integer, it would have complained and lost precision anyway: class(5) In R, when creating a number, 5 is a float and 5L is an integer. "I suppose toString() converts the number to float", nope, you did it yourself (even if unintentionally). So I marked r2evans answer as the best answer because this user helped me to find the right solution. ""I suppose toString() converts the number to float", nope, you did it yourself (even if unintentionally)." - Nope, R did it itself, that is the way how R reads numbers. I have to change the way how to pass numbers to R function, and it solves my problem. Now I know, the problem is how R reads and stores numbers. For example in Python: > def my_function(long_number): I am not R expert, so I just was curious why in another language it works and in R not. I just need to use something that some user passed. Then I realized, in R is impossible to get the real value of a long number passed into a function, because R automatically read the value with the loss.įor example, I have the function: > my_function my_function(-8664354335142703762)įor example, if I read some numbers from a file, it is easy. My question first was, if I can convert a long number to string without loss. I expected toString(a) = toString(b), but I got different values. How to easily convert a number to string to preserve precision? A have a simple example below. I have a problem to convert a long number to a string in R.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |