Skip to content
Prev 178261 / 398506 Next

rounding problem + sprintf

Prof Brian Ripley wrote:
besides noting that r is as much an international standard as excel,
here's an example of how the otherwise buggy sprintf (less so now,
thanks to martin's recent fixes) and other print routines let excel bugs
sneak into r:

    (x = c(0.15, 0.25, 0.35, 0.45, 0.55, 0.65))

    round(x, 1)
    print(x, 1)
    sprintf('%.1f', x)

?print and ?sprintf do not say anything about how rounding is done, but
given the above strong statement about standards, one would expect that
the ties-to-even mode is used.  apparently, round (the function) and the
print routines (sprintf and print) either operate in different rounding
modes, or read the input (the internal representation of numbers)
differently. 

not so surprisingly, sprintf in r reproduces the behaviour of sprintf in
c.  but would it be a good idea to blame c for excel bugs in r?

vQ