class Num is Cool does Real { }

A Num object stores a floating-point number. It is immutable. On most platforms, it's an IEEE 754 64-bit floating point numbers, aka "double precision".

Inf§

The value Inf is an instance of Num and represents value that's too large to represent in 64-bit double-precision floating point number (roughly, above 1.7976931348623158e308 for positive Inf and below -1.7976931348623157e308 for negative Inf) as well as returned from certain operations as defined by the IEEE 754-2008 standard.

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

The U+221E Unicode character can be used instead of the word Inf and can be handy when Inf would otherwise require an unspace, such as when writing Complex numbers:

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

Note that there are just two infinities (positive and negative), so even if an operation that would instinctively give a "larger" infinity is performed, the result in still an infinity of the original magnitude. The infinities can be compared, operated and used as an argument as if they were simply a number that's too big to represent or to signify "without bounds" or limits:

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)

In some cases, it's used as an implicit value to represent "all of them"

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

In the example above, Inf can be eliminated, since it's the default value for the second argument of .comb, used to indicate how many parts should be returned.

Division of an infinity by another infinity results in a NaN:

say/∞;             # OUTPUT: «NaN␤»

NaN§

The value NaN is an instance of Num and represents a floating point not-a-number value, which is returned from some routines where a concrete number as the answer is not defined, but a Numeric value is still acceptable. NaN is defined and boolifies to True, but is not numerically equal to any value (including itself).

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

To test for NaN, use isNaN method or === operator:

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

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.

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

method rand§

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

Returns a pseudo random number between 0 and the invocant.

sub srand§

sub srand(Int $seed --> 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.

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

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

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

method Bridge§

method Bridge(Num:D:)

Returns the number.

Typegraph§

Type relations for Num
raku-type-graph Num Num Cool Cool Num->Cool Real Real Num->Real Mu Mu Any Any Any->Mu Cool->Any Numeric Numeric Real->Numeric Stringy Stringy Str Str Str->Cool Str->Stringy Allomorph Allomorph Allomorph->Str NumStr NumStr NumStr->Num NumStr->Allomorph

Expand chart above