Search code examples
numericrakufloor

perl6 Function floor works differently with numbers and strings?


Quick question:

-1.9.floor gives you -1, while "-1.9".floor gives you -2. Is it supposed to be so? Seems a bit inconsistent to me.

> say -1.9.floor
-1
> say "-1.9".floor
-2

Documentation says "rounds it downwards to the nearest integer". Should both be -2?

Thanks!!!


Solution

  • Seems to be some operator precedence. Make the number a variable and use floor on the variable it looks ok.

    my $i = -1.9;
    say $i.floor; #-2 
    

    I your example it makes: .9.floor ==> 0 and then -1.0 ==> -1