Skip to content

[stdlib] Simplify 'BinaryFloatingPoint.init?<T: BinaryFloatingPoint>(exactly: T)' #33910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions stdlib/public/core/FloatingPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1948,9 +1948,30 @@ extension BinaryFloatingPoint {
/// - Parameter value: A floating-point value to be converted.
@inlinable
public init?<Source: BinaryFloatingPoint>(exactly value: Source) {
let (value_, exact) = Self._convert(from: value)
guard exact else { return nil }
self = value_
// We define exactness by equality after roundtripping; since NaN is never
// equal to itself, it can never be converted exactly.
if value.isNaN { return nil }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simplify the logic downstream by pulling out zero and infinity here (since both are always exact). WDYT?

Copy link
Collaborator Author

@xwu xwu Sep 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephentyrone I left it out because, when specialized, the case of Float-to-Double would be a single 'isNaN' check followed by conversion. Checking for finite nonzero values would have to be written separately anyway, since it wouldn't return nil; this is what happens here already. The only difference is whether it can be short-circuited or not when the concrete types are known; the logic doesn't otherwise get any simpler, and I'd like to be able to short circuit that check when it's not necessary.

if (Source.exponentBitCount > Self.exponentBitCount
|| Source.significandBitCount > Self.significandBitCount)
&& value.isFinite && !value.isZero {
let exponent = value.exponent
if exponent < Self.leastNormalMagnitude.exponent {
if exponent < Self.leastNonzeroMagnitude.exponent { return nil }
if value.significandWidth >
Int(Self.Exponent(exponent) - Self.leastNonzeroMagnitude.exponent) {
return nil
}
} else {
if exponent > Self.greatestFiniteMagnitude.exponent { return nil }
if value.significandWidth >
Self.greatestFiniteMagnitude.significandWidth {
return nil
}
}
}

self = Self(value)
}

@inlinable
Expand Down
41 changes: 41 additions & 0 deletions test/stdlib/FloatingPoint.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,47 @@ FloatingPoint.test("BinaryFloatingPoint/genericIntegerConversion") {
}

FloatingPoint.test("BinaryFloatingPoint/genericFloatingPointConversion") {
func convert<
T: BinaryFloatingPoint, U: BinaryFloatingPoint
>(exactly value: T, to: U.Type) -> U { U(exactly: value) }

expectEqual(convert(exactly: 0 as Float, to: Double.self), 0.0)
expectEqual(convert(exactly: -0.0 as Float, to: Double.self), -0.0)
expectEqual(
convert(exactly: -0.0 as Float, to: Double.self).sign,
FloatingPointSign.minus)
expectEqual(convert(exactly: 0 as Double, to: Float.self), 0.0 as Float)
expectEqual(convert(exactly: -0.0 as Double, to: Float.self), -0.0 as Float)
expectEqual(
convert(exactly: -0.0 as Double, to: Float.self).sign,
FloatingPointSign.minus)
expectEqual(convert(exactly: 1 as Float, to: Double.self), 1.0)
expectEqual(convert(exactly: -1 as Float, to: Double.self), -1.0)
expectEqual(convert(exactly: 1 as Double, to: Float.self), 1.0 as Float)
expectEqual(convert(exactly: -1 as Double, to: Float.self), -1.0 as Float)
expectEqual(
convert(exactly: Float.infinity, to: Double.self), Double.infinity)
expectEqual(
convert(exactly: -Float.infinity, to: Double.self), -Double.infinity)
expectEqual(
convert(exactly: Double.infinity, to: Float.self), Float.infinity)
expectEqual(
convert(exactly: -Double.infinity, to: Float.self), -Float.infinity)
expectEqual(convert(exactly: Float.nan, to: Double.self), nil)
expectEqual(convert(exactly: Double.nan, to: Float.self), nil)
expectEqual(convert(exactly: Float.nan, to: Float.self), nil)
expectEqual(convert(exactly: Double.nan, to: Double.self), nil)
expectEqual(
convert(exactly: Double.leastNonzeroMagnitude, to: Float.self), nil)
expectEqual(
convert(exactly: Float.leastNonzeroMagnitude, to: Double.self),
Double(Float.leastNonzeroMagnitude))
expectEqual(
convert(exactly: Double.greatestFiniteMagnitude, to: Float.self), nil)
expectEqual(
convert(exactly: Float.greatestFiniteMagnitude, to: Double.self),
Double(Float.greatestFiniteMagnitude))

expectTrue(Double._convert(from: 0 as Float) == (value: 0, exact: true))
expectTrue(Double._convert(from: -0.0 as Float) == (value: -0.0, exact: true))
expectTrue(Double._convert(from: 1 as Float) == (value: 1, exact: true))
Expand Down