Category talk:Wren-math: Difference between revisions
Content added Content deleted
(Added evalPoly and diffPoly methods to Math class.) |
(→Source code: Added various integer root functions..) |
||
Line 153: | Line 153: | ||
static mod(x, y) { ((x % y) + y) % y } |
static mod(x, y) { ((x % y) + y) % y } |
||
// Returns |
// Returns the integer square root of 'x' or null if 'x' is negative. |
||
static |
static sqrt(x) { (x >= 0) ? x.sqrt.floor : null } |
||
⚫ | |||
⚫ | |||
⚫ | |||
// Returns |
// Returns the integer cube root of 'x'. |
||
static |
static cbrt(x) { x.cbrt.truncate } |
||
⚫ | |||
// Returns the integer 'n'th root of 'x' or null if 'x' is negative and 'n' is even. |
|||
⚫ | |||
static root(n, x) { |
|||
if (!(n is Num) || !n.isInteger || n < 1) { |
|||
Fiber.abort("n must be a positive integer.") |
|||
} |
|||
return (n == 1) ? x : |
|||
(n == 2) ? sqrt(x) : |
|||
(n == 3) ? cbrt(x) : |
|||
(n % 2 == 1) ? x.sign * x.abs.pow(1/n).floor : |
|||
(n >= 0) ? x.pow(1/n).floor : null |
|||
⚫ | |||
// Returns whether or not 'x' is a perfect square. |
|||
static isSquare(x) { |
|||
⚫ | |||
⚫ | |||
} |
|||
// Returns whether or not 'x' is a perfect cube. |
|||
static isCube(x) { |
|||
⚫ | |||
⚫ | |||
} |
|||
// Returns whether or not 'x' is a perfect 'n'th power. |
|||
static isRoot(n, x) { |
|||
var r = root(n, x) |
|||
return r.pow(n) == x |
|||
} |
} |
||