Dealing with large numbers in R [Inf] and Python

后端 未结 2 820
野性不改
野性不改 2021-01-12 05:55

I am learning Python these days, and this is probably my first post on Python. I am relatively new to R as well, and have been using R for about a year. I am comparing both

2条回答
  •  隐瞒了意图╮
    2021-01-12 06:33

    Apparently python uses arbitrary precision integers by default when needed. R does not. However, there are many useful R packages to perform arbitrary precision arithmetic. Which package to pick depends on the use case.

    To bring up a package that hasn't been discussed yet, consider the Rmpfr package:

    > library(Rmpfr)
    > a <- 2^mpfr(1500, 10000)
    > a
    1 'mpfr' number of precision  10000   bits 
    [1] 35074662110434038747627587960280857993524015880330828824075798024790963850563322203657080886584969261653150406795437517399294548941469959754171038918004700847889956485329097264486802711583462946536682184340138629451355458264946342525383619389314960644665052551751442335509249173361130355796109709885580674313954210217657847432626760733004753275317192133674703563372783297041993227052663333668509952000175053355529058880434182538386715523683713208549376
    

    It requires you to set a precision, but if you make it large enough it can hold 2^1500 as integer.

    However, it also doesn't seem to define an as.character() function:

    > as.character(a)
    [1] ""
    

    So if your problem is specifically to count digits, then the gmp package as discussed in this answer is probably the way to go. On the other hand, if you're interested in arbitrary precision floating point arithmetic, Rmpfr might be a better choice.

提交回复
热议问题