# [R] number of decimal places in a number?

Jim Plante jimplante at me.com
Mon Jul 9 14:52:18 CEST 2012

```I don't know how significant this is, but WolframAlpha's value of pi disagrees with R's at about the 16th decimal:

Wpi->3.141592653589793238462643383279502884197169399375105
......................^
Rpi->3.14159265358979311599796346854418516

Probably not of interest to anyone but astronomers, nanotechnologists, and us anal lurkers though. We went to the moon and back on two decimal places.

Jim Plante

On Jul 8, 2012, at 1:39 PM, arun wrote:

> Hi Petr,
>
> I think sprintf and formatC are identical as it can round >22 decimal places as opposed to print and signif
> print(pi,digits=35)
> Error in print.default(pi, digits = 35) : invalid 'digits' argument
>  signif(pi,digits=22)
>  3.141593
> a<-sprintf("%.35f",pi)
> a
>  "3.14159265358979311599796346854418516"
> b<-formatC(pi,digits=36)
>  b
>  "3.14159265358979311599796346854418516"
>
>  identical(a,b)
>  TRUE
>
>
> identical(a,signif(pi,digits=35))
>  FALSE
>
>
> A.K.
>
>
>
> ----- Original Message -----
> From: Petr Savicky <savicky at cs.cas.cz>
> To: r-help at r-project.org
> Cc:
> Sent: Sunday, July 8, 2012 2:21 PM
> Subject: Re: [R] number of decimal places in a number?
>
> On Sat, Jul 07, 2012 at 01:12:34PM +0100, Ted Harding wrote:
>> I had thought of also (as well as my numerical routing) suggesting
>> a "gsub()" type solution like Joshua's below, but held back because
>> the result could depend on how the number arose (keyboard input,
>> file input, or from computation within R).
>>
>> However, I now also realise that (again because of binary rounding
>> errors), the "gsub()" method has interesting differences from my
>> numerical method. Example:
>>
>> [A] (as from my original method):
>>    f(123456789.123456789)
>>    #  7
>>
>> [B] (the "gsub()" method)
>>    nchar(gsub("(.*\\.)|(*\$)", "", as.character(123456789.123456789)))
>>    #  6
>>
>> Now look at:
>>
>> [C] (what as.character() does to 123456789.123456789)
>>    as.character(123456789.123456789)
>>    #  "123456789.123457"
>>
>> [D] ("22" is the maximum number of decimal digits for print())
>>    print(123456789.123456789,22)
>>    #  123456789.1234568
>>
>> So as.character() has rounded it to 6 decimal places (agreeing
>> with [B]), while using print() with the maximum of 22 digits
>> (more than enough for the 18 digits in 123456789.123456789)
>> rounds it to 7 decimal places (i.e. 16 digits in all), which
>
> Hi.
>
> This difference is due to rounding to 15 digits in as.character().
> This function rounds to 15 decimal digits, which is the maximum
> number of digits, which can always be converted to binary
> and back. Function print(, digits=22) prints the decimal
> equivalent of the represented number. So, it is more accurate, but
> its output may contain digits, which are purely the consequence
> of inaccuracy of the representation.
>
> The same output as from print(, digits=17) may be obtained
> using
>
>    sprintf("%20.17f", x)
>
> Of course, if the required number of digits is close to 17 or
> even more, the last digits are the last digits of the represented
> number, not of the intended result of the computation.
>
> Hope this helps.
>
> Petr Savicky.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
{"ft_lang_label":"__label__en","ft_lang_prob":0.74960756,"math_prob":0.73720014,"size":3854,"snap":"2020-45-2020-50","text_gpt3_token_len":1113,"char_repetition_ratio":0.14051948,"word_repetition_ratio":0.09137056,"special_character_ratio":0.41022316,"punctuation_ratio":0.20433018,"nsfw_num_words":1,"has_unicode_error":false,"math_prob_llama3":0.9697726,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-29T17:01:50Z\",\"WARC-Record-ID\":\"<urn:uuid:e468df9a-5672-4763-9f28-a9ccd0f28303>\",\"Content-Length\":\"8272\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:437dd427-c19f-413f-afe3-817bc5e68747>\",\"WARC-Concurrent-To\":\"<urn:uuid:212aeefd-01c4-4add-b2bb-947c83b42e33>\",\"WARC-IP-Address\":\"129.132.119.195\",\"WARC-Target-URI\":\"https://www.stat.math.ethz.ch/pipermail/r-help/2012-July/317817.html\",\"WARC-Payload-Digest\":\"sha1:IKVBFSX7NDA4HBSA7YCNRD25GQKBE2YN\",\"WARC-Block-Digest\":\"sha1:F2FZFCDSTPFZ3VS66HS4XGVRY5WGMPQH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107904834.82_warc_CC-MAIN-20201029154446-20201029184446-00108.warc.gz\"}"}