diff options
author | Eduardo Julian | 2016-12-02 21:46:23 -0400 |
---|---|---|
committer | Eduardo Julian | 2016-12-02 21:46:23 -0400 |
commit | 6dc77468ed29dde0f83a73661bae0fda02de7aed (patch) | |
tree | 7f7855f53a48c0b44c01ef5f4d570c38ae3b2454 | |
parent | 9f237c5c5a12dc4aa3b4fbd486586050bf84b2b8 (diff) |
- gcd and lcm functions now work on Nat, instead of Int.
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/math.lux | 20 | ||||
-rw-r--r-- | stdlib/source/lux/math/ratio.lux | 3 |
2 files changed, 10 insertions, 13 deletions
diff --git a/stdlib/source/lux/math.lux b/stdlib/source/lux/math.lux index 1ed87f1e8..12c32b853 100644 --- a/stdlib/source/lux/math.lux +++ b/stdlib/source/lux/math.lux @@ -76,26 +76,22 @@ [pow "invokestatic:java.lang.Math:pow:double,double"] ) -(def: (gcd' a b) - (-> Int Int Int) - (case b - 0 a - _ (gcd' b (i.% b a)))) - (def: #export (gcd a b) {#;doc "Greatest Common Divisor."} - (-> Int Int Int) - (gcd' (Int/abs a) (Int/abs b))) + (-> Nat Nat Nat) + (case b + +0 a + _ (gcd b (n.% b a)))) (def: #export (lcm x y) {#;doc "Least Common Multiple."} - (-> Int Int Int) + (-> Nat Nat Nat) (case [x y] - (^or [_ 0] [0 _]) - 0 + (^or [_ +0] [+0 _]) + +0 _ - (|> x (i./ (gcd x y)) (i.* y) Int/abs) + (|> x (n./ (gcd x y)) (n.* y)) )) ## [Syntax] diff --git a/stdlib/source/lux/math/ratio.lux b/stdlib/source/lux/math/ratio.lux index 5fb82c8a5..c2c9e7183 100644 --- a/stdlib/source/lux/math/ratio.lux +++ b/stdlib/source/lux/math/ratio.lux @@ -24,7 +24,8 @@ (def: #hidden (normalize (^slots [#numerator #denominator])) (-> Ratio Ratio) - (let [common (math;gcd numerator denominator) + (let [common (nat-to-int (math;gcd (int-to-nat (i:abs numerator)) + (int-to-nat (i:abs denominator)))) numerator (i./ common numerator) denominator (i./ common denominator)] {#numerator (if (and (i.< 0 numerator) |