0

If I enter the following in to the console of R:

1 - 3.125e-09

It prints 1 as its result.

However, a logical test will show that it is not actually 1.

if ( (1-3.125e-09) == 1){
   print('Number is equal to one')
} else {
   print('Number is not one')
}

How is this happening? How can I capture the true value? This behavior appears to break some graphing library functions where the total value should add up to 1.

Brian
  • 7,900
  • 1
  • 27
  • 41
Kevin Bolger
  • 106
  • 1
  • 9
  • 3
    `print(1 - 3.125e-09, digits=20)` will print the accurate value. This is just a display issue. How is it breaking some graphing library? – Warren Weckesser Oct 06 '19 at 00:07
  • 2
    Set the default number of digits with, for example, `options(digits=15)`; see https://stat.ethz.ch/R-manual/R-devel/library/base/html/options.html for more details. – Warren Weckesser Oct 06 '19 at 00:08
  • 1
    Possible duplicate of [Controlling number of decimal digits in print output in R](https://stackoverflow.com/questions/2287616/controlling-number-of-decimal-digits-in-print-output-in-r) – C8H10N4O2 Oct 06 '19 at 00:10
  • The print values not the issue. To clarify - its a donut chart. The values need to add up to one. If the value is really small, when I got to calculate the delta (1-small number) the value would print as 1 yes, but its true value is really small. However, when plotly tries to add that trace, it first draws the really small trace, but it cant add the second trace - as it thinks its value is 1. So there is no space for it. I can get around this by doing a quick check - just wondering is there a way to force accessing the true value. – Kevin Bolger Oct 06 '19 at 00:34
  • Try to create a reproducible example using `plotly` and add that to your question please. Otherwise this is likely to get closed as a duplicate. – Brian Oct 06 '19 at 00:38
  • @KevinBolger: That sounds like a bug in `plotly`. You should report it to the developers of that package. – user2554330 Oct 06 '19 at 02:33

0 Answers0