Category talk:Wren-rat: Difference between revisions

→‎Source code: Various changes or additions to make the Rat class more consistent with the new BigRat class.
(→‎Source code: Typo in comment.)
(→‎Source code: Various changes or additions to make the Rat class more consistent with the new BigRat class.)
Line 1:
===Source code===
 
<lang ecmascript>/* Module "rat.wren" */
 
Line 17 ⟶ 16:
if (o is Rat) return o
if (o is Num) return Rat.fromFloat(o)
if (o is String) return Rat(o.fromStringcontains("_") && o.contains("/")) ? fromMixedString(o) :
o.contains("/") ? fromRationalString(o) : fromString(o)
Fiber.abort("Argument must either be a rational number, a number or a numeric string.")
}
Line 42:
 
// Constants.
static zero minusOne { Rat.new( 0-1, 1) }
static onezero { Rat.new( 1 0, 1) }
static twoone { Rat.new( 2 1, 1) }
static tentwo { Rat.new(10 2, 1) }
static halften { Rat.new( 110, 21) }
static tenthhalf { Rat.new( 1, 10 2) }
static tenth { Rat.new( 1, 10) }
 
// Constructs a new Rat object by passing it a numerator and a denominator.
Line 71 ⟶ 72:
_d = d
}
 
// Convenience method which constructs a new Rat object by passing it just a numerator.
static new(n) { Rat.new(n, 1) }
 
// Constructs a rational number from an integer.
Line 92 ⟶ 96:
 
// Constructs a rational number from a string of the form "n/d".
// Improper fractions are allowed.
static fromRationalString(s) {
s = s.trim()
Line 101 ⟶ 106:
return Rat.new(n, d)
}
 
// Constructs a rational number from a string of the form "i_n/d" where 'i' is an integer.
// Improper and negative fractional parts are allowed.
static fromMixedString(s) {
var ind = s.split("_")
if (ind.count != 2) Fiber.abort("Argument is not a suitable string.")
var nd = fromRationalString(ind[1])
var i = Rat.fromString(ind[0])
var neg = i.isNegative || (i.isZero && ind[0][0] == "-")
return neg ? i - nd : i + nd
}
 
// Returns the greater of two rational numbers.
static max(r1, r2) { (r1 < r2) ? r2 : r1 }
 
// Returns the smaller of two rational numbers.
static min(r1, r2) { (r1 < r2) ? r1 : r2 }
 
// Private helper method to compare two integers.
static compareInts_(i, j) { (i - j).sign }
 
// Determines whether a Rat object is always shown as such or, if integral, as an integer.
Line 109 ⟶ 134:
num { _n } // numerator
den { _d } // denominator
ratio { [_n, _d] } // a two element list of the above
float { _n/_d } // converts to a Num
isInteger { float.isInteger } // checks if integral or not
isPositive { _n > 0 } // checks if positive
isNegative { _n < 0 } // checks if negative
isUnit { _n.abs == 1 } // checks if plus or minus one
floatisZero { _n/_d } == 0 } // converts tochecks aif Numzero
 
// Rounding methods (similar to those in Num class).
ceil { Rat.fromInt(floattoFloat.ceil) } // higher integer
floor { Rat.fromInt(floattoFloat.floor) } // lower integer
truncate { Rat.fromInt(floattoFloat.truncate) } // lower integer, towards zero
round { Rat.fromInt(floattoFloat.round) } // nearer integer
fraction { this - truncate } // fractional part (same sign as this.num)
 
Line 133 ⟶ 161:
-(o) { (o = Rat.check_(o)) && (this + (-o)) }
*(o) { (o = Rat.check_(o)) && Rat.new(_n * o.num, _d * o.den) }
/(o) { (o = Rat.check_(o)) && Rat.new(this_n * o.den, _d * o.inversenum) }
%(o) { (o = Rat.check_(o)) && (this/o - idiv(o) * o) }
 
// Computes integral powers.
pow(i) {
if (!((i is Num) && i.isInteger)) Fiber.abort("Argument must be an integeinteger.r")
if (i == 0) return this
var np = _n.pow(i).round
Line 144 ⟶ 172:
return (i > 0) ? Rat.new(np, dp) : Rat.new(dp, np)
}
 
// Returns the square of the current instance.
square { Rat.new(_n * _n , _d *_d) }
 
// Other methods.
Line 156 ⟶ 187:
// Compares this Rat with another one to enable comparison operators via Comparable trait.
compare(other) { (float - other.float).sign }
if ((other is Num) && other.isInfinity) return -other.sign
other = Rat.check_(other)
if (_d == other.den) return Rat.compareInts_(_n, other.num)
return Rat.compareInts_(_n * other.den, other.num * _d)
}
 
// As above but compares the absolute values of the BigRats.
compareAbs(other) { this.abs.compare(other.abs) }
 
// Converts the current instance to a Num.
toFloat { _n/_d }
 
// Converts the current instance to an integer with any fractional part truncated.
toInt { this.toFloat.truncate }
 
// Returns a string represenation of this instance in the form "i_n/d" where 'i' is an integer.
toMixedString {
var q = _n / _d
var r = _n % _d
if (r.isNegative) r = -r
return q.toString + "_" + r.toString + "/" + _d.toString
}
 
// Returns the string representation of this Rat object depending on 'showAsInt'.
9,482

edits