From 6e85aa2bbde250dcbc051f76c64d24195ae9fdc1 Mon Sep 17 00:00:00 2001 From: Ben Rimmington Date: Tue, 21 Jan 2020 19:47:19 +0000 Subject: [PATCH] [BigInt] Revert the BinaryFloatingPoint conversion --- Sources/BigInt/BigInt.swift | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Sources/BigInt/BigInt.swift b/Sources/BigInt/BigInt.swift index 618ab4e..3d1006d 100644 --- a/Sources/BigInt/BigInt.swift +++ b/Sources/BigInt/BigInt.swift @@ -343,21 +343,24 @@ extension BigInt: BinaryInteger { \(type(of: source)) value cannot be converted to BigInt because it is \ either infinite or NaN """) - precondition(T.significandBitCount < .max) - let source = source.rounded(.towardZero) - let isNegative = source < 0 + let isNegative = source < 0.0 + var float = isNegative ? -source : source - guard !source.isZero else { - self = 0 - return + if let _ = UInt(exactly: T.greatestFiniteMagnitude) { + 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 + + self.words = words } - self = BigInt(source.significandBitPattern) - self |= BigInt(1) << T.significandBitCount - self <<= source.exponent - self >>= T.significandBitCount - if isNegative { self.negate() }