0

How accurate is scientific notation in R? I'm generating data that as values like this:

-5.59924596431885e-320

Is this an accurate value? I know R is usual only accurate to 15 decimal places when the number is written like this:

-2.1411372887

So is the scientific notation accurate at such small numbers?

Plinth
  • 289
  • 1
  • 13
  • Accurate compared to what? Computers don't have an infinite level of precision. It's important to understand the limits of [floating point arithmetic](http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal). But it's hard to talk about accuracy when it's not clear what "true" value you are trying to calculate. – MrFlick Feb 11 '16 at 20:40
  • 1
    Questions that have been asked before like [Dealing with very small numbers in R](http://stackoverflow.com/questions/5802592/dealing-with-very-small-numbers-in-r) and [Handling extremely small numbers](http://stackoverflow.com/questions/26513029/handling-extremely-small-numbers) have recommendations like `Rmpfr`, `gmp`, and `Brobdingnag`. Please show effort by researching these options. – Pierre L Feb 11 '16 at 20:41
  • If these are estimates of parameters then they are effectively zero and that would be so if the power of 10 were even as low as -16. The "accuracy to 15 places" notion comes from the issues of error propagation in multiplying floating point numbers. It is also outside the range of possible values for the `double`- class in R which is listed on its help page as 2e-308 to 2e+308. So I think "not accurate" is the likely answer. – IRTFM Feb 11 '16 at 20:48
  • @PierreLafortune I'm not trying to get more accuracy. I'm just trying to understand whether the significand of -5.59924596431885e-320 or some similar number is precise to approximately 15 decimal places – Plinth Feb 11 '16 at 20:51
  • @42- So would the significand of -2.12414279668623E-224 be accurate to 15 digits since it is within the possible values of the double class? I'm just trying to understand how precision and accuracy works in R. – Plinth Feb 11 '16 at 20:57
  • Search Wikipedia for "Floating point number" (section on IEEE-754) and read the `?double` page. – IRTFM Feb 11 '16 at 21:09
  • 1
    @Plinth yes, number like `2.12414279668623E-224` will be accurate to 15 digits. But `5.59924596431885e-320` won't – Severin Pappadeux Feb 11 '16 at 22:25

2 Answers2

3

This isn't really a question about R, but a question about floating point representation. Normal doubles are accurate to 15 or 16 decimal digits. However, the smallest normal double is 2^-1022, or 2.225074e-308. Values smaller then this can be represented by doubles, but you start to lose precision, because the significand will be padded with zeros. So, for example, 1e-320 will only be accurate to about 3 or 4 decimal digits, since it is about 10^12 times smaller than min_double.

See: https://en.wikipedia.org/wiki/Denormal_number

The values of max and min double and machine epsilon are stored in R as .Machine. On my computer:

> .Machine
$double.eps
[1] 2.220446e-16

$double.neg.eps
[1] 1.110223e-16

$double.xmin
[1] 2.225074e-308

$double.xmax
[1] 1.797693e+308

$double.base
[1] 2

$double.digits
[1] 53

$double.rounding
[1] 5

$double.guard
[1] 0

$double.ulp.digits
[1] -52

$double.neg.ulp.digits
[1] -53

$double.exponent
[1] 11

$double.min.exp
[1] -1022

$double.max.exp
[1] 1024

$integer.max
[1] 2147483647

$sizeof.long
[1] 8

$sizeof.longlong
[1] 8

$sizeof.longdouble
[1] 16

$sizeof.pointer
[1] 8
mrip
  • 14,913
  • 4
  • 40
  • 58
  • See the question I'm having is when you say "Normal doubles are accurate to 15 or 16 decimal digits", when a number is written in scientific notation, does that rule apply to the entire number or only to the significand? – Plinth Feb 11 '16 at 21:05
2

If you're using IEEE-754 doubles in R (and I'm 99.99% sure you are), there are two minimum values:

  1. Normalized minimum, which, as @mrip said, is about 2.22507e-308

  2. Denormalized minimum, which is about 4.94066e-324

Moral of the story: you're getting denorms in your calculation, and no, they cannot be accurate to 15 decimal places

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64