Balanced ternary: Difference between revisions

m
m (→‎{{header|ALGOL 68}}: add addition and miltiplication)
m (→‎{{header|Perl 6}}: various fixes)
Line 2,902:
</pre>
=={{header|Perl 6}}==
{{worksWorks with|rakudo|20122016-03-1011}}
<lang perl6>class BT {
has @.coeff;
Line 2,910:
 
multi method new (Str $s) {
self.bless(*, coeff => %bt2co{$s.flip.comb});
}
multi method new (Int $i where $i >= 0) {
self.bless(*, coeff => carry $i.base(3).comb.reverse);
}
multi method new (Int $i where $i < 0) {
Line 2,940:
multi infix:<+> (BT $x, BT $y) {
my ($b,$a) = sort +*.coeff, $x, $y;
BT.new: coeff => carry ($a.coeff Z+ |$b.coeff, |(0 xx *$a.coeff - $b.coeff));
}
 
Line 2,951:
my @safe;
for @x -> $xd {
@z = @z Z+ |(@y X* $xd), |(0 xx *@z-@y);
@safe.push: @z.shift;
}
2,392

edits