Skip to content

[stdlib] Make use of protocol requirements to convert from concrete floating-point types #33803

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
merged 3 commits into from
Sep 5, 2020
Merged
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
21 changes: 20 additions & 1 deletion stdlib/public/core/FloatingPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1890,7 +1890,26 @@ extension BinaryFloatingPoint {
/// - Parameter value: A floating-point value to be converted.
@inlinable
public init<Source: BinaryFloatingPoint>(_ value: Source) {
self = Self._convert(from: value).value
#if !os(macOS) && !(os(iOS) && targetEnvironment(macCatalyst))
if #available(iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
if case let value_ as Float16 = value {
self = Self(Float(value_))
return
}
}
#endif
switch value {
case let value_ as Float:
Copy link
Contributor

Choose a reason for hiding this comment

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

This should handle Float16 as well (implemented as self = Self(Float(value_)) in that case).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@stephentyrone The required checks for the availability of Float16 are...not pretty. See the commit "🤮" for details.

self = Self(value_)
case let value_ as Double:
self = Self(value_)
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
case let value_ as Float80:
self = Self(value_)
#endif
default:
self = Self._convert(from: value).value
}
}

/// Creates a new instance from the given value, if it can be represented
Expand Down