From 6dc77468ed29dde0f83a73661bae0fda02de7aed Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 2 Dec 2016 21:46:23 -0400 Subject: - gcd and lcm functions now work on Nat, instead of Int. --- stdlib/source/lux/math.lux | 20 ++++++++------------ stdlib/source/lux/math/ratio.lux | 3 ++- 2 files changed, 10 insertions(+), 13 deletions(-) (limited to 'stdlib') 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) -- cgit v1.2.3