struggling with integers (maximum integer size)

前端 未结 4 877
野性不改
野性不改 2020-11-29 08:52

I was using a function that requires input as integers.

So I have been trying to read up on making things integers:

y <- 3.14
as.integer(y)
[1] 3          


        
4条回答
  •  清歌不尽
    2020-11-29 09:39

    The largest integer R can hold is

    .Machine$integer.max
    # [1] 2147483647
    

    This has nothing to do with scientific notation and everything to do with how the computer actually stores the numbers. The current version of R stores integers still as 32bit, regardless of the architecture. This might change in the future though.

    see also ?as.integer

    Currently you can get access to 64 bit integers through the int64 package

    > as.integer(.Machine$integer.max)
    [1] 2147483647
    > # We get problems with this
    > as.integer(.Machine$integer.max + 1)
    [1] NA
    Warning message:
    NAs introduced by coercion 
    > # But if we use int64
    > library(int64)
    > as.int64(.Machine$integer.max) + 1L
    [1] 2147483648
    

提交回复
热议问题