|
@@ -19,11 +19,14 @@ extension CS.BigUInt {
|
|
var overflow = false
|
|
var overflow = false
|
|
var count = 0
|
|
var count = 0
|
|
while !overflow {
|
|
while !overflow {
|
|
- let (p, o) = power.multipliedReportingOverflow(by: Word(radix))
|
|
|
|
- overflow = o
|
|
|
|
- if !o || p == 0 {
|
|
|
|
|
|
+ let (high,low) = power.multipliedFullWidth(by: Word(radix))
|
|
|
|
+ if high > 0 {
|
|
|
|
+ overflow = true
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if !overflow || (high == 1 && low == 0) {
|
|
count += 1
|
|
count += 1
|
|
- power = p
|
|
|
|
|
|
+ power = low
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return (count, power)
|
|
return (count, power)
|
|
@@ -37,7 +40,8 @@ extension CS.BigUInt {
|
|
/// - Parameter `radix`: The base of the number system to use, or 10 if unspecified.
|
|
/// - Parameter `radix`: The base of the number system to use, or 10 if unspecified.
|
|
/// - Returns: The integer represented by `text`, or nil if `text` contains a character that does not represent a numeral in `radix`.
|
|
/// - Returns: The integer represented by `text`, or nil if `text` contains a character that does not represent a numeral in `radix`.
|
|
public init?<S: StringProtocol>(_ text: S, radix: Int = 10) {
|
|
public init?<S: StringProtocol>(_ text: S, radix: Int = 10) {
|
|
- precondition(radix > 1)
|
|
|
|
|
|
+ precondition(radix > 1 && radix < 36)
|
|
|
|
+ guard !text.isEmpty else { return nil }
|
|
let (charsPerWord, power) = CS.BigUInt.charsPerWord(forRadix: radix)
|
|
let (charsPerWord, power) = CS.BigUInt.charsPerWord(forRadix: radix)
|
|
|
|
|
|
var words: [Word] = []
|
|
var words: [Word] = []
|
|
@@ -97,7 +101,7 @@ extension CS.BigInt {
|
|
}
|
|
}
|
|
guard let m = magnitude else { return nil }
|
|
guard let m = magnitude else { return nil }
|
|
self.magnitude = m
|
|
self.magnitude = m
|
|
- self.sign = sign
|
|
|
|
|
|
+ self.sign = m.isZero ? .plus : sign
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|