Rules / Types, classes & roles / basic

Num Reference

Floating-point number

What it is #

Position in the hierarchy #

Inherits from
Does
Inherited byNumStr

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()
multi method new($n)

Num.new without argument will create a Num with the value 0e0. With an argument, it will be coerced to Num and then returned.

method rand #

method rand(Num:D: --> Num)

Returns Num.

Returns a pseudo random number between 0 and the invocant.

sub srand #

sub srand(Int $seed --> Int:D)

Returns Int:D.

Seeds the pseudo random number generator used by Num.rand with the provided value. Note that srand is called with a platform dependent value when a Raku program is started.

method Capture #

method Capture()

Throws X::Cannot::Capture.

method Int #

method Int(Num:D:)

Converts the number to an Int. Fails with X::Numeric::CannotConvert if the invocant is a NaN or Inf/-Inf. No rounding is performed.

method Rat #

method Rat(Num:D: Real $epsilon = 1e-6)

Converts the number to a Rat with $epsilon precision. If the invocant is an Inf, -Inf, or NaN, converts them to a Rat with 0 denominator and 1, -1, or 0 numerator, respectively.

method FatRat #

method FatRat(Num:D: Real $epsilon = 1e-6)

Converts the number to a FatRat with the precision $epsilon. If invocant is an Inf, -Inf, or NaN, converts them to a FatRat with 0 denominator and 1, -1, or 0 numerator, respectively.

method Num #

method Num()

Returns the invocant.

method Str #

method Str(Int:D)

Returns a string representation of the number. Cool being a parent class of Num, an explicit call to the Num.Str method is seldom needed.

method Bridge #

method Bridge(Num:D:)

Returns the number.

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 doc-drift · 2 no-output · 8 ok

class Num is Cool does Real { }

Not executed: the documentation states no expected output for this example.

say 2e300 ** 2e300; # OUTPUT: «Inf␤»
say (-1/0).Num;     # OUTPUT: «-Inf␤»
Output
Inf
-Inf

Documentation, Rakudo and Raku++ all agree.

say Inf+Inf\i; # Backslash (unspace) before `i` required
say ∞+∞i;      # No backslash is needed

Not executed: the documentation states no expected output for this example.

say ∞²;                       # OUTPUT: «Inf␤»
say 42 + Inf === ∞;           # OUTPUT: «True␤»
say atan ∞;                   # OUTPUT: «1.5707963267949␤»
say -∞ < 42 < ∞;              # OUTPUT: «True␤»
my  $l := 1, 2, 4, 8 ... Inf; # Infinite sequence (no limits)
Documentation
Inf
True
1.5707963267949
True
Rakudo
Inf
True
1.5707963267948966
True
Raku++
Inf
True
1.5707963267948966
True

Both engines agree; the documentation states something else. Trust the engines.

say "House of M".comb(3,Inf).join("←X→");
# OUTPUT: «Hou←X→se ←X→of ←X→M␤»
Output
Hou←X→se ←X→of ←X→M

Documentation, Rakudo and Raku++ all agree.

say ∞/∞;             # OUTPUT: «NaN␤»
Output
NaN

Documentation, Rakudo and Raku++ all agree.

say cos ∞;     # OUTPUT: «NaN␤»
say (0/0).Num; # OUTPUT: «NaN␤»
Output
NaN
NaN

Documentation, Rakudo and Raku++ all agree.

say (0/0).isNaN;       # OUTPUT: «True␤»
say (0/0).Num === NaN; # OUTPUT: «True␤»
Output
True
True

Documentation, Rakudo and Raku++ all agree.

say Num.new(⅓); # OUTPUT: «0.3333333333333333␤»
Output
0.3333333333333333

Documentation, Rakudo and Raku++ all agree.

say π.Str;                # OUTPUT: «3.141592653589793␤»
Output
3.141592653589793

Documentation, Rakudo and Raku++ all agree.

say π.Str.comb == π.comb; # OUTPUT: «True␤»
Output
True

Documentation, Rakudo and Raku++ all agree.