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="
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
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
compareAbs(other) { this.abs.compare(other.abs) }
Line 218 ⟶ 221:
toMixedString {
var q = _n / _d
var
return sign + q.toString + "_" + r.toString + "/" + _d.toString
}
|