Jeff Newmiller
2022-Feb-17 23:20 UTC
[R] Why does the print method fail for very small numbers?
Go to https://cs.lmu.edu/~ray/demos/ieee754.html and enter 2.48e-324 into the encoder box. You get 0000000000000001 which means you are well beyond normalized representation and are approximating this value with a total of 1 bit of precision. Do you really want to fault the print method for having difficulty returning your value to you after this much rounding? On February 17, 2022 2:40:09 PM PST, Marius Hofert <marius.hofert at uwaterloo.ca> wrote:>Dear expeRts, > >I'm familiar with IEEE 754. Is there an easy way to explain why even >just printing of small numbers fails? > >1e-317 # 1e-317 => fine >1e-318 # 9.999987e-319 => gets tricky; seems to call print() => as.character() => format() => paste() >1e-318 == 9.999987e-319 # TRUE >2.48e-324 # prints 4.940656e-324 for me >2.48e-324 == 4.940656e-324 # TRUE >## Relative error as a plot >rel_error <- function(x) > plot(abs((as.numeric(as.character(x)) - x) / x), type = "l", > ylab = "Relative error between x and as.numeric(as.character(x))") >rel_error(seq(0.001, 0.001 + .Machine$double.xmin, length.out = 1001)) # fine >rel_error(seq(0, .Machine$double.xmin, length.out = 1001)) # printing breaks down > >Of course, [0,.Machine$double.xmin] is somewhat of a strange set of numbers to consider, >and I expect things like "==" to be easily fooled there, but already the print method (?) > >Thanks & cheers, >Marius > >sessionInfo() >R version 4.1.2 (2021-11-01) >Platform: x86_64-apple-darwin21.2.0 (64-bit) >Running under: macOS Monterey 12.1 >... > > >______________________________________________ >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 http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.
Marius Hofert
2022-Feb-17 23:36 UTC
[R] Why does the print method fail for very small numbers?
Thanks, Jeff. ... all clear (as I wrote, I'm familiar with IEEE 754). I'm just wondering why this happens since the print method seems to call print() => as.character()... so shouldn't this *not* be a problem then (?). I guess at some point it is indeed handled as a number and then (as you wrote) the problem appears (for which one shouldn't blame anybody of course :-) ). Thanks & cheers, M ________________________________________ From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us> Sent: Thursday, February 17, 2022 18:20 To: r-help at r-project.org; Marius Hofert; r-help at r-project.org Subject: Re: [R] Why does the print method fail for very small numbers? Go to https://cs.lmu.edu/~ray/demos/ieee754.html and enter 2.48e-324 into the encoder box. You get 0000000000000001 which means you are well beyond normalized representation and are approximating this value with a total of 1 bit of precision. Do you really want to fault the print method for having difficulty returning your value to you after this much rounding? On February 17, 2022 2:40:09 PM PST, Marius Hofert <marius.hofert at uwaterloo.ca> wrote:>Dear expeRts, > >I'm familiar with IEEE 754. Is there an easy way to explain why even >just printing of small numbers fails? > >1e-317 # 1e-317 => fine >1e-318 # 9.999987e-319 => gets tricky; seems to call print() => as.character() => format() => paste() >1e-318 == 9.999987e-319 # TRUE >2.48e-324 # prints 4.940656e-324 for me >2.48e-324 == 4.940656e-324 # TRUE >## Relative error as a plot >rel_error <- function(x) > plot(abs((as.numeric(as.character(x)) - x) / x), type = "l", > ylab = "Relative error between x and as.numeric(as.character(x))") >rel_error(seq(0.001, 0.001 + .Machine$double.xmin, length.out = 1001)) # fine >rel_error(seq(0, .Machine$double.xmin, length.out = 1001)) # printing breaks down > >Of course, [0,.Machine$double.xmin] is somewhat of a strange set of numbers to consider, >and I expect things like "==" to be easily fooled there, but already the print method (?) > >Thanks & cheers, >Marius > >sessionInfo() >R version 4.1.2 (2021-11-01) >Platform: x86_64-apple-darwin21.2.0 (64-bit) >Running under: macOS Monterey 12.1 >... > > >______________________________________________ >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 http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.