Category talk:Wren-rat: Difference between revisions

m
→‎Source code: Now uses Wren S/H lexer.
(→‎Source code: Harmonization of constants between numeric modules.)
m (→‎Source code: Now uses Wren S/H lexer.)
 
(5 intermediate revisions by the same user not shown)
Line 1:
===Source code===
<syntaxhighlight lang="ecmascriptwren">/* Module "rat.wren" */
 
import "./trait" for Comparable
Line 158:
truncate { Rat.fromInt(toFloat.truncate) } // lower integer, towards zero
round { Rat.fromInt(toFloat.round) } // nearer integer
roundUp { this >= 0 ? ceil : floor } // higher integer, away from zero
fraction { this - truncate } // fractional part (same sign as this.num)
 
Line 179 ⟶ 180:
pow(i) {
if (!((i is Num) && i.isInteger)) Fiber.abort("Argument must be an integer.")
if (i == 0) return thisRat.copy()one
varif np(i == _n1) return this.powcopy(i).round
varif dp(i == _d.pow(i-1) return this.roundinverse
var np = _n.pow(i.abs).round
var dp = _d.pow(i.abs).round
return (i > 0) ? Rat.new(np, dp) : Rat.new(dp, np)
}
Line 206 ⟶ 209:
}
 
// As above but compares the absolute values of the BigRatsRats.
compareAbs(other) { this.abs.compare(other.abs) }
 
Line 218 ⟶ 221:
toMixedString {
var q = _n / _d
var rsign = _nq %< _d0 ? "-" : ""
if (r.isNegative) rq = -rq.abs.truncate
returnvar q.toStringr += "_" + r_n.toStringabs + "/" +% _d.toString
return sign + q.toString + "_" + r.toString + "/" + _d.toString
}
 
9,476

edits