class Num

Floating-point number
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|Reference,NaN (definition) 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.

See Also

class int

Native integer

class Allomorph

Dual value number and string

class Any

Thing/object

class AST

Abstract representation of a piece of source code

class atomicint

Integer (native storage at the platform's atomic operation size)

class Block

Code object with its own lexical scope

class CallFrame

Captures the current frame state

class Code

Code object

class Collation

Encapsulates instructions about how strings should be sorted

class Compiler

Information related to the compiler that is being used

class Complex

Complex number

class ComplexStr

Dual value complex number and string

class Cool

Object that can be treated as both a string and number

class CurrentThreadScheduler

Scheduler that synchronously executes code on the current thread

class Date

Calendar date

class DateTime

Calendar date with time

class Distribution::Hash

Distribution::Hash

class Distribution::Locally

Distribution::Locally

class Distribution::Path

Distribution::Path

class Distribution::Resource

Every one of the resources installed with a distribution

class Duration

Length of time

class Encoding::Registry

Management of available encodings

class FatRat

Rational number (arbitrary-precision)

class ForeignCode

Rakudo-specific class that wraps around code in other languages (generally NQP)

class Format

Convert values to a string given a format specification

class Formatter

Produce Callable for given format specification

class HyperSeq

An object for performing batches of work in parallel with ordered output

class HyperWhatever

Placeholder for multiple unspecified values/arguments

class Instant

Specific moment in time

class Int

Integer (arbitrary-precision)

class IntStr

Dual value integer and string

class Junction

Logical superposition of values

class Label

Tagged location in the source code

class Lock::Async

A non-blocking, non-re-entrant, mutual exclusion lock

class Macro

Compile-time routine

class Method

Member function

class Mu

The root of the Raku type hierarchy.

class Nil

Absence of a value or a benign failure

role Numeric

Number or object that can act as a number

class NumStr

Dual value floating-point number and string

class ObjAt

Unique identification for an object

class Parameter

Element of a Signature

class Perl

Perl related information

class Proxy

Item container with custom storage and retrieval

class RaceSeq

Performs batches of work in parallel without respecting original order.

class Raku

Raku related information

package RakuAST

Namespace for holding RakuAST related classes

class RakuAST::Doc::Block

Contains the information of a RakuDoc block

class RakuAST::Doc::Declarator

Contains the declarator docs of a RakuAST object

class RakuAST::Doc::Markup

Contains the information about RakuDoc markup

class RakuAST::Doc::Paragraph

Contains the information about a RakuDoc paragraph

class Rat

Rational number (limited-precision)

class RatStr

Dual value rational number and string

class Routine

Code object with its own lexical scope and return handling

class Routine::WrapHandle

Holds all information needed to unwrap a wrapped routine.

class Scalar

A mostly transparent container used for indirections

class Signature

Parameter list pattern

class Str

String of characters

class StrDistance

Contains the result of a string transformation.

class Sub

Subroutine

class Submethod

Member function that is not inherited by subclasses

class Telemetry

Collect performance state for analysis

class Telemetry::Instrument::Thread

Instrument for collecting Thread data

class Telemetry::Instrument::ThreadPool

Instrument for collecting ThreadPoolScheduler data

class Telemetry::Instrument::Usage

Instrument for collecting getrusage data

class Telemetry::Period

Performance data over a period

class Telemetry::Sampler

Telemetry instrument pod

Subset UInt

Unsigned integer (arbitrary-precision)

class ValueObjAt

Unique identification for value types

class Variable

Object representation of a variable for use in traits

class Version

Module version descriptor

class Whatever

Placeholder for the value of an unspecified argument

class WhateverCode

Code object constructed by Whatever-priming

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.