[R] "digits" in round()

Martin Maechler maechler at stat.math.ethz.ch
Tue Mar 31 15:15:32 CEST 2009


>>>>> "TH" == Ted Harding <Ted.Harding at manchester.ac.uk>
>>>>>     on Tue, 31 Mar 2009 13:59:41 +0100 (BST) writes:

    TH> Hi Folks,
    TH> Compare

    TH> print(1234567890,digits=4)
    TH> # [1] 1.235e+09
    TH> print(1234567890,digits=5)
    TH> # [1] 1234567890

    TH> Granted that

    TH> digits: a non-null value for 'digits' specifies the minimum
    TH> number of significant digits to be printed in values.

    TH> how does R decide to switch from the "1.235e+09" (rounded to
    TH> 4 digits, i.e. the minumum, in "e" notation) to "1234567890" (the
    TH> complete raw notation, 10 digits) when 'digits' goes from 4 to 5?

that's easy (well, as I'm one of the co-implementors ...) :

One of the design ideas has been to use "e"-notation only when
it's shorter (under the constraints given by 'digits'), i.e.,

   1.2346e+09
is not shorter (but has less information) than
   1234567890
hence the latter is chosen.
There are quite a few cases, and constraints (*) that apply
simultaneously, such that sometimes the default numeric
formatting may seem peculiar,  but I hope that in the mean
time  we have squished all real bugs here.

*) such as platform (in)dependency; S - back-compatibility, ..

Best regards,
Martin Maechler, ETH Zurich




More information about the R-help mailing list