0

I encountered a problem with R.

If I insert the following division I get these results.

9207550/4

Result:

2301888

But it should be:

2301887.5

If I add extend numerator and denominator for several zeros, I get the same result:

9207550.0000 / 4.0000

2301888

Does anybody encounter the same issue? Can someone explain this behavior to me?

Thank you in advance. Any help is appreciated.

Chris Kraus
  • 71
  • 1
  • 8
  • 9
    `print(9207550/4, digits=10)` so its an optical illusion / default printing quirk. See `?print.default`. Actually, you can type `2301887.5` at the console and also see the rounding. – Frank May 03 '17 at 14:17
  • 2
    You have shown to us the result of the printing. That differs from the result of the calculation. The result of the calculation is as you expected: `x <- 9207550/4; 4*x` – jogo May 03 '17 at 14:35
  • 1
    @Frank thank you for your comment. I see that I misinterpreted the result. – Chris Kraus May 04 '17 at 10:45
  • @jogo I see so the calculations are correct after all. Thanks. – Chris Kraus May 04 '17 at 10:46
  • Those other questions are focused on reducing long decimals. Since this addresses missing digits, I think it warrants its own question. The answers here certainly are different than the answers there. – Dannid Mar 02 '20 at 23:57

1 Answers1

2

There is an option in R to control the number of digit displayed. As an example:

options(digits=5)

or if you are not a fan of scientific notation then:

options(digits = 999)

Most systems tend to have some default display precision built in, however that is different from the actual calculation as mentioned by @jogo. R is no different from any of those systems when it comes to result rendition.

As far as the calculation is concerned, the base R calculation will be sufficient for a most numbers you will deal with in day to day life, however, if you really want to be precise to n decimals where n can be typically greater than 15, I would suggest using gmp, Rmpfr or rcdd packages.

EDIT

I just noticed your attempt at float division. Certain language versions like python 2.xx have a different treatment of division when it comes to floats vs integers keeping the storage and computational efficiency in mind for the times it was written. This difference is now gone in the latest version. R was built by statisticians for statisticians, so you can rest assured that when it comes to mathematical operations, the default treatment would be any different. Numbers are what they are -- numbers. Int and float are programmatic constructs, not mathematical ones.

Drj
  • 1,176
  • 1
  • 11
  • 26