Skip to content

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

Merged

Conversation

stephentyrone
Copy link
Contributor

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.

This is an ABI change, but a very minor one which should not break any currently-functioning code.

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.
@stephentyrone stephentyrone requested a review from moiseev June 19, 2018 15:21
@stephentyrone
Copy link
Contributor Author

@swift-ci Please test

@stephentyrone stephentyrone requested a review from milseman June 19, 2018 15:57
@jrose-apple
Copy link
Contributor

You should be able to drop the constraint from abs then, right?

@stephentyrone
Copy link
Contributor Author

@jrose-apple Actually I think I can go further and remove the FloatingPoint abs entirely. I'll update this shortly.

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.
@stephentyrone
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 7208f96

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 7208f96

@@ -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")
Copy link
Collaborator

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?

Copy link
Contributor Author

@stephentyrone stephentyrone Jun 19, 2018

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.

@stephentyrone
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - d74597c

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - d74597c

@stephentyrone stephentyrone merged commit ff89fce into swiftlang:master Jun 20, 2018
stephentyrone added a commit to stephentyrone/swift that referenced this pull request Jun 20, 2018
* 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.
stephentyrone added a commit that referenced this pull request Jun 22, 2018
* 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.
@stephentyrone stephentyrone deleted the floating-point-magnitude-self branch February 8, 2023 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants