-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib] Add **
and **=
for exponentiation
#36053
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
70613cd
[stdlib] Add exponentiation precedence group and operator
xwu 19585c4
[stdlib] Re-enable floating-point tests
benrimmington 53fa483
[stdlib] Remove `**` and `**=` placeholders
benrimmington 95602b9
[stdlib] Add `__builtin_pow` to LibcShims.h
benrimmington 17b88fd
[stdlib] Add `**` and `**=` implementations
benrimmington 4bea48d
[stdlib] Add `**` and `**=` tests
benrimmington File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
// RUN: %target-codesign %t/a.out | ||
// RUN: %line-directive %t/FloatingPoint.swift -- %target-run %t/a.out | ||
// REQUIRES: executable_test | ||
// REQUIRES: rdar49026133 | ||
|
||
import Swift | ||
import StdlibUnittest | ||
|
@@ -118,17 +117,17 @@ FloatingPoint.test("BinaryFloatingPoint/genericIntegerConversion") { | |
FloatingPoint.test("BinaryFloatingPoint/genericFloatingPointConversion") { | ||
func convert< | ||
T: BinaryFloatingPoint, U: BinaryFloatingPoint | ||
>(exactly value: T, to: U.Type) -> U { U(exactly: value) } | ||
>(exactly value: T, to: U.Type) -> U? { U(exactly: value) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused what happened here. Did it always not compile? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
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, | ||
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, | ||
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) | ||
|
@@ -531,6 +530,22 @@ FloatingPoint.test("${Self}.round") { | |
} | ||
} | ||
|
||
FloatingPoint.test("${Self}.exponentiation") { | ||
expectEqual(${Self}(0), 0 ** 42) | ||
expectEqual(${Self}(1), 1 ** 42) | ||
expectEqual(${Self}(1), 42 ** 0) | ||
expectEqual(${Self}(42), 42 ** 1) | ||
expectEqual(${Self}(0.5), 2 ** -1) | ||
expectEqual(${Self}(0.25), 2 ** -2) | ||
expectEqual(${Self}(1024), 2 ** 10) | ||
expectEqual(${Self}(2.0).squareRoot(), 2.0 ** 0.5) | ||
expectEqual(${Self}(0.5).squareRoot(), 0.5 ** 0.5) | ||
|
||
var actual: ${Self} = 2 | ||
actual **= 10 | ||
expectEqual(1024, actual) | ||
} | ||
|
||
FloatingPoint.test("${Self}.remainder") { | ||
// Basic sanity tests only; these are only sufficient to provide reassurance | ||
// that a known-good remainder function (e.g. from the C stdlib) has been | ||
|
@@ -827,10 +842,11 @@ FloatingPoint.test("${FloatSelf}/{Comparable,Hashable,Equatable}") { | |
// If either lhs or rhs is signaling, or if both are quiet NaNs, the | ||
// result is a quiet NaN. | ||
if lhs.isSignalingNaN || rhs.isSignalingNaN || (lhs.isNaN && rhs.isNaN) { | ||
expectTrue(min.isQuietNaN) | ||
expectTrue(max.isQuietNaN) | ||
expectTrue(minMag.isQuietNaN) | ||
expectTrue(maxMag.isQuietNaN) | ||
// FIXME: rdar49026133 | ||
// expectTrue(min.isQuietNaN) | ||
// expectTrue(max.isQuietNaN) | ||
// expectTrue(minMag.isQuietNaN) | ||
// expectTrue(maxMag.isQuietNaN) | ||
} | ||
// If only one of lhs and rhs is NaN, the result of the min/max | ||
// operations is always the other value. While contrary to all other | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
The tricky thing about adding these for real (and @stephentyrone has said this in the forums) is that we then need to talk about whether the
(Self, Self) -> Self
overloads should vend IEEEpow
(integral exponents get special treatment) orpowr
(derived by considering onlyexp(rhs * log(lhs))
).We would also need to consider if
(Self, Int) -> Self
overloads need to be added at the same time (vending IEEEpown
), and if so whether they can appropriately be spelled with the same operator for two subtly different functions. The reason it'd have to be talked about now is, if we do decide to overload and choosepowr
for the above(Self, Self) -> Self
overload, then for an integer literal exponent adding the overload later would silently change the behavior of existing code.(My personal preference would be to use
** (Self, Self) -> Self
to vendpow
and** (Self, Int) -> Self
to vendpown
, with a consideration for&**
to be added if needed forpowr
. But I have to admit I haven't thought about it deeply.)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.
Swift Numerics uses
__builtin_pow{,f,l}
for all pairs ofpow(_:_:)
methods:Float16: Real
Float32: Real
Float64: Real
Float80: Real
Clang also has:
__builtin_powi{,f,l}
operations, which may be IEEEpown
?__builtin_powf16
and__builtin_sqrtf16
, and some support for_Float16
.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.
Well I think that's settled then...
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.
Correction: Swift Numerics uses a
guard
statement, to implementpow(x, y)
asexp(y * log(x))
:So I think Clang/C99 is IEEE
pow
? Swift Numerics is IEEEpowr
andpown
?__builtin_pow(-2.0, 4.0)
16.0
pow
pow(-2.0, 4.0)
16.0
pow
Double.pow(-2.0, 4.0)
.nan
powr
Double.pow(-2.0, 4__)
16.0
pown
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.
Yeah, conceptually, that would be how they're aligned then (there are some edge conditions where the IEEE-specified result of
pow
andpowr
differ (pow(1, .infinity)
being one example)).