[R] why the base::round(0.015, 2) returns 0.02?

Philipp Upravitelev upr@vitelev @ending from gm@il@com
Wed Nov 28 14:49:42 CET 2018


Dear colleagues,
could you help me with the function base::round()? I can't understand how
it works.

For example, when I want to round 0.015 to the second digit, base::round()
returns 0.02.

But the real representation of the 0.015 is different:
> sprintf('%.20f', 0.015)
[1] "0.01499999999999999944"
> 0.015 == 0.01499999999999999944
[1] TRUE
> round(0.015, 2)
[1] 0.02

Therefore, according to the arithmetic rules, rounded 0.014 to the second
digit is 0.01. Also, the round() function in other programming languages
(Python, Java) returns 0.01. It is a bit counterintuitive but
mathematically correct.

I'll be very pleased if you could help me to figure out why the
base::round(0.015, 2) returns 0.02 and what is the purpose of this feature.

Best regards,
Philipp Upravitelev

	[[alternative HTML version deleted]]



More information about the R-help mailing list