Rules / Types, classes & roles / basic

Real Reference

Non-complex number

What it is #

Position in the hierarchy #

Inherits from
Does
Consumed byInt, Num, Rational[::NuT,, Duration, Instant

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 Bridge #

method Bridge(Real:D:)

Default implementation coerces the invocant to Num and that's the behavior of this method in core Real types. This method primarily exist to make it easy to implement custom Real types by users, with the Bridge method returning one of the core Real types (NOT necessarily a Num) that best represent the custom Real type. In turn, this lets all the core operators and methods obtain a usable value they can work with.

method Complex #

method Complex(Real:D: --> Complex:D)

Returns Complex:D.

Converts the number to a Complex with the number converted to a Num as its real part and 0e0 as the imaginary part.

method Int #

method Int(Real:D:)

Calls the Bridge method on the invocant and then the Int method on its return value.

method Rat #

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

Calls the Bridge method on the invocant and then the Rat method on its return value with the $epsilon argument.

method Real #

multi method Real(Real:D: --> Real:D)
multi method Real(Real:U: --> Real:D)

Returns Real:D.

The :D variant simply returns the invocant. The :U variant issues a warning about using an uninitialized value in numeric context and then returns self.new.

method Str #

multi method Str(Real:D:)

Calls the Bridge method on the invocant and then the Str method on its return value.

method Num #

method Num(Real:D:)

Calls the Bridge method on the invocant and then the Num method on its return value.

routine rand #

sub term:<rand> (--> Num:D)
method rand(Real:D: --> Real:D)

Returns Num:D or Real:D.

Returns a pseudo-random number between zero (inclusive) and the number (non-inclusive). The Bridge method is used to coerce the Real to a numeric that supports rand method. The term form returns a pseudo-random Num between 0e0 (inclusive) and 1e0 (non-inclusive.)

method sign #

method sign(Real:D:)

Returns -1 if the number is negative, 0 if it is zero and 1 otherwise.

method round #

method round(Real:D: $scale = 1)

Rounds the number to scale $scale. If $scale is 1, rounds to an integer. If scale is 0.1, rounds to one digit after the radix point (period or comma), etc.

method floor #

method floor(Real:D: --> Int:D)

Returns Int:D.

Return the largest integer not greater than the number.

method ceiling #

method ceiling(Real:D: --> Int:D)

Returns Int:D.

Returns the smallest integer not less than the number.

method truncate #

method truncate(Real:D: --> Int:D)

Returns Int:D.

Rounds the number towards zero.

method polymod #

method polymod(Real:D: +@mods)

Returns the remainders after applying sequentially all divisors in the @mods argument; the last element of the array will be the last remainder. 10 xx 8 is simply an array with eight number 10s; the first division by 10 will return 1 as a remainder, while the rest, up to the last, will return 0. With 8 divisors, as above, the result will have one more elements, in this case for the last remainder.

method base #

method base(Real:D: Int:D $base where 2..36, $digits? --> Str:D)

Returns Str:D.

Converts the number to a string, using $base as base. For $base larger than ten, capital Latin letters are used. The optional $digits argument asks for that many digits of fraction (which may not be negative). If omitted, a reasonable default is chosen based on type. For Int this default is 0. For Num, the default is 8. For Rational, the number of places is scaled to the size of the

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 · 5 no-output · 1 not-runnable · 2 ok

role Real does Numeric { ... }

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

class Temperature is Real {
    has Str:D  $.unit  is required where any <K F C>;
    has Real:D $.value is required;
    method new ($value, :$unit = 'K') { self.bless :$value :$unit }
    # Note: implementing .new() that handles $value of type Temperature is left as an exercise

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

    method Bridge {
        when $!unit eq 'F' { ($!value + 459.67) × 5/9 }
        when $!unit eq 'C' {  $!value + 273.15 }
        $!value
    }
    method gist { self.Str }
    method Str  { "$!value degrees $!unit" }
}

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

sub postfix:<℃> { Temperature.new: $^value, :unit<C> }
sub postfix:<℉> { Temperature.new: $^value, :unit<F> }
sub postfix:<K> { Temperature.new: $^value, :unit<K> }

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

my $human := 36.6℃;
my $book  := 451℉;
my $sun   := 5778K;
say $human;                # OUTPUT: «36.6 degrees C␤»
say $human + $book + $sun; # OUTPUT: «6593.677777777778␤»
say 123K + 456K;           # OUTPUT: «579␤»
Documentation
36.6 degrees C
6593.677777777778
579
Rakudo
Raku++
36.6
6265.6
123

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 (1e8+1).polymod(10 xx 8);  # OUTPUT: «(1 0 0 0 0 0 0 0 1)␤»
Output
(1 0 0 0 0 0 0 0 1)

Documentation, Rakudo and Raku++ all agree.

say ⅔.polymod(⅓);                            # OUTPUT: «(0 2)␤»
say 5.Rat.polymod(.3, .2);                   # OUTPUT: «(0.2 0 80)␤»
Output
(0 2)
(0.2 0 80)

Documentation, Rakudo and Raku++ all agree.

255.base(16);            # 'FF'

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

say pi.base(10, 3);      # OUTPUT: «3.142␤»
say (1/128).base(10, *); # OUTPUT: «0.0078125␤»
say (1/100).base(10, *); # OUTPUT: «0.01␤»
say (1/3)  .base(10, *); # WRONG: endlessly repeating fractional part

Neither engine can run this in isolation — the example depends on context from the surrounding text.