[R] rounding issues
(Ted Harding)
ted.harding at wlandres.net
Thu Oct 14 12:10:04 CEST 2010
On 14-Oct-10 09:43:53, Federico Calboli wrote:
> Hi All,
>
> I'm running the now almost-to-be upgraded R 2.11.1 on a Intel Mac, and
> on a Ubuntu machine, but the problem I see is the same. I noticed the
> following behaviour:
>
> 407585.91 * 0.8
> [1] 326068.7 -- the right asnwer is 326068.728
You need to keep in mind the distinction between the result
that R displays, and the result that it has in internal storage.
Compare your result above with:
print(407585.91 * 0.8,7)
# [1] 326068.7
print(407585.91 * 0.8,8)
# [1] 326068.73
print(407585.91 * 0.8,9)
# [1] 326068.728
print(407585.91 * 0.8,10)
# [1] 326068.728
print(407585.91 * 0.8,11)
# [1] 326068.728
all being the correct answer provided you print() to sufficiently
many digits (at least 9 in this case). Without the requirement
to print to enough digits, the default (see '?print.default')
is what is returned by 'getOption("digits")', which again by
default is 7 (though you can change the "digits" option if you
want a different value to apply globally).
> round(407585.91 * 0.8, 2)
> [1] 326068.7 -- same issue
> 407585.91/100
> [1] 4075.859 -- the right answer is 4075.8591
And this has the same explanation:
print(round(407585.91 * 0.8, 2),8)
# [1] 326068.73
print(round(407585.91 * 0.8, 2),9)
# [1] 326068.73
print(round(407585.91 * 0.8, 2),10)
# [1] 326068.73
'round(407585.91 * 0.8, 2)' has 8 digits, but by default will
be displayed to 7 digits. Provided you print() to at least 8
digits, the result will be fully correct.
Similarly:
print(407585.91/100,8)
# [1] 4075.8591
> I have no saved .Rwhatever in my environment, and I never set
> any option to have such strange rounding. I'm obviously missing
> something, and I'd appreciate suggestions.
>
> Best,
> Federico
The rounding you observe is carried out when R prepares what
is to be displayed on screen and, as illustrated above, this
has a default value of 7 digits -- though this only applies to
rounding of the fractional part: the integer part is always
displayed in full:
1234567891/10
# [1] 123456789
print(1234567891/10,10)
# [1] 123456789.1
print(1234567891/10,4)
# [1] 123456789
The internally stored value is always stored to the full available
precision.
Hoping this helps,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 14-Oct-10 Time: 11:10:01
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list