is Cool does Real
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 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␤»
say Inf+Inf\i; # Backslash (unspace) before `i` requiredsay ∞+∞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 := 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
say ∞/∞; # OUTPUT: «NaN␤»
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␤»
say (0/0).isNaN; # OUTPUT: «True␤»say (0/0).Num === NaN; # OUTPUT: «True␤»
multi method new()multi method new()
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(Num: --> Num)
Returns a pseudo random number between 0 and the invocant.
sub srand(Int --> Int)
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 Rat(Num: Real = 1e-6)
method FatRat(Num: Real = 1e-6)
Returns the invocant.
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␤»
Returns the number.