Issue with character data type length in R & decimal precision

前端 未结 1 1915
旧巷少年郎
旧巷少年郎 2021-01-15 08:24

Trying to create a function to get the precision of numeric data. (the number of digits to the right of the decimal place)

    decimalplaces <- function(x         


        
相关标签:
1条回答
  • 2021-01-15 08:38

    The 'problem' is not in nchar but in gsub, which applies as.character to a non-character x. The documentation for as.character says:

    as.character represents real and complex numbers to 15 significant digits (technically the compiler's setting of the ISO C constant DBL_DIG, which will be 15 on machines supporting IEC60559 arithmetic according to the C99 standard). This ensures that all the digits in the result will be reliable (and not the result of representation error), but does mean that conversion to character and back to numeric may change the number. If you want to convert numbers to character with the maximum possible precision, use format.

    So, you can use

    dec_part <- gsub(pattern,"\\2", format(x,digits=22))    
    

    instead of

    dec_part <- gsub(pattern,"\\2", x)
    

    in your code, but be careful because the 15 significant digit limit was set for a good reason, so there is a good chance to find just noise in trailing numbers. For example,

    > format(1/3,digits=22)
    [1] "0.3333333333333333148296"
    
    0 讨论(0)
提交回复
热议问题