Skip to content

Commit dbd3b18

Browse files
committed
Fix bug #13: addition/subtraction of negative BigInts and zero.
1 parent c317982 commit dbd3b18

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

NumberKit.xcodeproj/xcshareddata/xcschemes/NumberKit iOS.xcscheme

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<BuildableReference
1616
BuildableIdentifier = "primary"
1717
BlueprintIdentifier = "CCD2603823F20F7200DFE5A4"
18-
BuildableName = "NumberKit iOS.framework"
18+
BuildableName = "NumberKit.framework"
1919
BlueprintName = "NumberKit iOS"
2020
ReferencedContainer = "container:NumberKit.xcodeproj">
2121
</BuildableReference>
@@ -61,7 +61,7 @@
6161
<BuildableReference
6262
BuildableIdentifier = "primary"
6363
BlueprintIdentifier = "CCD2603823F20F7200DFE5A4"
64-
BuildableName = "NumberKit iOS.framework"
64+
BuildableName = "NumberKit.framework"
6565
BlueprintName = "NumberKit iOS"
6666
ReferencedContainer = "container:NumberKit.xcodeproj">
6767
</BuildableReference>

Sources/NumberKit/BigInt.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public struct BigInt: Hashable,
260260
// Determine base
261261
let radix = UInt32(base.radix)
262262
// Shortcut handling of zero
263-
if isZero {
263+
if self.isZero {
264264
return "0"
265265
}
266266
var radixPow: UInt32 = 1
@@ -428,6 +428,9 @@ public struct BigInt: Hashable,
428428

429429
/// Returns the sum of `self` and `rhs` as a `BigInt`.
430430
public func plus(_ rhs: BigInt) -> BigInt {
431+
guard !rhs.isZero else {
432+
return self
433+
}
431434
guard self.negative == rhs.negative else {
432435
return self.minus(rhs.negate)
433436
}
@@ -454,6 +457,9 @@ public struct BigInt: Hashable,
454457

455458
/// Returns the difference between `self` and `rhs` as a `BigInt`.
456459
public func minus(_ rhs: BigInt) -> BigInt {
460+
guard !rhs.isZero else {
461+
return self
462+
}
457463
guard self.negative == rhs.negative else {
458464
return self.plus(rhs.negate)
459465
}
@@ -606,7 +612,7 @@ public struct BigInt: Hashable,
606612
/// Computes the square root; this is the largest `BigInt` value `x` such that `x * x` is
607613
/// smaller than `self`.
608614
public var sqrt: BigInt {
609-
guard !self.isNegative else {
615+
guard !self.negative else {
610616
preconditionFailure("cannot compute square root of negative number")
611617
}
612618
guard !self.isZero && !self.isOne else {

Tests/NumberKitTests/BigIntTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ class BigIntTests: XCTestCase {
356356
XCTAssertEqual(x3n.description, x3s)
357357
}
358358

359+
func testBasicOpWithZero() {
360+
let x1: BigInt = "0"
361+
let x2: BigInt = "-1"
362+
XCTAssertFalse(x1.isNegative)
363+
XCTAssertFalse(x1.negate.isNegative)
364+
XCTAssertEqual(x2.plus(x1), x2)
365+
XCTAssertEqual(x2.minus(x1), x2)
366+
}
367+
359368
static let allTests = [
360369
("testConstructors", testConstructors),
361370
("testPlus", testPlus),

0 commit comments

Comments
 (0)