-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Make FloatingPoint require that Self.Magnitude == Self #17323
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
Make FloatingPoint require that Self.Magnitude == Self #17323
Conversation
We didn't have the where clause to express this constraint at the time that the FloatingPoint protocol was implemented, but we do now. This is not a semantic change to FloatingPoint, which has always bound IEEE-754 arithmetic types, for which this constraint would necessarily hold, but it does effect the type system. For example, currently the following function does not type check: ~~~~ func foo<T>(x: T) -> T where T: FloatingPoint { var r = x.remainder(dividingBy: 1) return r.magnitude } ~~~~ with this change, it compiles correctly.
@swift-ci Please test |
You should be able to drop the constraint from |
@jrose-apple Actually I think I can go further and remove the FloatingPoint |
We no longer need to have a separate `abs` defined on FloatingPoint; we can use the existing function defined on `SignedNumeric` instead. Additionally mark `fabs` deprecated in favor of the Swift name `abs`; we don't need to have two names for the same function.
@swift-ci Please test |
Build failed |
Build failed |
@@ -14,8 +14,8 @@ import SwiftShims | |||
|
|||
// Generic functions implementable directly on FloatingPoint. | |||
@_transparent | |||
public func fabs<T: FloatingPoint>(_ x: T) -> T | |||
where T.Magnitude == T { | |||
@available(swift, deprecated: 4.2, renamed: "abs") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: why deprecate this specifically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the same reason that we deprecated other tgmath
overlays whose functionality duplicates APIs available on [Binary]FloatingPoint
and which don't offer compelling ergonomic advantages like sqrt
or fma
. We don't need to carry two names for the same function into the future, and abs
is the better name for this. fabs
is a curious artifact of the fact that C doesn't have overloaded functions.
@swift-ci please test |
Build failed |
Build failed |
* Make FloatingPoint require that Self.Magnitude == Self We didn't have the where clause to express this constraint at the time that the FloatingPoint protocol was implemented, but we do now. This is not a semantic change to FloatingPoint, which has always bound IEEE-754 arithmetic types, for which this constraint would necessarily hold, but it does effect the type system. For example, currently the following function does not type check: ~~~~ func foo<T>(x: T) -> T where T: FloatingPoint { var r = x.remainder(dividingBy: 1) return r.magnitude } ~~~~ with this change, it compiles correctly. Having done this, we no longer need to have a separate `abs` defined on FloatingPoint; we can use the existing function defined on `SignedNumeric` instead. Additionally mark the global `fabs` defined in the platform C module deprecated in favor of the Swift `abs`; we don't need to carry two names for this function going forward.
* Make FloatingPoint require that Self.Magnitude == Self We didn't have the where clause to express this constraint at the time that the FloatingPoint protocol was implemented, but we do now. This is not a semantic change to FloatingPoint, which has always bound IEEE-754 arithmetic types, for which this constraint would necessarily hold, but it does effect the type system. For example, currently the following function does not type check: ~~~~ func foo<T>(x: T) -> T where T: FloatingPoint { var r = x.remainder(dividingBy: 1) return r.magnitude } ~~~~ with this change, it compiles correctly. Having done this, we no longer need to have a separate `abs` defined on FloatingPoint; we can use the existing function defined on `SignedNumeric` instead. Additionally mark the global `fabs` defined in the platform C module deprecated in favor of the Swift `abs`; we don't need to carry two names for this function going forward.
We didn't have the where clause to express this constraint at the time that the FloatingPoint protocol was implemented, but we do now. This is not a semantic change to FloatingPoint, which has always bound IEEE-754 arithmetic types, for which this constraint would necessarily hold, but it does effect the type system.
For example, currently the following function does not type check:
with this change, it compiles correctly.
This is an ABI change, but a very minor one which should not break any currently-functioning code.