09d755c Updates Rational role's methods with latest nom changes. kboga authored May 15, 2012 1 # XXX: should be Rational[Int, UInt64] 63875df Remove trailing whitespace lizmat authored Dec 30, 2014 2 my class Rat is Cool does Rational[Int, Int] { 85196e7 conversions between Rat and FatRat moritz authored Feb 13, 2012 3 method Rat (Rat:D: Real $?) { self } c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 25, 2015 4 method FatRat(Rat:D: Real$?) { FatRat.new($!numerator,$!denominator); } 21f01b0 Add .Range "coercer" for Num/Rat types lizmat authored Mar 16, 2015 5 method Range(Rat:U:) { Range.new(-Inf,Inf) } 5b9e9af separate FatRat.perl, fix Rational.Str moritz authored Feb 13, 2012 6 multi method perl(Rat:D:) { 689d079 Streamline some Rats lizmat authored May 4, 2016 7 if $!denominator == 1 { 8$!numerator ~ '.0' 9 } 10 else { 11 my $d =$!denominator; 12 unless $d == 0 { 13$d = $d div 5 while$d %% 5; 14 $d =$d div 2 while $d %% 2; 15 self.REDUCE-ME; 16 } 17$d == 1 18 ?? self.base(10,*) 19 !! '<' ~ $!numerator ~ '/' ~$!denominator ~ '>' 0506d2a don't infiniloop when trying to .perl a Rat with 0 denominator timo authored Jun 19, 2013 20 } 5b9e9af separate FatRat.perl, fix Rational.Str moritz authored Feb 13, 2012 21 } 85196e7 conversions between Rat and FatRat moritz authored Feb 13, 2012 22 } fd27043 Have Rat.perl return decimal numbers if denominator is normalizable t… pmichaud authored Mar 3, 2013 23 8f76180 Merge branch 'Rational' into nom kboga authored May 15, 2012 24 my class FatRat is Cool does Rational[Int, Int] { 85196e7 conversions between Rat and FatRat moritz authored Feb 13, 2012 25 method FatRat(FatRat:D: Real $?) { self } 26 method Rat (FatRat:D: Real$?) { c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 27 $!denominator <$UINT64_UPPER 689d079 Streamline some Rats lizmat authored May 4, 2016 28 ?? Rat.new($!numerator,$!denominator) 29 !! Failure.new("Cannot convert from FatRat to Rat because denominator is too big") 85196e7 conversions between Rat and FatRat moritz authored Feb 13, 2012 30 } 5b9e9af separate FatRat.perl, fix Rational.Str moritz authored Feb 13, 2012 31 multi method perl(FatRat:D:) { c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 32 "FatRat.new($!numerator,$!denominator)"; 5b9e9af separate FatRat.perl, fix Rational.Str moritz authored Feb 13, 2012 33 } 85196e7 conversions between Rat and FatRat moritz authored Feb 13, 2012 34 } 2685a77 preliminary work to get both Rat and FatRat working moritz authored Feb 12, 2012 35 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 36 sub DIVIDE_NUMBERS(Int:D \nu, Int:D \de, $t1,$t2) { 442a567 Preserve numerator if denominator is 0 lizmat authored Jun 7, 2015 37 my Int $gcd := de == 0 ?? 1 !! nu gcd de; a3cf223 Avoid scalar allocations in DIVIDE_NUMBERS moritz authored Nov 24, 2014 38 my Int$numerator := nu div $gcd; 39 my Int$denominator := de div $gcd; c238291 make DIVIDE_NUMBERS friendlier to block inlining timo authored Nov 24, 2014 40 my$r; 2685a77 preliminary work to get both Rat and FatRat working moritz authored Feb 12, 2012 41 if $denominator < 0 { a3cf223 Avoid scalar allocations in DIVIDE_NUMBERS moritz authored Nov 24, 2014 42$numerator := -$numerator; 43$denominator := -$denominator; 2685a77 preliminary work to get both Rat and FatRat working moritz authored Feb 12, 2012 44 } ddd1f02 fix thinko in DIVIDE_NUMBERS moritz authored Feb 13, 2012 45 if nqp::istype($t1, FatRat) || nqp::istype($t2, FatRat) { c238291 make DIVIDE_NUMBERS friendlier to block inlining timo authored Nov 24, 2014 46$r := nqp::create(FatRat); f19881b Eliminate nqp::p6decont for plain nqp::decont. jnthn authored Mar 19, 2013 47 nqp::bindattr($r, FatRat, '$!numerator', nqp::decont($numerator)); 48 nqp::bindattr($r, FatRat, '$!denominator', nqp::decont($denominator)); f27eab1 thinkos, syntax errors moritz authored Feb 12, 2012 49 } elsif $denominator <$UINT64_UPPER { c238291 make DIVIDE_NUMBERS friendlier to block inlining timo authored Nov 24, 2014 50 $r := nqp::create(Rat); f19881b Eliminate nqp::p6decont for plain nqp::decont. jnthn authored Mar 19, 2013 51 nqp::bindattr($r, Rat, '$!numerator', nqp::decont($numerator)); 52 nqp::bindattr($r, Rat, '$!denominator', nqp::decont($denominator)); 2685a77 preliminary work to get both Rat and FatRat working moritz authored Feb 12, 2012 53 } else { c238291 make DIVIDE_NUMBERS friendlier to block inlining timo authored Nov 24, 2014 54$r := nqp::p6box_n(nqp::div_In( f19881b Eliminate nqp::p6decont for plain nqp::decont. jnthn authored Mar 19, 2013 55 nqp::decont($numerator), 56 nqp::decont($denominator) 7f1a890 some small fixes, plus Num fallback for Rats moritz authored Feb 12, 2012 57 ) 58 ); 2685a77 preliminary work to get both Rat and FatRat working moritz authored Feb 12, 2012 59 } c238291 make DIVIDE_NUMBERS friendlier to block inlining timo authored Nov 24, 2014 60 $r; 2685a77 preliminary work to get both Rat and FatRat working moritz authored Feb 12, 2012 61 } 62 c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 63 sub DON'T_DIVIDE_NUMBERS(Int:D \nu, Int:D \de,$t1, $t2) { 64 my$r; 65 if nqp::istype($t1, FatRat) || nqp::istype($t2, FatRat) { 66 $r := nqp::create(FatRat); 67 nqp::bindattr($r, FatRat, '$!numerator', nqp::decont(nu)); 68 nqp::bindattr($r, FatRat, '$!denominator', nqp::decont(de)); 69 } else { 70$r := nqp::create(Rat); 71 nqp::bindattr($r, Rat, '$!numerator', nqp::decont(nu)); 72 nqp::bindattr($r, Rat, '$!denominator', nqp::decont(de)); 73 } 74 $r; 75 } 76 310efb2 multi -> multi sub for easier grep/ackability lizmat authored Dec 30, 2014 77 multi sub prefix:<->(Rat:D \a) { 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 78 Rat.new(-a.numerator, a.denominator); 723859b backport Rat operators from master moritz authored Jun 11, 2011 79 } 310efb2 multi -> multi sub for easier grep/ackability lizmat authored Dec 30, 2014 80 multi sub prefix:<->(FatRat:D \a) { 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 81 FatRat.new(-a.numerator, a.denominator); 4ab4ffa start to switch some arithmetic ops to smart Rat/FatRat semantics moritz authored Feb 12, 2012 82 } 83 310efb2 multi -> multi sub for easier grep/ackability lizmat authored Dec 30, 2014 84 multi sub infix:<+>(Rational \a, Rational \b) { c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 85 if a.denominator == b.denominator { 86 DON'T_DIVIDE_NUMBERS(a.numerator + b.numerator, a.denominator, a, b); 87 } 88 else { 89 my Int$gcd := a.denominator gcd b.denominator; 90 DIVIDE_NUMBERS( 91 (a.numerator * (b.denominator div $gcd) + b.numerator * (a.denominator div$gcd)), 92 ((a.denominator div $gcd) * b.denominator), 93 a, 94 b, 95 ); 96 } 4ab4ffa start to switch some arithmetic ops to smart Rat/FatRat semantics moritz authored Feb 12, 2012 97 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 98 multi sub infix:<+>(Rational \a, Int \b) { c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 99 DON'T_DIVIDE_NUMBERS( 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 100 (a.numerator + b * a.denominator), 101 a.denominator, 102 a, 103 b, 4ab4ffa start to switch some arithmetic ops to smart Rat/FatRat semantics moritz authored Feb 12, 2012 104 ); 105 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 106 multi sub infix:<+>(Int \a, Rational \b) { c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 107 DON'T_DIVIDE_NUMBERS( 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 108 (a * b.denominator + b.numerator), 109 b.denominator, 110 a, 111 b, 4ab4ffa start to switch some arithmetic ops to smart Rat/FatRat semantics moritz authored Feb 12, 2012 112 ); 113 } 114 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 115 multi sub infix:<->(Rational \a, Rational \b) { c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 116 if a.denominator == b.denominator { 117 DON'T_DIVIDE_NUMBERS(a.numerator - b.numerator, a.denominator, a, b); 118 } 119 else { 120 my Int$gcd = a.denominator gcd b.denominator; 121 DIVIDE_NUMBERS 122 a.numerator * (b.denominator div $gcd) - b.numerator * (a.denominator div$gcd), 123 (a.denominator div \$gcd) * b.denominator, 124 a, 125 b; 126 } 4ab4ffa start to switch some arithmetic ops to smart Rat/FatRat semantics moritz authored Feb 12, 2012 127 } 128 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 129 multi sub infix:<->(Rational \a, Int \b) { c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 130 DON'T_DIVIDE_NUMBERS 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 131 a.numerator - b * a.denominator, 132 a.denominator, 133 a, 134 b; 723859b backport Rat operators from master moritz authored Jun 11, 2011 135 } 136 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 137 multi sub infix:<->(Int \a, Rational \b) { c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 138 DON'T_DIVIDE_NUMBERS 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 139 a * b.denominator - b.numerator, 140 b.denominator, 141 a, 142 b; 723859b backport Rat operators from master moritz authored Jun 11, 2011 143 } 144 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 145 multi sub infix:<*>(Rational \a, Rational \b) { d8d17cb switch the rest of the Rat ops to Rational moritz authored Feb 12, 2012 146 DIVIDE_NUMBERS 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 147 a.numerator * b.numerator, 148 a.denominator * b.denominator, 149 a, 150 b; 723859b backport Rat operators from master moritz authored Jun 11, 2011 151 } 152 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 153 multi sub infix:<*>(Rational \a, Int \b) { d8d17cb switch the rest of the Rat ops to Rational moritz authored Feb 12, 2012 154 DIVIDE_NUMBERS 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 155 a.numerator * b, 156 a.denominator, 157 a, 158 b; 723859b backport Rat operators from master moritz authored Jun 11, 2011 159 } 160 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 161 multi sub infix:<*>(Int \a, Rational \b) { d8d17cb switch the rest of the Rat ops to Rational moritz authored Feb 12, 2012 162 DIVIDE_NUMBERS 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 163 a * b.numerator, 164 b.denominator, 165 a, 166 b; 723859b backport Rat operators from master moritz authored Jun 11, 2011 167 } 168 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 169 multi sub infix:(Rational \a, Rational \b) { d8d17cb switch the rest of the Rat ops to Rational moritz authored Feb 12, 2012 170 DIVIDE_NUMBERS 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 171 a.numerator * b.denominator, 172 a.denominator * b.numerator, 173 a, 174 b; 723859b backport Rat operators from master moritz authored Jun 11, 2011 175 } 176 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 177 multi sub infix:(Rational \a, Int \b) { 125e7f9 more FatRat type fixes; run the tests! moritz authored Feb 13, 2012 178 DIVIDE_NUMBERS 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 179 a.numerator, 180 a.denominator * b, 181 a, 182 b; 723859b backport Rat operators from master moritz authored Jun 11, 2011 183 } 184 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 185 multi sub infix:(Int \a, Rational \b) { 8b4c2ab Fix RT #126391: [BUG] Bad "divide by 0" error message stmuk authored Oct 20, 2015 186 b.REDUCE-ME; # RT #126391: [BUG] Bad "divide by 0" error message d8d17cb switch the rest of the Rat ops to Rational moritz authored Feb 12, 2012 187 DIVIDE_NUMBERS 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 188 b.denominator * a, 189 b.numerator, 190 a, 191 b; 723859b backport Rat operators from master moritz authored Jun 11, 2011 192 } 193 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 194 multi sub infix:(Int \a, Int \b) { 195 DIVIDE_NUMBERS a, b, a, b 4db1c8a No need to move Rat to BOOTSTRAP moritz authored Jun 29, 2011 196 } 723859b backport Rat operators from master moritz authored Jun 11, 2011 197 beb41a0 Add Rational versions of infix:<%>. colomon authored Jan 9, 2015 198 multi sub infix:<%>(Rational \a, Int \b) { 199 a - floor(a / b) * b 200 } 201 202 multi sub infix:<%>(Int \a, Rational \b) { 203 a - floor(a / b) * b 204 } 205 206 multi sub infix:<%>(Rational \a, Rational \b) { 207 a - floor(a / b) * b 208 } 209 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 210 multi sub infix:<**>(Rational \a, Int \b) { 94eed8f Remove whitespace at EOL lizmat authored May 31, 2015 211 b >= 0 563abdd Fix Int/Rat ** -Int. colomon authored May 18, 2015 212 ?? DIVIDE_NUMBERS 5b8c080 better undef/overflow on Rat ** Int TimToady authored Dec 3, 2015 213 (a.numerator ** b // fail (a.numerator.abs > a.denominator ?? X::Numeric::Overflow !! X::Numeric::Underflow).new), 214 a.denominator ** b, # we presume it likely already blew up on the numerator 563abdd Fix Int/Rat ** -Int. colomon authored May 18, 2015 215 a, 216 b 217 !! DIVIDE_NUMBERS 56836b6 last patch had copy/paste error TimToady authored Dec 4, 2015 218 (a.denominator ** -b // fail (a.numerator.abs < a.denominator ?? X::Numeric::Overflow !! X::Numeric::Underflow).new), 563abdd Fix Int/Rat ** -Int. colomon authored May 18, 2015 219 a.numerator ** -b, 220 a, 221 b 723859b backport Rat operators from master moritz authored Jun 11, 2011 222 } 223 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 224 multi sub infix:<==>(Rational:D \a, Rational:D \b) { c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 225 a.numerator * b.denominator == b.numerator * a.denominator 3218a72 ==-compare rats directly moritz authored Apr 1, 2012 226 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 227 multi sub infix:<==>(Rational:D \a, Int:D \b) { c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 228 a.REDUCE-ME; 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 229 a.numerator == b && a.denominator == 1 3218a72 ==-compare rats directly moritz authored Apr 1, 2012 230 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 231 multi sub infix:<==>(Int:D \a, Rational:D \b) { c8ec299 speed up Rat addtion by reducing lazily TimToady authored Sep 26, 2015 232 b.REDUCE-ME; 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 233 a == b.numerator && b.denominator == 1; 3218a72 ==-compare rats directly moritz authored Apr 1, 2012 234 } 60c273d Add === fast-paths for Complex and Rational. jnthn authored Jun 29, 2015 235 multi sub infix:<===>(Rational:D \a, Rational:D \b) returns Bool:D { 236 a.WHAT =:= b.WHAT && a == b 237 } 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 238 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 239 multi sub infix:«<»(Rational:D \a, Rational:D \b) { 240 a.numerator * b.denominator < b.numerator * a.denominator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 241 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 242 multi sub infix:«<»(Rational:D \a, Int:D \b) { 243 a.numerator < b * a.denominator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 244 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 245 multi sub infix:«<»(Int:D \a, Rational:D \b) { 246 a * b.denominator < b.numerator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 247 } 248 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 249 multi sub infix:«<=»(Rational:D \a, Rational:D \b) { 250 a.numerator * b.denominator <= b.numerator * a.denominator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 251 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 252 multi sub infix:«<=»(Rational:D \a, Int:D \b) { 253 a.numerator <= b * a.denominator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 254 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 255 multi sub infix:«<=»(Int:D \a, Rational:D \b) { 256 a * b.denominator <= b.numerator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 257 } 258 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 259 multi sub infix:«>»(Rational:D \a, Rational:D \b) { 260 a.numerator * b.denominator > b.numerator * a.denominator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 261 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 262 multi sub infix:«>»(Rational:D \a, Int:D \b) { 263 a.numerator > b * a.denominator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 264 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 265 multi sub infix:«>»(Int:D \a, Rational:D \b) { 266 a * b.denominator > b.numerator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 267 } 268 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 269 multi sub infix:«>=»(Rational:D \a, Rational:D \b) { 270 a.numerator * b.denominator >= b.numerator * a.denominator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 271 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 272 multi sub infix:«>=»(Rational:D \a, Int:D \b) { 273 a.numerator >= b * a.denominator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 274 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 275 multi sub infix:«>=»(Int:D \a, Rational:D \b) { 276 a * b.denominator >= b.numerator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 277 } 278 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 279 multi sub infix:«<=>»(Rational:D \a, Rational:D \b) { 280 a.numerator * b.denominator <=> b.numerator * a.denominator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 281 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 282 multi sub infix:«<=>»(Rational:D \a, Int:D \b) { 283 a.numerator <=> b * a.denominator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 284 } 61d67fa use sigilless params in Rat moritz authored Aug 14, 2012 285 multi sub infix:«<=>»(Int:D \a, Rational:D \b) { 286 a * b.denominator <=> b.numerator 984dc7c comperator ops for rats and rats and ints moritz authored Apr 1, 2012 287 } 45e2308 tired of accidentally adding tabs to setting TimToady authored Mar 1, 2014 288 289 # vim: ft=perl6 expandtab sw=4