mirror of
https://github.com/apple/swift-numerics
synced 2026-11-28 05:14:31 +00:00
Address PR feedback
Eliminate unnecessary math function calls, and also handle floating point edge cases better.
This commit is contained in:
1 parent
bc2f9fb7c6
commit
cb56525312
3 files changed
+26
-48
No files matched your search
+23
-12
@@ -4,9 +4,6 @@
|
||||
//
|
||||
// Created by Robert Thompson on 2/4/19.
|
||||
//
|
||||
|
||||
import NumericsShims
|
||||
|
||||
fileprivate let digitsAndDash = Set("-0123456789")
|
||||
|
||||
public struct BigInt: BinaryInteger, SignedNumeric, SignedInteger, CustomStringConvertible, LosslessStringConvertible, Hashable {
|
||||
@@ -159,18 +156,28 @@ public struct BigInt: BinaryInteger, SignedNumeric, SignedInteger, CustomStringC
|
||||
}
|
||||
|
||||
public init<T>(_ source: T) where T : BinaryFloatingPoint {
|
||||
precondition(
|
||||
!(source.isNaN || source.isInfinite),
|
||||
"\(type(of: source)) value cannot be converted to BigInt because it is either infinite or NaN"
|
||||
)
|
||||
|
||||
let isNegative = source < 0.0
|
||||
var float = Double(isNegative ? -source : source)
|
||||
var words = Words()
|
||||
while float > 0.0 {
|
||||
let digit = UInt(libm_remainder(float, Double(UInt.max) + 1.0))
|
||||
words.append(digit)
|
||||
var float = isNegative ? -source : source
|
||||
|
||||
if let _ = UInt(exactly: T.greatestFiniteMagnitude) {
|
||||
self.words = [UInt(float)]
|
||||
} else {
|
||||
var words = Words()
|
||||
let radix = T(sign: .plus, exponent: T.Exponent(UInt.bitWidth), significand: 1)
|
||||
repeat {
|
||||
let digit = UInt(float.truncatingRemainder(dividingBy: radix))
|
||||
words.append(digit)
|
||||
float = (float / radix).rounded(.towardZero)
|
||||
} while float != 0
|
||||
|
||||
float = libm_floor(float / (Double(UInt.max) + 1.0))
|
||||
self.words = words
|
||||
}
|
||||
|
||||
self.words = words
|
||||
|
||||
if isNegative {
|
||||
self = -self
|
||||
}
|
||||
@@ -203,7 +210,11 @@ public struct BigInt: BinaryInteger, SignedNumeric, SignedInteger, CustomStringC
|
||||
}
|
||||
|
||||
public init?<T>(exactly source: T) where T : BinaryFloatingPoint {
|
||||
if libm_ceil(Double(source)) != Double(source) { return nil }
|
||||
if (source.isNaN || source.isInfinite) ||
|
||||
(source.rounded(.towardZero) != source) {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.init(source)
|
||||
}
|
||||
|
||||
|
||||
@@ -143,18 +143,6 @@ HEADER_SHIM float libm_log10f(float x) {
|
||||
return __builtin_log10f(x);
|
||||
}
|
||||
|
||||
HEADER_SHIM float libm_ceilf(float x) {
|
||||
return __builtin_ceilf(x);
|
||||
}
|
||||
|
||||
HEADER_SHIM float libm_floorf(float x) {
|
||||
return __builtin_floorf(x);
|
||||
}
|
||||
|
||||
HEADER_SHIM float libm_remainderf(float x, float y) {
|
||||
return __builtin_remainderf(x, y);
|
||||
}
|
||||
|
||||
#if !defined _WIN32
|
||||
HEADER_SHIM float libm_lgammaf(float x, int *signp) {
|
||||
extern float lgammaf_r(float, int *);
|
||||
@@ -275,18 +263,6 @@ HEADER_SHIM double libm_log10(double x) {
|
||||
return __builtin_log10(x);
|
||||
}
|
||||
|
||||
HEADER_SHIM double libm_ceil(double x) {
|
||||
return __builtin_ceil(x);
|
||||
}
|
||||
|
||||
HEADER_SHIM double libm_floor(double x) {
|
||||
return __builtin_floor(x);
|
||||
}
|
||||
|
||||
HEADER_SHIM double libm_remainder(double x, double y) {
|
||||
return __builtin_remainder(x, y);
|
||||
}
|
||||
|
||||
#if !defined _WIN32
|
||||
HEADER_SHIM double libm_lgamma(double x, int *signp) {
|
||||
extern double lgamma_r(double, int *);
|
||||
@@ -404,18 +380,6 @@ HEADER_SHIM long double libm_lgammal(long double x, int *signp) {
|
||||
extern long double lgammal_r(long double, int *);
|
||||
return lgammal_r(x, signp);
|
||||
}
|
||||
|
||||
HEADER_SHIM long double libm_ceill(long double x) {
|
||||
return __builtin_ceill(x);
|
||||
}
|
||||
|
||||
HEADER_SHIM long double libm_floorl(long double x) {
|
||||
return __builtin_floorl(x);
|
||||
}
|
||||
|
||||
HEADER_SHIM long double libm_remainderl(long double x, long double y) {
|
||||
return __builtin_remainderl(x, y);
|
||||
}
|
||||
#endif
|
||||
|
||||
// MARK: - shims to import C complex operations for timing purposes
|
||||
|
||||
@@ -34,6 +34,9 @@ final class BigIntTests: XCTestCase {
|
||||
XCTAssertNotNil(baz)
|
||||
let equal = (baz ?? 0) / BigInt(1e38) == BigInt(24)
|
||||
XCTAssertEqual(equal, true)
|
||||
|
||||
let infinite = BigInt(exactly: Double.infinity)
|
||||
XCTAssertNil(infinite)
|
||||
}
|
||||
|
||||
func testUIntConversion() {
|
||||
|
||||
Reference in new issue
Block a user