-2

I was perplexed by the difference in R:

log(0.0001)/0.0001
-92103.4

and, e.g., Google calculator rendering:

 ln(0.0001)/0.0001
 -92103.4037198

why is the rounding so different?

user1603472
  • 1,408
  • 15
  • 24
  • I agree that the answer is valid also for this question, but the wording of the other question does not give that away immediately. – user1603472 Jan 14 '17 at 23:15
  • 2
    the down-voting lately is getting crazy. clear question, minimal reproducible example, and expected output. I get that you all are super duper smart and this is a basic question for you, but seriously put your adult pants on for once and stop down-voting questions into oblivion – rawr Jan 15 '17 at 01:00
  • Seconding what @rawr says. And upvoting to counteract it. This is a newish user you're downvoting. Doing so without helping them understand why is not a way to build community. – Richard Jan 15 '17 at 05:45
  • Also, I am asking why, not only how to fix it. The question of why the default is set that only one decimal was not explained in the other answers, as far as I could see. That said, I am not offended by the downvotes, it definitely is answered elsewhere, albeit worded a bit clumsily. – user1603472 Jan 15 '17 at 22:35

1 Answers1

5

Increasing the displayed precision with, e.g.:

options(digits=20)

fixes the "problem"

> log(0.0001)/0.0001
[1] -92103.4
> options(digits=20)
> log(0.0001)/0.0001
[1] -92103.403719761816319

Note that the internal precision is always high and can be viewed with the .Machine variable:

> .Machine
#Many other details here
$double.digits
[1] 53

the foregoing indicates that the machine has a 53-bit mantissa, which indicates that double-precision floating-point is being used for the calculations, which is standard.

Richard
  • 56,349
  • 34
  • 180
  • 251
  • 1
    Could also do `print(log(0.0001)/0.0001, digits = 20)` and not have to worry about changing any options. – Rich Scriven Jan 14 '17 at 23:43
  • 1
    I think it's worth being explicit that the difference is only in the way the result is printed, not the way it's calculated or stored. `x <- log(0.0001)/0.0001`, followed by a calculation using `x` (eg: `x * 1E6`) returns a higher precision result (eg: `-92103403720`) than the printed result of `x` would appear to allow. – rosscova Jan 15 '17 at 03:33
  • @rosscova: I had noted it, but now stress it with additional information. I'd've been fine with you editing the answer as well :-) – Richard Jan 15 '17 at 05:43