Category talk:Wren-big: Difference between revisions

Content added Content deleted
(→‎Source code: Bug fix.)
(→‎Source code: Improved BigInt.isqrt method - up to 3x faster than before.)
Line 1,015: Line 1,015:
// r.square <= this. Throws an error if the current instance is negative.
// r.square <= this. Throws an error if the current instance is negative.
isqrt {
isqrt {
if (this.isNegative) Fiber.abort("Cannot take the square root of a negative number.")
if (isNegative) Fiber.abort("Cannot take the square root of a negative number.")
var q = BigInt.one
if (isSmall) return BigInt.small_(toSmall.sqrt.floor)
while (q <= this) q = q * 4
var one = BigInt.one
var z = this.copy()
var a = one << ((bitLength + one) >> one)
var r = BigInt.zero
while (true) {
while (q > BigInt.one) {
var b = (this/a + a) >> one
q = q / 4
if (b >= a) return a
var t = z - r - q
a = b
r = r / 2
if (t >= 0) {
z = t
r = r + q
}
}
}
return r
}
}
// Returns a list containing the quotient and the remainder after dividing the current instance
// Returns a list containing the quotient and the remainder after dividing the current instance