R shows too few digits

Viewed 53

I wanted to calculate that term:

(21000*(1.022^7-1))/(1.022-1)

R tells me, it's 157065.7. This was my answer to a task that I had to solve. It was stated that this answer is not correct, the correct answer should be 157065.67. Then I calculated this with my calculator on my smartphone and indeed, the solution was 157065.67034. I am really wondering what is wrong with R and whether I can change some options. I've tried options()$digits but that's 7, this can't be the problem...

3 Answers

One option:

formatC((21000*(1.022^7-1))/(1.022-1), digits = 11)
#> [1] "157065.67035"

Created on 2021-06-24 by the reprex package (v2.0.0)

Use options(digits = 15). Note that this changes globally the format. Also, the number of digits indicates all the digits (before and after the decimal separator).

Here's how you would display more decimals.

print((21000*(1.022^7-1))/(1.022-1), digits=16)
[1] 157065.670346861

There's nothing "wrong" with R in this respect. Decisions have to be made about how to print floating point values to the console.

Related