Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rational numbers in Raku

Tags:

raku

I am using Raku for some computations, because it has nice numeric types. However, I have an issue with using '.raku'

say (1/6+1/6).raku
#<1/3>

We obtain this. However,

say (1/10+1/10).raku
#0.2

Is it a bug? I expected <1/5>. What happens?

like image 423
milou123 Avatar asked Mar 27 '20 23:03

milou123


2 Answers

In Raku, 0.2 constructs a Rat, and thus produces the very same result as writing 1/5 (which will be constant folded) or <1/5> (the literal form). You only get floating point in the case of specifying an exponent (for example, 2e-1).

The .raku (formerly known as the .perl) method's job is to produce something that will roundtrip and produce the same value if EVAL'd. In the case of 1/5, that can be exactly represented as a decimal, so it will produce 0.2. It only resorts to the fractional representation when a decimal form would not round-trip.

You can always recover the numerator and denominator using the .numerator and .denominator methods to format as you wish. Additionally .nude method returns a list of the numerator and denominator, which one can join with a / if wanted:

say (1/6+1/6).nude.join("/");     # 1/3
say (1/10+1/10).nude.join("/");   # 1/5
like image 175
Jonathan Worthington Avatar answered Oct 08 '22 19:10

Jonathan Worthington


Hi @milou123 I was also a bit surprised that raku reverts to decimal representation - I can see that some contexts - such as teaching fractional arithmetic would benefit from having a "keep as rat" mode. Having said that, ultimately it makes sense that there is only one way to .raku something and that decimal is the default representation.

Of course, with raku, you can also just change the language a bit. In this case, I have invented a new '→' postfix operator...

multi postfix:<→> ( Rat:D $r ) { $r.nude.join("/") }
say (1/5+1/5)→;    # 2/5

I am not smart enough to work out if the built in 'raku' method can be overridden in a similar way, would be keen to see advice on how to do that concisely...

like image 32
p6steve Avatar answered Oct 08 '22 17:10

p6steve