Jump to content

Category talk:Wren-big: Difference between revisions

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

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.