long integer handling

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

long integer handling

Yousri Fanous
I want to calculate 2^64-1 which is
18446744073709551615

I set the following options to prevent scientific notation
options("scipen"=100, "digits"=4)
> x<-2^64 -1
> x
[1] 18446744073709551616

This is not correct. There seem to be still some approximation happening.
How can I get the correct result?

Yousri
IBM Canada ltd
Software developer

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: long integer handling

R help mailing list-2
The largest consecutive integer that can be represented in double
precision is 2^53.

You'll have to move past double precision.

---JRG



On 2020-11-13 20:44, Yousri Fanous wrote:

> I want to calculate 2^64-1 which is
> 18446744073709551615
>
> I set the following options to prevent scientific notation
> options("scipen"=100, "digits"=4)
>> x<-2^64 -1
>> x
> [1] 18446744073709551616
>
> This is not correct. There seem to be still some approximation happening.
> How can I get the correct result?
>
> Yousri
> IBM Canada ltd
> Software developer
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: [External] long integer handling

Richard M. Heiberger
In reply to this post by Yousri Fanous
You need the Rmpfr package.  Your calculation of 2^64 is an ordinary
double precision number with 53 bits of precision.

> library(Rmpfr)
Loading required package: gmp

Attaching package: ‘gmp’

The following objects are masked from ‘package:base’:

    %*%, apply, crossprod, matrix, tcrossprod

C code of R package 'Rmpfr': GMP using 64 bits per limb


Attaching package: ‘Rmpfr’

The following object is masked from ‘package:gmp’:

    outer

The following objects are masked from ‘package:stats’:

    dbinom, dgamma, dnorm, dpois, pnorm

The following objects are masked from ‘package:base’:

    cbind, pmax, pmin, rbind

> class(2)
[1] "numeric"
> class(2^32)
[1] "numeric"
> class(2^64)
[1] "numeric"
> Two <- mpfr(2, precBits=64)
> Two^64
1 'mpfr' number of precision  64   bits
[1] 18446744073709551616
> class(Two^64)
[1] "mpfr"
attr(,"package")
[1] "Rmpfr"
> Two^64 - 1
1 'mpfr' number of precision  64   bits
[1] 18446744073709551615
> getPrec(Two)
[1] 64
> getPrec(2.)
[1] 53
>

On Fri, Nov 13, 2020 at 8:45 PM Yousri Fanous <[hidden email]> wrote:

>
> I want to calculate 2^64-1 which is
> 18446744073709551615
>
> I set the following options to prevent scientific notation
> options("scipen"=100, "digits"=4)
> > x<-2^64 -1
> > x
> [1] 18446744073709551616
>
> This is not correct. There seem to be still some approximation happening.
> How can I get the correct result?
>
> Yousri
> IBM Canada ltd
> Software developer
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: long integer handling

Rui Barradas
In reply to this post by Yousri Fanous
Hello,

You can compute the exact result with package Rmpfr.
See ?mpfr and [1].


library(Rmpfr)

two <- mpfr(2, precBits = 64)
two^64 - 1
#1 'mpfr' number of precision  64   bits
#[1] 18446744073709551615



[1] https://www.mpfr.org/

Hope this helps,

Rui Barradas

Às 01:44 de 14/11/20, Yousri Fanous escreveu:

> I want to calculate 2^64-1 which is
> 18446744073709551615
>
> I set the following options to prevent scientific notation
> options("scipen"=100, "digits"=4)
>> x<-2^64 -1
>> x
> [1] 18446744073709551616
>
> This is not correct. There seem to be still some approximation happening.
> How can I get the correct result?
>
> Yousri
> IBM Canada ltd
> Software developer
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: long integer handling

Rui Barradas
Hello,

I forgot to suggest package gmp. See the second example in

?gmp::bigz

Hope this helps,

Rui Barradas

Às 05:50 de 14/11/20, Rui Barradas escreveu:

> Hello,
>
> You can compute the exact result with package Rmpfr.
> See ?mpfr and [1].
>
>
> library(Rmpfr)
>
> two <- mpfr(2, precBits = 64)
> two^64 - 1
> #1 'mpfr' number of precision  64   bits
> #[1] 18446744073709551615
>
>
>
> [1] https://www.mpfr.org/
>
> Hope this helps,
>
> Rui Barradas
>
> Às 01:44 de 14/11/20, Yousri Fanous escreveu:
>> I want to calculate 2^64-1 which is
>> 18446744073709551615
>>
>> I set the following options to prevent scientific notation
>> options("scipen"=100, "digits"=4)
>>> x<-2^64 -1
>>> x
>> [1] 18446744073709551616
>>
>> This is not correct. There seem to be still some approximation happening.
>> How can I get the correct result?
>>
>> Yousri
>> IBM Canada ltd
>> Software developer
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: long integer handling

Yousri Fanous
Thank you!

On Sat, Nov 14, 2020 at 12:54 AM Rui Barradas <[hidden email]> wrote:

> Hello,
>
> I forgot to suggest package gmp. See the second example in
>
> ?gmp::bigz
>
> Hope this helps,
>
> Rui Barradas
>
> Às 05:50 de 14/11/20, Rui Barradas escreveu:
> > Hello,
> >
> > You can compute the exact result with package Rmpfr.
> > See ?mpfr and [1].
> >
> >
> > library(Rmpfr)
> >
> > two <- mpfr(2, precBits = 64)
> > two^64 - 1
> > #1 'mpfr' number of precision  64   bits
> > #[1] 18446744073709551615
> >
> >
> >
> > [1] https://www.mpfr.org/
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> > Às 01:44 de 14/11/20, Yousri Fanous escreveu:
> >> I want to calculate 2^64-1 which is
> >> 18446744073709551615
> >>
> >> I set the following options to prevent scientific notation
> >> options("scipen"=100, "digits"=4)
> >>> x<-2^64 -1
> >>> x
> >> [1] 18446744073709551616
> >>
> >> This is not correct. There seem to be still some approximation
> happening.
> >> How can I get the correct result?
> >>
> >> Yousri
> >> IBM Canada ltd
> >> Software developer
> >>
> >>     [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> >> 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.
> >>
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.