Integer (arbitrary-precision)
is Cool does Real
Int
objects store integral numbers of arbitrary size. Int
s are immutable.
There are two main syntax forms for Int
literals
123; # Int in decimal notation:16<BEEF>; # Int in radix notation
For your convenience common radix forms come with a prefix shortcut.
say so :2<11111111> == 0b11111111 == :8<377> == 0o377 == 255 == 0d255 == :16<ff> == 0xff;# OUTPUT: «True»
All forms allow underscores between any two digits which can serve as visual separators, but don't carry any meaning:
5_00000; # five Lakhs500_000; # five hundred thousand0xBEEF_CAFE; # a strange place:2<1010_1010>; # 0d170
Radix notation also supports round and angle brackets which allow you to parse a string for a given base, and putting together digits into a whole number respectively:
:16("9F"); # 159:100[99, 2, 3]; # 990203
These notations allow you to use variables, too:
my = "2";my = "99";:16(); # 153:100[99, , 3]; # 990203
multi method new(Any )multi method new(Any \value --> Int)multi method new(int \value --> Int)
The first form will throw an exception; the second and third form will create an new Int from the actual integer value contained in the variable.
method Capture()
Throws X::Cannot::Capture
.
multi sub chr(Int --> Str)multi method chr(Int: --> Str)
Returns a one-character string, by interpreting the integer as a Unicode codepoint number and converting it to the corresponding character.
Example:
65.chr; # returns "A"196.chr; # returns "Ä"
multi sub expmod( , , --> Int)multi sub expmod(Int , Int , Int --> Int)multi method expmod(Int: Int , Int --> Int)
Returns the given Int
raised to the $y
power within modulus $mod
, that is gives the result of ($x ** $y) mod $mod
. The subroutine form can accept non-Int
arguments, which will be coerced to Int
.
say expmod(4, 2, 5); # OUTPUT: «1»say 7.expmod(2, 5); # OUTPUT: «4»
$y
argument can also be negative, in which case, the result is equivalent to ($x ** $y)
mod $mod.
say 7.expmod(-2, 5); # OUTPUT: «4»
method polymod(Int: +)
Returns a sequence of mod results corresponding to the divisors in @mods
in the same order as they appear there. For the best effect, the divisors should be given from the smallest "unit" to the largest (e.g. 60 seconds per minute, 60 minutes per hour) and the results are returned in the same way: from smallest to the largest (5 seconds, 4 minutes). The last non-zero value will be the last remainder.
say 120.polymod(10); # OUTPUT: «(0 12)»say 120.polymod(10,10); # OUTPUT: «(0 2 1)»
In the first case, 120 is divided by 10 giving as a remainder 12, which is the last element. In the second, 120 is div
ided by 10, giving 12, whose remainder once divided by 10 is 2; the result of the integer division of 12 div
10 is the last remainder. The number of remainders will be always one more item than the number of given divisors. If the divisors are given as a lazy list, runs until the remainder is 0 or the list of divisors is exhausted. All divisors must be Int
s, unless the method is called on a non-Int
number.
my = 1 * 60*60*24 # days+ 3 * 60*60 # hours+ 4 * 60 # minutes+ 5; # secondssay .polymod(60, 60); # OUTPUT: «(5 4 27)»say .polymod(60, 60, 24); # OUTPUT: «(5 4 3 1)»say 120.polymod: 1, 10, 10², 10³, 10⁴; # OUTPUT: «(0 0 12 0 0 0)»say 120.polymod: lazy 1, 10, 10², 10³, 10⁴; # OUTPUT: «(0 0 12)»say 120.polymod: 1, 10, 10² … ∞; # OUTPUT: «(0 0 12)»say ⅔.polymod(⅓); # OUTPUT: «(0 2)»say 5.Rat.polymod(.3, .2); # OUTPUT: «(0.2 0 80)»my = 9123607.polymod(37 xx *); # Base conversionsay .reverse # OUTPUT: «[4 32 4 15 36]»
To illustrate how the Int
, non-lazy version of polymod works, consider this code that implements it:
my = 2 * 60*60*24 # days+ 3 * 60*60 # hours+ 4 * 60 # minutes+ 5; # secondsmy ;for 60, 60, 24 ->.push: ;say ; # OUTPUT: «[5 4 3 2]»
For a more detailed discussion, see this blog post.
We can use lazy lists in polymod
, as long as they are finite:
my = lazy gather ;say 600.polymod( ); # OUTPUT: «(0 2 6 3)»
multi sub is-prime (Int --> Bool)multi method is-prime (Int: --> Bool)
Returns True
if this Int
is known to be a prime, or is likely to be a prime based on a probabilistic Miller-Rabin test.
Returns False
if this Int
is known not to be a prime.
say 2.is-prime; # OUTPUT: «True»say is-prime(9); # OUTPUT: «False»
multi method lsb(Int:)multi sub lsb(Int)
Short for "Least Significant Bit". Returns Nil if the number is 0. Otherwise returns the zero-based index from the right of the least significant (rightmost) 1 in the binary representation of the number.
say 0b01011.lsb; # OUTPUT: «0»say 0b01010.lsb; # OUTPUT: «1»say 0b10100.lsb; # OUTPUT: «2»say 0b01000.lsb; # OUTPUT: «3»say 0b10000.lsb; # OUTPUT: «4»
multi method msb(Int:)multi sub msb(Int)
Short for "Most Significant Bit". Returns Nil if the number is 0. Otherwise returns the zero-based index from the right of the most significant (leftmost) 1 in the binary representation of the number.
say 0b00001.msb; # OUTPUT: «0»say 0b00011.msb; # OUTPUT: «1»say 0b00101.msb; # OUTPUT: «2»say 0b01010.msb; # OUTPUT: «3»say 0b10011.msb; # OUTPUT: «4»
multi sub unival(Int --> Numeric)multi method unival(Int: --> Numeric)
Returns the number represented by the Unicode codepoint with the given integer number, or NaN if it does not represent a number.
say ord("¾").unival; # OUTPUT: «0.75»say 190.unival; # OUTPUT: «0.75»say unival(65); # OUTPUT: «NaN»
Returns a Range object that represents the range of values supported.
method Bridge(Int: --> Num)
Returns the integer converted to Num
.
multi sub infix:<div>(Int, Int --> Int)
Does an integer division, rounded down.