Skip to content

Commit 5da37a5

Browse files
Round Float.pi down so that it's in [0,π].
* Change value of Float.pi so that it's rounded down; this helps insure that angles computed via trigonometry end up in the correct quadrant. * Missed commit of corresponding test change.
1 parent 3c9eba5 commit 5da37a5

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

stdlib/public/core/FloatingPoint.swift.gyb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ public protocol FloatingPoint: Comparable, IntegerLiteralConvertible,
122122

123123
/// The mathematical constant pi = 3.14159...
124124
///
125+
/// This value should be rounded towards zero to keep user computations
126+
/// with angles from inadvertently ending up in the wrong quadrant.
127+
///
125128
/// Extensible floating-point types might provide additional APIs to obtain
126129
/// this value to caller-specified precision.
127130
static var pi: Self { get }

stdlib/public/core/FloatingPointTypes.swift.gyb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,12 @@ extension ${Self}: BinaryFloatingPoint {
341341

342342
public static var pi: ${Self} {
343343
%if bits == 32:
344-
return Float(0x1.921fb6p1)
344+
// Note: this is not the correctly-rounded (to nearest) value of pi,
345+
// because pi would round *up* in Float precision, which can result
346+
// in angles in the wrong quadrant if users aren't careful. This is
347+
// not a problem for Double or Float80, as pi rounds down in both of
348+
// those formats.
349+
return Float(0x1.921fb4p1)
345350
%elif bits == 64:
346351
return Double(0x1.921fb54442d18p1)
347352
%elif bits == 80:

test/1_stdlib/FloatingPoint.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ FloatingPoint.test("Float/staticProperties") {
104104
expectBitwiseEqual(bitPattern: 0x7fa0_0000, Ty.signalingNaN)
105105
expectBitwiseEqual(bitPattern: 0x7f80_0000, Ty.infinity)
106106
expectBitwiseEqual(0x1.ffff_fe__p127, Ty.greatestFiniteMagnitude)
107-
expectBitwiseEqual(0x1.921f_b6__p1, Ty.pi)
107+
expectBitwiseEqual(0x1.921f_b4__p1, Ty.pi)
108108
expectBitwiseEqual(0x1.0p-23, Ty.ulpOfOne)
109109
expectBitwiseEqual(0x1.0p-126, Ty.leastNormalMagnitude)
110110
#if arch(arm)

0 commit comments

Comments
 (0)