47 lines
1.1 KiB
Plaintext
47 lines
1.1 KiB
Plaintext
-- parent script "Frac"
|
|
property num
|
|
property denom
|
|
|
|
----------------------------------------
|
|
-- @constructor
|
|
-- @param {integer} numerator
|
|
-- @param {integer} [denominator=1]
|
|
----------------------------------------
|
|
on new (me, numerator, denominator)
|
|
if voidP(denominator) then denominator = 1
|
|
if denominator=0 then return VOID -- rule out division by zero
|
|
g = me._gcd(numerator, denominator)
|
|
if g<>0 then
|
|
numerator = numerator/g
|
|
denominator = denominator/g
|
|
else
|
|
numerator = 0
|
|
denominator = 1
|
|
end if
|
|
if denominator<0 then
|
|
numerator = -numerator
|
|
denominator = -denominator
|
|
end if
|
|
me.num = numerator
|
|
me.denom = denominator
|
|
return me
|
|
end
|
|
|
|
----------------------------------------
|
|
-- Returns string representation "<num>/<denom>"
|
|
-- @return {string}
|
|
----------------------------------------
|
|
on toString (me)
|
|
return me.num&"/"&me.denom
|
|
end
|
|
|
|
----------------------------------------
|
|
--
|
|
----------------------------------------
|
|
on _gcd (me, a, b)
|
|
if a = 0 then return b
|
|
if b = 0 then return a
|
|
if a > b then return me._gcd(b, a mod b)
|
|
return me._gcd(a, b mod a)
|
|
end
|