Skip to content

Max sig figs as well as Min in print?

2 messages · (Ted Harding), Brian Ripley

#
Hi Folks,

Consider the following example (artificial, but it illustates the point):

  > r2<-sqrt(2)
  > x<-2-r2*r2

  > print(c(pi,sqrt(pi)),digits=5)
  [1] 3.1416 1.7725


  > print(c(pi,sqrt(pi),x),digits=5)
  [1]  3.1416e+00  1.7725e+00 -4.4409e-16

whereas I would prefer

  [1] 3.1416 1.7725 0.0000

which could be achieved by

  > print(c(pi,sqrt(pi),round(x)),digits=5)
  [1] 3.1416 1.7725 0.0000

if need be.

The above of course is due to the definition of "digits" as the miniumum
number of significant figures to print. In order to avoid the behaviour of
the second "print" and make it like the last line, one would need to be
able to set somthing like "maxdigits=5" as well as "digits=5".

Is anything of this sort possible (apart from anticipating the situation
and wrapping relevant computations in "round")?

Thanks, and best wishes,
Ted.
#
On Thu, 5 Jun 2003 Ted.Harding at nessie.mcc.ac.uk wrote:

            
Might zapsmall help?
[1] 3.1416 1.7725 0.0000

Alternatively, 

sprintf("%8.4f %8.4f %8.4f", pi,sqrt(pi),x)
formatC(c(pi,sqrt(pi),x), digits=4, format="f")