Peter Dunn
2008-Jun-06 00:48 UTC
[R] Why doesn't formatC( x, digits=2, format="g") doesn't always give 2 sig figs?
Hi all I am not a C programmer, but I am trying to understand formatC to get consistent printing of reals to a given number of significant digits. Can someone please explain this to me? These first three give what I expect on reading ?formatC:> formatC(0.0059999, digits=2,format="fg",flag="#")[1] "0.0060"> formatC(0.59999, digits=2,format="fg",flag="#")[1] "0.60"> formatC(5.9999, digits=2,format="fg",flag="#")[1] "6.0" This seems consistent with what I read (but perhaps do not understand) in ?formatC, where I read this: digits the desired number of digits after the decimal point (format = "f") or significant digits (format = "g", = "e" or = "fg"). Since I am using format="fg" and digits=2, so I am expecting two significant digits to always show, which I have above. So I fail to understand this:> formatC(0.000059999, digits=2,format="fg",flag="#")[1] "0.00006"> formatC(0.000059, digits=2, format="fg",flag="#")[1] "0.000059" I was expecting both of these to produce "0.000059". But in the first case above, I get one significant digit only. I'm obviously misunderstanding something; can someone enlighten me? (No doubt, someone will point out a nuance of the help files I didn't understand!) Also, since the above obviously doesn't do what I hoped (consistently printing two sig figs), could someone also explain how I can consistently get two significant figures in situation like above? Thanks as always. P. -- Dr Peter Dunn | dunn <at> usq.edu.au Faculty of Sciences, USQ; http://www.sci.usq.edu.au/staff/dunn Aust. Centre for Sustainable Catchments: www.usq.edu.au/acsc This email (including any attached files) is confidentia...{{dropped:15}}
(Ted Harding)
2008-Jun-06 01:18 UTC
[R] Why doesn't formatC( x, digits=2, format="g") doesn't al
On 06-Jun-08 00:48:50, Peter Dunn wrote:> Hi all > I am not a C programmer, but I am trying to understand formatC to > get consistent printing of reals to a given number of significant > digits. > Can someone please explain this to me? These first three give what > I expect on reading ?formatC: > >> formatC(0.0059999, digits=2,format="fg",flag="#") > [1] "0.0060" >> formatC(0.59999, digits=2,format="fg",flag="#") > [1] "0.60" >> formatC(5.9999, digits=2,format="fg",flag="#") > [1] "6.0" > > This seems consistent with what I read (but perhaps do not > understand) in ?formatC, where I read this: > > digits the desired number of digits after the decimal point > (format = "f") or significant digits (format = "g", = "e" or > = "fg"). > > Since I am using format="fg" and digits=2, so I am expecting two > significant digits to always show, which I have above. So I fail to > understand this: > >> formatC(0.000059999, digits=2,format="fg",flag="#") > [1] "0.00006" >> formatC(0.000059, digits=2, format="fg",flag="#") > [1] "0.000059" > > I was expecting both of these to produce "0.000059". But in the > first case above, I get one significant digit only. > > I'm obviously misunderstanding something; can someone enlighten me? > (No doubt, someone will point out a nuance of the help files I > didn't understand!)I think that what is happening is that 0.000059999 rounds (to 2 significant digits) to 0.000060 in the first instance, and the "0", not being "significant" (it adds no information to 0.00006), is dropped.> Also, since the above obviously doesn't do what I hoped > (consistently printing two sig figs), could someone also explain > how I can consistently get two significant figures in situation > like above?I don't know! I thought using the "width" parameter might do it, but it does not affect the number of decimal places printed.> Thanks as always. > P.Sorry I coujldn't be more helpful! Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 06-Jun-08 Time: 02:18:00 ------------------------------ XFMail ------------------------------
Duncan Murdoch
2008-Jun-06 01:39 UTC
[R] Why doesn't formatC( x, digits=2, format="g") doesn't always give 2 sig figs?
On 05/06/2008 8:48 PM, Peter Dunn wrote:> Hi all > > I am not a C programmer, but I am trying to understand formatC to > get consistent printing of reals to a given number of significant > digits. > > Can someone please explain this to me? These first three give what > I expect on reading ?formatC: > > >> formatC(0.0059999, digits=2,format="fg",flag="#") > [1] "0.0060" >> formatC(0.59999, digits=2,format="fg",flag="#") > [1] "0.60" >> formatC(5.9999, digits=2,format="fg",flag="#") > [1] "6.0" > > > This seems consistent with what I read (but perhaps do not > understand) in ?formatC, where I read this: > > > digits the desired number of digits after the decimal point > (format = "f") or significant digits (format = "g", = "e" or > = "fg"). > > > Since I am using format="fg" and digits=2, so I am expecting two > significant digits to always show, which I have above. So I fail to > understand this: > > >> formatC(0.000059999, digits=2,format="fg",flag="#") > [1] "0.00006" >> formatC(0.000059, digits=2, format="fg",flag="#") > [1] "0.000059" > > > I was expecting both of these to produce "0.000059". But in the > first case above, I get one significant digit only. > > I'm obviously misunderstanding something; can someone enlighten me? > (No doubt, someone will point out a nuance of the help files I > didn't understand!) > > Also, since the above obviously doesn't do what I hoped > (consistently printing two sig figs), could someone also explain > how I can consistently get two significant figures in situation > like above?I'll let someone else comment on whether this is a bug in formatC; it looks like a bug or a documentation error to me. You can get 2 sig figs in your situation like this: sprintf("%.6f",0.000059999) but obviously this doesn't help with numbers on a different scale. For those, I think you're going to have to put together something like x <- 0.0000599999 sprintf("%.*f", -trunc(log10(x))+2, x) (which only works for numbers less than 1). Duncan Murdoch
Apparently Analagous Threads
- Bug/Error in formatC? (Was: Why doesn't formatC( x, digits=2, format= "g")...)
- Possible bug in formatC
- formatC with format="fg" displays number in exponential notation (PR#2299)
- cannot see the y-labels (getting cut-off)
- Formatting in formatC and format (PR#129)