Int Reference
Integer (arbitrary-precision)
What it is #
Position in the hierarchy #
Routines #
Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.
method new #
multi method new(Any:U $type)
multi method new(Any:D \value --> Int:D)
multi method new(int \value --> Int:D)
Returns Int:D.
The first form will throw an exception; the second and third form will create a new Int from the actual integer value contained in the variable.
method Str #
multi method Str(Int:D)
multi method Str(Int:D, :$superscript)
multi method Str(Int:D, :$subscript)
Returns a string representation of the number. Cool being a parent class of Int, an explicit call to the Int.Str method is seldom needed, unless you want the string to be returned in superscript or subscript. The :superscript and :subscript named arguments are available as of the 2023.05 Rakudo compiler release.
method Capture #
method Capture()
Throws X::Cannot::Capture.
routine chr #
multi chr(Int:D --> Str:D)
multi method chr(Int:D: --> Str:D)
Returns Str:D.
Returns a one-character string, by interpreting the integer as a Unicode codepoint number and converting it to the corresponding character. Example:
routine expmod #
multi expmod( $x, $y, $mod --> Int:D)
multi expmod(Int:D $x, Int $y, Int $mod --> Int:D)
multi method expmod(Int:D: Int $y, Int $mod --> Int:D)
Returns Int:D.
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. $y argument can also be negative, in which case, the result is equivalent to ($x ** $y) mod $mod.
method polymod #
method polymod(Int:D: +@mods)
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.
routine is-prime #
multi is-prime (Int:D $number --> Bool:D)
multi method is-prime (Int:D: --> Bool:D)
Returns Bool:D.
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.
routine lsb #
multi method lsb(Int:D:)
multi lsb(Int:D)
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.
routine msb #
multi method msb(Int:D:)
multi msb(Int:D)
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.
routine unival #
multi unival(Int:D --> Numeric)
multi method unival(Int:D: --> Numeric)
Returns Numeric.
Returns the number represented by the Unicode codepoint with the given integer number, or NaN if it does not represent a number.
method Range #
Returns a Range object that represents the range of values supported.
method Bridge #
method Bridge(Int:D: --> Num:D)
Returns Num:D.
Returns the integer converted to Num.
infix div #
multi infix:<div>(Int:D, Int:D --> Int:D)
Returns Int:D.
Does an integer division, rounded down.
Examples, run three ways #
Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.
1 all-differ · 10 no-output · 2 not-runnable · 11 ok
class Int is Cool does Real { }Not executed: the documentation states no expected output for this example.
123; # Int in decimal notation :16<BEEF>; # Int in radix notation
Not executed: the documentation states no expected output for this example.
say so :2<11111111> == 0b11111111 == :8<377> == 0o377 == 255 == 0d255 == :16<ff> == 0xff; # OUTPUT: «True»
True
Documentation, Rakudo and Raku++ all agree.
5_00000; # five Lakhs 500_000; # five hundred thousand 0xBEEF_CAFE; # a strange place :2<1010_1010>; # 0d170
Not executed: the documentation states no expected output for this example.
:16("9F"); # 159
:100[99, 2, 3]; # 990203Not executed: the documentation states no expected output for this example.
my $two = "2"; my $ninety-nine = "99"; :16($ninety-nine); # 153 :100[99, $two, 3]; # 990203
Not executed: the documentation states no expected output for this example.
say 42.Str; # OUTPUT: «42»
42
Documentation, Rakudo and Raku++ all agree.
say 42.Str(:superscript); # OUTPUT: «⁴²» say 42.Str(:subscript); # OUTPUT: «₄₂»
⁴²
₄₂
Documentation, Rakudo and Raku++ all agree.
65.chr; # returns "A" 196.chr; # returns "Ä"
Not executed: the documentation states no expected output for this example.
say expmod(4, 2, 5); # OUTPUT: «1» say 7.expmod(2, 5); # OUTPUT: «4»
1
4
Documentation, Rakudo and Raku++ all agree.
say 7.expmod(-2, 5); # OUTPUT: «4»
4
Documentation, Rakudo and Raku++ all agree.
say 120.polymod(10); # OUTPUT: «(0 12)» say 120.polymod(10,10); # OUTPUT: «(0 2 1)»
(0 12)
(0 2 1)
Documentation, Rakudo and Raku++ all agree.
my $seconds = 1 * 60*60*24 # days
+ 3 * 60*60 # hours
+ 4 * 60 # minutes
+ 5; # secondsNot executed: the documentation states no expected output for this example.
say $seconds.polymod(60, 60); # OUTPUT: «(5 4 27)» say $seconds.polymod(60, 60, 24); # OUTPUT: «(5 4 3 1)»
Neither engine can run this in isolation — the example depends on context from the surrounding text.
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)» my @digits-in-base37 = 9123607.polymod(37 xx *); # Base conversion say @digits-in-base37.reverse # OUTPUT: «[4 32 4 15 36]»
(0 0 12 0 0 0)
(0 0 12)
(0 0 12)
[4 32 4 15 36]
(120)
(120)
(120)
(4 32 4 15 36)
(0 0 12 0 0 0)
(120)
(120)
(4 32 4 15 36)
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
say 120.polymod(⅓); # ERROR
Not executed: the documentation states no expected output for this example.
my $seconds = 2 * 60*60*24 # days
+ 3 * 60*60 # hours
+ 4 * 60 # minutes
+ 5; # secondsNot executed: the documentation states no expected output for this example.
my @pieces;
for 60, 60, 24 -> $divisor {
@pieces.push: $seconds mod $divisor;
$seconds div= $divisor
}
@pieces.push: $seconds;Not executed: the documentation states no expected output for this example.
say @pieces; # OUTPUT: «[5 4 3 2]»
Neither engine can run this in isolation — the example depends on context from the surrounding text.
my $some-numbers = lazy gather { take 3*$_ for 1..3 };
say 600.polymod( $some-numbers ); # OUTPUT: «(0 2 6 3)»(0 2 6 3)
Documentation, Rakudo and Raku++ all agree.
say 2.is-prime; # OUTPUT: «True» say is-prime(9); # OUTPUT: «False»
True
False
Documentation, Rakudo and Raku++ all agree.
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»
0
1
2
3
4
Documentation, Rakudo and Raku++ all agree.
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»
0
1
2
3
4
Documentation, Rakudo and Raku++ all agree.
say ord("¾").unival; # OUTPUT: «0.75»
say 190.unival; # OUTPUT: «0.75»
say unival(65); # OUTPUT: «NaN»0.75
0.75
NaN
Documentation, Rakudo and Raku++ all agree.