Skip to content

Commit

Permalink
Updated BigInt version and made related changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgriebling committed Jul 14, 2023
1 parent 0e0ffc2 commit 7d49c4e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ let package = Package(
dependencies: ["BigDecimal"]),
]
)

//package.dependencies.append(
// .package(url: "https://github.com/SwiftPackageIndex/SPIManifest.git", from: "0.12.0")
//)
16 changes: 8 additions & 8 deletions Sources/BigDecimal/BigDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ public struct BigDecimal : Comparable, Equatable, Hashable, Codable {
}
let s = BInt(d.sign == .minus ? -significand : significand)
if exponent < 0 {
self.init(BInt.five ** (-exponent) * s, exponent)
self.init(BInt.FIVE ** (-exponent) * s, exponent)
} else if exponent > 0 {
self.init(BInt.two ** exponent * s, 0)
self.init(BInt.TWO ** exponent * s, 0)
} else {
self.init(s, 0)
}
Expand Down Expand Up @@ -584,7 +584,7 @@ extension BigDecimal {
var q = self.digits
var n = 0
while true {
let (q1, r) = q.quotientAndRemainder(dividingBy: BInt.ten)
let (q1, r) = q.quotientAndRemainder(dividingBy: BInt.TEN)
if !r.isZero {
break
}
Expand All @@ -596,7 +596,7 @@ extension BigDecimal {

/// Unit in last place = Self(1, self.exponent)
public var ulp: Self {
self.isFinite ? Self(BInt.one, self.exponent) : Self.flagNaN()
self.isFinite ? Self(BInt.ONE, self.exponent) : Self.flagNaN()
}


Expand Down Expand Up @@ -749,7 +749,7 @@ extension BigDecimal {
}
var exp = self.exponent
var sig = self.digits.abs
while sig.limbs.count > 2 {
while sig.words.count > 2 {
sig /= 10
exp += 1
}
Expand All @@ -764,7 +764,7 @@ extension BigDecimal {
if sig == 0 {
return Decimal(0)
}
assert(sig.limbs.count < 3)
assert(sig.words.count < 3)
assert(minExp <= exp && exp <= maxExp)

func decode() -> UInt16 {
Expand Down Expand Up @@ -887,7 +887,7 @@ extension BigDecimal {
d1 >>= count2
var count5 = 0
while true {
let (q, r) = d1.quotientAndRemainder(dividingBy: BInt.five)
let (q, r) = d1.quotientAndRemainder(dividingBy: BInt.FIVE)
if !r.isZero {
break
}
Expand All @@ -912,7 +912,7 @@ extension BigDecimal {
let z = Rounding.pow10(ctx.precision)
var r = BInt.zero
while q.abs >= z {
(q, r) = q.quotientAndRemainder(dividingBy: BInt.ten)
(q, r) = q.quotientAndRemainder(dividingBy: BInt.TEN)
m -= 1
}
switch ctx.mode {
Expand Down
4 changes: 2 additions & 2 deletions Sources/BigDecimal/Rounding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public struct Rounding: Equatable {
let pr = q.abs.asString().count
if pr > self.precision {
// 999.9 => 1000
return BigDecimal(q / BInt.ten, x.exponent + d + 1)
return BigDecimal(q / BInt.TEN, x.exponent + d + 1)
} else {
return BigDecimal(q, x.exponent + d)
}
Expand Down Expand Up @@ -160,7 +160,7 @@ public struct Rounding: Equatable {

static func pow10(_ n: Int) -> BInt {
assert(n >= 0)
return n < pow10table.count ? pow10table[n] : BInt.ten ** n
return n < pow10table.count ? pow10table[n] : BInt.TEN ** n
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/BigDecimalTests/TestDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ final class TestDecimal: XCTestCase {
XCTAssertTrue(min > 0.0)
let x = BigDecimal(min)
XCTAssertEqual(x.exponent, -128)
XCTAssertEqual(x.digits, BInt.one)
XCTAssertEqual(x.digits, BInt.ONE)
XCTAssertEqual(min, x.asDecimal())

let d = BigDecimal(BInt.one, -129).asDecimal()
let d = BigDecimal(BInt.ONE, -129).asDecimal()
XCTAssertEqual(d, 0.0)
XCTAssertFalse(BigDecimal.nanFlag)
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/BigDecimalTests/TestDecimal128Encoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class TestDecimal128Encoding: XCTestCase {

func string2UInt128(_ s: String) -> UInt128 {
let b = BInt(s, radix: 16)!
let lo = b.limbs[0]
let hi = b.limbs.count == 1 ? 0 : b.limbs[1]
return UInt128(high: hi, low: lo)
let lo = b.words[0]
let hi = b.words.count == 1 ? 0 : b.words[1]
return UInt128(high: UInt128.High(hi), low: UInt128.Low(lo))
}

struct test {
Expand Down

0 comments on commit 7d49c4e

Please sign in to comment.