Category talk:Wren-big: Difference between revisions

→‎Source code: Added iroot and icbrt methods to BigInt class.
(→‎Source code: Improved BigInt.isqrt method - up to 3x faster than before.)
(→‎Source code: Added iroot and icbrt methods to BigInt class.)
Line 1,010:
}
return BigInt.big_(BigInt.square_(_value), false)
}
 
// Returns the integer n'th root of the current instance
// i.e. the precise n'th root truncated towards zero if not an integer.
// Throws an error if the current instance is negative and n is even.
iroot(n) {
if (!((n is Num) && n.isInteger && n > 0)) {
Fiber.abort("Argument must be a positive integer.")
}
if (n == 1) return this
var t = copy()
var neg = t < 0
if (neg) {
if (n % 2 == 0) {
Fiber.abort("Cannot take the %(n)th root of a negative number.")
} else {
t = -t
}
}
n = n - 1
var s = t + 1
var u = t
while (u < s) {
s = u
u = ((u * n) + t / u.pow(n)) / (n + 1)
}
return (neg) ? -s : s
}
 
// Returns the cube root of the current instance
icbrt {
if (isSmall) return BigInt.small_(toSmall.cbrt.floor)
return iroot(3)
}
 
9,476

edits