Category talk:Wren-math: Difference between revisions

→‎Source code: Added various integer root functions..
(Added evalPoly and diffPoly methods to Math class.)
(→‎Source code: Added various integer root functions..)
Line 153:
static mod(x, y) { ((x % y) + y) % y }
 
// Returns whetherthe orinteger notsquare root of 'nx' isor anull perfectif square'x' is negative.
static isSquaresqrt(nx) { (x >= 0) ? x.sqrt.floor : null }
var s = n.sqrt.floor
return s * s == n
}
 
// Returns whetherthe orinteger notcube root of 'nx' is a perfect cube.
static isCubecbrt(nx) { x.cbrt.truncate }
 
var c = n.cbrt.truncate
// Returns the integer 'n'th root of 'x' or null if 'x' is negative and 'n' is even.
return c * c * c == n
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) {
var s = n.sqrt.floor(x)
return s * s == nx
}
 
// Returns whether or not 'x' is a perfect cube.
static isCube(x) {
var c = n.cbrt.truncate(x)
return c * c * c == nx
}
 
// 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
}
 
9,477

edits