Category talk:Wren-big: Difference between revisions
Content added Content deleted
(→Source code: Improved algorithm for BigInt.divisors2 and added divisorSum and divisorCount methods.) |
(→Source code: Added some more constants, 'dim' method and several other minor changes.) |
||
Line 59: | Line 59: | ||
static four { BigInt.small_( 4) } |
static four { BigInt.small_( 4) } |
||
static five { BigInt.small_( 5) } |
static five { BigInt.small_( 5) } |
||
static six { BigInt.small_( 6) } |
|||
static seven { BigInt.small_( 7) } |
|||
static eight { BigInt.small_( 8) } |
|||
static nine { BigInt.small_( 9) } |
|||
static ten { BigInt.small_(10) } |
static ten { BigInt.small_(10) } |
||
Line 639: | Line 643: | ||
if (!(b is BigInt)) b = BigInt.new(b) |
if (!(b is BigInt)) b = BigInt.new(b) |
||
return (a < b) ? a : b |
return (a < b) ? a : b |
||
} |
|||
// Returns the positive difference of two BigInts. |
|||
static dim(a, b) { |
|||
if (!(a is BigInt)) a = BigInt.new(a) |
|||
if (!(b is BigInt)) b = BigInt.new(b) |
|||
if (a >= b) return a - b |
|||
return zero |
|||
} |
} |
||
Line 964: | Line 976: | ||
+ (n) { |
+ (n) { |
||
if (!(n is BigInt)) n = BigInt.new(n) |
if (!(n is BigInt)) n = BigInt.new(n) |
||
if (n.isZero) return this |
if (n.isZero) return this.copy() |
||
if (this.isSmall) return smallAdd_(n) |
if (this.isSmall) return smallAdd_(n) |
||
if (_signed != n.signed_) return this - (-n) |
if (_signed != n.signed_) return this - (-n) |
||
Line 987: | Line 999: | ||
- (n) { |
- (n) { |
||
if (!(n is BigInt)) n = BigInt.new(n) |
if (!(n is BigInt)) n = BigInt.new(n) |
||
if (n.isZero) return this |
if (n.isZero) return this.copy() |
||
if (this.isSmall) return smallSubtract_(n) |
if (this.isSmall) return smallSubtract_(n) |
||
if (_signed != n.signed_) return this + (-n) |
if (_signed != n.signed_) return this + (-n) |
||
Line 1,005: | Line 1,017: | ||
} |
} |
||
if (a.value_ == 0) return BigInt.zero |
if (a.value_ == 0) return BigInt.zero |
||
if (a.value_ == 1) return this |
if (a.value_ == 1) return this.copy() |
||
if (a.value_ == -1) return -this |
if (a.value_ == -1) return -this |
||
return BigInt.multiplySmallAndList_(a.value_.abs, _value, _signed != a.signed_) |
return BigInt.multiplySmallAndList_(a.value_.abs, _value, _signed != a.signed_) |
||
Line 1,019: | Line 1,031: | ||
if (n.isSmall) { |
if (n.isSmall) { |
||
if (b == 0) return BigInt.zero |
if (b == 0) return BigInt.zero |
||
if (b == 1) return this |
if (b == 1) return this.copy() |
||
if (b == -1) return -this |
if (b == -1) return -this |
||
var ab = b.abs |
var ab = b.abs |
||
Line 1,063: | Line 1,075: | ||
Fiber.abort("Argument must be a positive integer.") |
Fiber.abort("Argument must be a positive integer.") |
||
} |
} |
||
if (n == 1) return this |
if (n == 1) return this.copy() |
||
var t = copy() |
var t = copy() |
||
var neg = t < 0 |
var neg = t < 0 |
||
Line 1,102: | Line 1,114: | ||
a = b |
a = b |
||
} |
} |
||
} |
} |
||
// 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 |
||
Line 1,307: | Line 1,319: | ||
} |
} |
||
// Returns true if the 'n'th bit of the current instance is set or false otherwise. |
|||
testBit(n) { |
testBit(n) { |
||
if (n.type != Num || !n.isInteger || n < 0) Fiber.abort("Argument must be a non-negative integer.") |
if (n.type != Num || !n.isInteger || n < 0) Fiber.abort("Argument must be a non-negative integer.") |
||
Line 1,480: | Line 1,492: | ||
} |
} |
||
// Returns all the divisors of 'n' including 1 and 'n' itself. |
|||
static divisors(n) { |
static divisors(n) { |
||
if (n < 1) return [] |
if (n < 1) return [] |
||
Line 1,493: | Line 1,505: | ||
divs.add(i) |
divs.add(i) |
||
var j = n / i |
var j = n / i |
||
if (j != i) divs2.add(j) |
if (j != i) divs2.add(j) |
||
} |
} |
||
i = i + k |
i = i + k |
||
Line 1,604: | Line 1,616: | ||
if (n > 2) prod = prod * 2 |
if (n > 2) prod = prod * 2 |
||
return prod |
return prod |
||
} |
} |
||
} |
} |
||
Line 1,772: | Line 1,784: | ||
// Rounding methods (similar to those in Num class). |
// Rounding methods (similar to those in Num class). |
||
ceil { // higher integer |
ceil { // higher integer |
||
if (isInteger) return this |
if (isInteger) return this.copy() |
||
var div = _n/_d |
var div = _n/_d |
||
if (!this.isNegative) div = div.inc |
if (!this.isNegative) div = div.inc |
||
Line 1,779: | Line 1,791: | ||
floor { // lower integer |
floor { // lower integer |
||
if (isInteger) return this |
if (isInteger) return this.copy() |
||
var div = _n/_d |
var div = _n/_d |
||
if (this.isNegative) div = div.dec |
if (this.isNegative) div = div.dec |
||
Line 1,788: | Line 1,800: | ||
round { // nearer integer |
round { // nearer integer |
||
if (isInteger) return this |
if (isInteger) return this.copy() |
||
var div = _n / _d |
var div = _n / _d |
||
if (_d == 2) { |
if (_d == 2) { |
||
Line 1,823: | Line 1,835: | ||
pow(i) { |
pow(i) { |
||
if (!((i is Num) && i.isInteger)) Fiber.abort("Argument must be an integer.") |
if (!((i is Num) && i.isInteger)) Fiber.abort("Argument must be an integer.") |
||
if (i == 0) return this |
if (i == 0) return this.copy() |
||
var np = _n.pow(i.abs) |
var np = _n.pow(i.abs) |
||
var dp = _d.pow(i.abs) |
var dp = _d.pow(i.abs) |
||
Line 1,866: | Line 1,878: | ||
// Other methods. |
// Other methods. |
||
inc { this + BigRat.one } // increment |
inc { this + BigRat.one } // increment |
||
dec { this - BigRat.one } // decrement |
dec { this - BigRat.one } // decrement |
||
abs { (_n >= BigInt.zero) ? |
abs { (_n >= BigInt.zero) ? copy() : -this } // absolute value |
||
sign { _n.sign } // sign |
sign { _n.sign } // sign |
||
// The inherited 'clone' method just returns 'this' as BigRat objects are immutable. |
// The inherited 'clone' method just returns 'this' as BigRat objects are immutable. |