Now that Math::BigInt mostly works (still no negative numbers, alas), I thought I’d take a stab at FatRat. My first step (after creating Math-FatRat on github) was to copy the source code for Rat. After all, FatRat is mostly the same thing, only with Math::BigInt instead of Int.

The first method is pretty straightforward to port:

multi method new() {
self.bless(*, :numerator(0), :denominator(1));
}

becomes

multi method new() {
self.bless(*, :numerator(0L), :denominator(1L));
}

Yup, that’s simple.

And at that point, I wanted to calculate gcd, and the best way to do that was to get a function from Math::BigInt, since Math::BigInt can call directly into the BigDigits library. While I waited for TimToady to give me permission to add a gcd function to Perl 6, I looked for another way to create useful FatRat objects. And hey, it occurred to me that you might well want to create a FatRat from a normal Rat. So…

multi method new(Rat $r) {
self.bless(*,
:numerator(Math::BigInt.new($r.numerator)),
:denominator(Math::BigInt.new($r.denominator)));
}

I did eventually get permission for gcd, and while I haven’t added it to Rakudo or the spec yet, I did add it to Math::BigInt so I could use it here. Plowing on from there it was simple to get to `infix:<+>`

. And that’s where the real trouble started.

So, while working on Math::BigInt, I discovered that I couldn’t declare the arithmetic operators to be “our”, else they would block the visibility of Rakudo’s basic arithmetic operators. (Pretty sure that’s a bug.)

But the situation seems to be even worse with Math::FatRat. If I define

class Math::FatRat {
multi sub infix:<+>(Math::FatRat $a, Math::FatRat $b) is export(:DEFAULT)
}

I get this error when I “use Math::FatRat;”

===SORRY!===
Can't import symbol &infix:<+> because it already exists in this lexical scope

That happens whether or not I “use Math::BigInt;” in the same file. Adding “our” to the definition doesn’t change anything. Again, Rakudo bug, I think.

I have no idea how to work around this. Any suggestions (or bug fixes) would be very welcome.

In the meantime, I’ve named it `infix:<FR+>`

, just so I can get practice writing it. But that is the subject for another post.

### Like this:

Like Loading...

*Related*

This entry was posted on February 6, 2011 at 3:02 am and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

February 6, 2011 at 10:11 pm |

Shouldn’t it be called BigRat instead of FatRat, so that its name parallels the BigInt module that it’s based on?

February 6, 2011 at 10:25 pm |

FatRat is the name of Perl 6’s arbitrary int over arbitrary int rational type. This is an attempt to provide an implementation of that type as a stopgap until Perl 6 implementations start supporting it directly.