Created
December 29, 2016 16:15
-
-
Save zoffixznet/69d1e519c71a594288aea0ad6f55f564 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/core/Rat.pm b/src/core/Rat.pm | |
index ee4111f..7272747 100644 | |
--- a/src/core/Rat.pm | |
+++ b/src/core/Rat.pm | |
@@ -1,5 +1,42 @@ | |
# XXX: should be Rational[Int, UInt64] | |
my class Rat is Cool does Rational[Int, Int] { | |
+ method new(Int \nu = 0, Int \de = 1) { | |
+ nqp::stmts( | |
+ (my $new := nqp::create(self)), | |
+ nqp::if( | |
+ nqp::isfalse(de), | |
+ nqp::stmts( # 0 denominator take it verbatim to support | |
+ # Inf/-Inf/NaN | |
+ nqp::bindattr( | |
+ $new, self.WHAT, '$!numerator', nqp::decont(nu) | |
+ ), | |
+ nqp::bindattr( | |
+ $new, self.WHAT, '$!denominator', nqp::decont(de) | |
+ ), | |
+ ), | |
+ nqp::stmts( # normalize | |
+ (my $gcd := nqp::gcd_I(nu, de, Int)), | |
+ (my $numerator := nqp::div_I(nu, $gcd, Int)), | |
+ (my $denominator := nqp::div_I(de, $gcd, Int)), | |
+ nqp::if( | |
+ nqp::islt_I($denominator, 0), | |
+ nqp::stmts( | |
+ ($numerator := nqp::neg_I($numerator, Int)), | |
+ ($denominator := nqp::neg_I($denominator, Int)), | |
+ ), | |
+ ), | |
+ nqp::bindattr( | |
+ $new, self.WHAT, '$!numerator', nqp::decont($numerator) | |
+ ), | |
+ nqp::bindattr( | |
+ $new, self.WHAT, '$!denominator', nqp::decont($denominator) | |
+ ), | |
+ ) | |
+ ), | |
+ $new, | |
+ ); | |
+ } | |
+ | |
method Rat (Rat:D: Real $?) { self } | |
method FatRat(Rat:D: Real $?) { FatRat.new($!numerator, $!denominator); } | |
method Range(Rat:U:) { Range.new(-Inf,Inf,:excludes-min,:excludes-max) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment