Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby get maximum integer size value

What is the Ruby equivalent for Java's Integer.MAX_VALUE?. Hopefully native.

Runner Up: If no equivalent, I need to set this for a Time object, so instead of hardcoding the maximum date for an integer 2116-02-20, is there a system constant that would work for this?

like image 935
user8897013 Avatar asked Dec 19 '17 19:12

user8897013


1 Answers

There's no maximum any more for integers, they automatically shift to "bignum" representation:

1 << 64
# => 18446744073709551616
(1 << 64) + 1
# => 18446744073709551617

There's really no limit other than memory:

1 << (1 << 16)
# => 20035299304...(thousands of digits)...05719156736

As for Time, it's similarly unbounded so now you can express times well after the heat death of the universe if you really want to:

Time.at(1<<128)
# => 10783118943836478994022445751222-08-06 04:04:16 -0400

This used to be limited to the usual +/- 2.1 billion range, subject to the 2038 problem but that hasn't been the case since Ruby ~1.9. I'm not sure where 2116 factors in except from a Windows perspective.

If you want to know the max/min that can be represented in a "native" integer then that's platform dependent. 32-bit and 64-bit binaries will have different limits.

like image 143
tadman Avatar answered Oct 15 '22 13:10

tadman