Right digits of a floating number

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Right digits of a floating number

vincent.deluard

Hi all,

I am dealing with very large numbers but I am only interested in their last digits.

> 244^244
[1] Inf

As far as I can tell, the largest number R can take has 308 digits (1+E308). Is there a way I could see the last digits only of 244^244?

Many thanks for your help.

Vincent Deluard, CFA.
Reply | Threaded
Open this post in threaded view
|

Re: Right digits of a floating number

jholtman
You can get the 'bc' package for R, or just use 'bc' itself:

bc 1.05
Copyright 1991, 1992, 1993, 1994, 1997, 1998 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
244^244
33351673199434891333790950239586798749134713899690921746779018273444\
75398673310645133249773025954160733596789166000500186867664357401485\
87160730684300567846077908572759584281796987638871579388947169645252\
65884981224005256730735620079297185198463448605170457608557721076573\
61665848520780522718876972173657918707476536085620731363673111929041\
83170713323201797482821835264498138162569644028599053754627294465957\
51701663776258263242051496174799483177057640433668486014770839602370\
82298835743579080531187365656700060161423751088967180500399136029256\
656142701941657367182137862147849322496


On Tue, Jul 27, 2010 at 6:34 PM, vincent.deluard
<[hidden email]> wrote:

>
>
> Hi all,
>
> I am dealing with very large numbers but I am only interested in their last
> digits.
>
>> 244^244
> [1] Inf
>
> As far as I can tell, the largest number R can take has 308 digits (1+E308).
> Is there a way I could see the last digits only of 244^244?
>
> Many thanks for your help.
>
> Vincent Deluard, CFA.
> --
> View this message in context: http://r.789695.n4.nabble.com/Right-digits-of-a-floating-number-tp2304210p2304210.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Right digits of a floating number

Richard M. Heiberger
In reply to this post by vincent.deluard
>
>
> As far as I can tell, the largest number R can take has 308 digits
> (1+E308).
>

This is not a correct statement.  The magnitude of the largest 64-bit double
precision floating
point number in R is approximately 1E308.  The internal storage in R (and
in most computers today)
contains only the first 53 binary digits of the number multiplied by a power
of 2.  Please see R FAQ 7.31
and the Goldberg article referenced there.

The bc results Jim suggested will get you the several hundred digits of the
full precision number,
but that is not what R sees.

The best way to see the exact number internally stored in R is the sprintf
%a format

sprintf("%+13.13a", x)

> sprintf("%+13.13a", .66)
[1] "+0x1.51eb851eb851fp-1"
> sprintf("%+13.13a", 1-.34)
[1] "+0x1.51eb851eb851ep-1"
> sprintf("%+13.13a", .66-(1-.34))
[1] "+0x1.0000000000000p-53"
>

In the .66 display, the repeating hex (51eb8) was rounded, in the 1-.34
display it was chopped.
The difference between these two numbers is not 0.

Rich

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Right digits of a floating number

Duncan Murdoch-2
In reply to this post by vincent.deluard
On 27/07/2010 6:34 PM, vincent.deluard wrote:

>
> Hi all,
>
> I am dealing with very large numbers but I am only interested in their last
> digits.
>
>> 244^244
> [1] Inf
>
> As far as I can tell, the largest number R can take has 308 digits (1+E308).
> Is there a way I could see the last digits only of 244^244?
>
> Many thanks for your help.
>
> Vincent Deluard, CFA.


Use the facts that the last digits of X are equal to X modulo 10^n, for
some n, and that X*Y modulo Z = (X modulo Z) * (Y modulo Z) modulo Z.
Then you can calculate it easily in base R, as long as n is no more than
7 (so that the individual multiplications don't overflow).  For example,

 > result <- 1
 > for ( i in 1:244)
+ result <- (result * 244) %% 1000
 > result
[1] 496

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.