[Pitch] round π to nearest

Motivation

About 130 years ago, the Indiana state legislature briefly entertained a bill (bill 246 of the 1897 sitting of the Indiana General Assembly) that would have incidentally legislated the value of π. I figure it's time to try again.

When we designed the FloatingPoint protocols for Swift, IEEE 754 provided no guidance as to how π should be rounded. There are two plausibly defensible choices: the closest representable value, or the closest representable value that is strictly less than π (rounding toward zero). We opted for the second choice.

However, the IEEE 754 working group now has consensus that implementations should return the best approximation they can, rather than force rounding to a less accurate value to try to preserve some invariant. Accordingly we will adjust the definition of FloatingPoint.pi to match this guidance.

Detailed design

In the documentation for FloatingPoint.pi, strike this sentence:

/// This value is rounded toward zero to keep user computations with angles
/// from inadvertently ending up in the wrong quadrant.

and change the definition of Float.pi from 0x1.921fb4p1 to 0x1.921fb6p1 (the representable value just larger than π).

ABI compatibility

It is likely that some software out there does depend on this specific value in some surprising way. In such cases, it may be necessary to use 0x1.921fb4p1 instead of Float.pi. We do not expect such cases to be common, and the ability to more easily get the same results as programs written in other languages will outweigh any transient hassles.

Acknowledgments

Thanks to the IEEE 754 working group for detailed discussion of the issue.


Proposal draft here: swift-evolution/proposals/NNNN-indiana-pi-bill.md at indiana-pi-bill · stephentyrone/swift-evolution · GitHub

24 Likes

As far as "other languages to interoperate with", the value of the C macro (i.e. "inlinable compile-time constant") M_PI is probably what a Swift program is most likely to interoperate with, other than "data saved by past and future versions of the same program (or the same program on past and future OS versions)". What's that value now, on each of Swift's supported OSs, and is it likely to change?

3 Likes

Anybody checking floating-point calculations without accounting for loss of precision and rounding doesn't know what they're doing anyway, so this tiny change shouldn't matter.

Before you complain about looking this up, the current value should be easy to check on the GitHub CI now, using the right demo code and a highly cross-platform config like Swift System uses.

M_PI is not informative here, because for Double rounding π towards zero or to nearest produce the same value.

Some math libraries define an M_PIf as an extension; in all cases I know of it is rounded to nearest (because no one actually defined the constant, they just put f on the back of an overly-precise π literal and let the compiler round it, or appended f to M_PI in the preprocessor).

4 Likes

Oh, this is really only Float(32), plus guidance for implementors (but that latter part can't be enforced, because FloatingPoint is an longstanding protocol and who knows how many implementors it has). Okay, so this will help in that (in the default rounding mode) Float(Double.pi) == .pi after this change, and probably didn't before?

It is unlikely that anything except some tests benefits from this property, but yes.

5 Likes

Perhaps the doc comment should gain text explaining which version of Swift the change took place, and why. Something like:

“This value is rounded to the nearest representable float, in accordance with IEEE 754 recommendation u.v.w. Prior to Swift x.y.z it had been rounded toward zero, a difference of 1 ulp.”

7 Likes

That Float(Double.pi) != Float.pi is counter-intuitive and feels like a bug.

/// This value is rounded toward zero to keep user computations with angles
/// from inadvertently ending up in the wrong quadrant.

An example of what that rounding towards zero wanted to avoid:

    print(sin(Double(Float16.pi)))          // 0.0009676534387822795
    print(sin(Double(Float.pi)))            // 1.5099579909783707e-07
    print(sin(Double(Float(Double.pi))))    // -8.742278000372475e-08 (negative)
    print(sin(Double.pi))                   // 1.2246467991473532e-16

Too clever hack. Any other languages doing that?

4 Likes

I dunno, relying on == or != with floating-point values to me already feels like a bug in and of itself.

10 Likes

That's not always a bug, otherwise we would not have == or != on floating-point values to begin with.

1 Like

Not always, but almost always when checking the output of any realistic calculation, which is why my link above points out that relative accuracy that fits your context should almost always be used instead.

2 Likes

Sorry for the obvious nitpick, but particularly when comparing to zero, relative accuracy does not seem like the best tool!

1 Like

See my link above, they address that too.

1 Like

In other languages:

// C++ (playcode.io):
std::cout << std::setprecision(8) <<
    std::numbers::pi_v<float> << std::endl;   // 3.1415927
std::cout << std::setprecision(8) <<
    acosf(-1) << std::endl;                   // 3.1415925 ↓

// Rust (play.rust-lang.org):
println!("{}", std::f32::consts::PI);         // 3.1415927
println!("{}", (-1.0_f32).acos());            // 3.1415927
    
// C# (dotnetfiddle.net):
Console.WriteLine($"{Single.Pi}");            // 3.1415927
Console.WriteLine($"{MathF.Acos(-1.0f):F7}"); // 3.1415927

// Swift swiftfiddle.com and godbolt.org
print(Float.pi)                               // 3.1415925 ↓
print(acos(-1 as Float))                      // 3.1415927

// Swift (Xcode + arm mac):
print(Float.pi)                               // 3.1415925 ↓
print(acos(-1 as Float))                      // 3.1415925 ↓

Edit: added acos(-1). Interestingly ARM (or Foundation?) gives a different result.

1 Like

Neither ARM nor Foundation. Apple's libm, because of a a very literal reading of text in the C standard:

The acos functions return arccos x in the interval [0,π] radians.

combined with the same considerations that caused us to originally define Float.pi the way we did.

3 Likes

I see, thank you.

FTM, in the above list of examples only this one is "breaking that invariant" † :

// Swift swiftfiddle.com and godbolt.org
print(Float.pi)                               // 3.1415925 ↓
print(acos(-1 as Float))                      // 3.1415927

and would indeed be "fixed" if the value of Float.pi is bumped up.

† calling this "breaking" is probably a bit too strong – Swift doesn't even have documented behaviour for acos (outside of a Wikipedia link in the swift-numerics package – which most people using acos might not even use, or the documentation for the underlying C API which isn't fully applicable here).

Floating point equality

This is a popular belief, but it is not exactly correct. Floating point is more of a “shape” that can be used to represent the numbers: sign * significand * base^exponent.

For example, IEEE Standard for Floating-Point Arithmetic (IEEE 754) defines Float and Double, but it also defines various decimal formats like decimal32, decimal64, and decimal128. Those are commonly used for representing money. If we divide 10€ equally among 3 people, we have to end up with: 3.33€, 3.33€ and 3.34€. In this case, using equality to validate the result of such unit test is perfectly valid.

Article

Was this article written by AI? Very often they reference “Solution X” where the “Solution X” does not appear in the article. AI check also says it is 100% AI.

Anyway, one thing that caught my eye is:

For specific scenarios, particularly when dealing with display formatting or when exact decimal precision is critical, converting floats to strings and comparing the strings can be an option.

(Code example…)

Note: This is generally less efficient and can be problematic if the string formatting itself introduces inaccuracies.

First of all:

  • “display formatting” should be used for displaying numbers, and not for comparing them. Comparing the String representations rounded to a certain amount of digits has the same problem as fixed EPSILON - it does not scale with the magnitude of the compared numbers. For example 0.01 difference can be:
    • thousands of ulp off in the 0…1 range
    • the same number around greatestFiniteMagnitude
  • cases when “exact decimal precision is critical” should use decimal type instead of binary floating point.

As for the raw String conversion without precision IEEE 754 2019 says:

5.12 Details of conversion between floating-point data and external character

Implementations shall provide conversions between each supported binary format and external decimal character sequences such that, under roundTiesToEven, conversion from the supported format to external decimal character sequence and back recovers the original floating-point representation, except that a signaling NaN might be converted to a quiet NaN. See 5.12.1 and 5.12.2 for details.

To put it simply:

  • Different binary floating point values are guaranteed to have different String representations.
  • Each binary floating point can have multiple different String representations, as long as they convert back to the same value (the standard does NOT guarantee that the shortest String is used).

The raw String conversion produces the same results as ==:

  • If the values are equal via ==, they will produce the same String representation.
  • If they are different, they will produce different String representations.

We also need to special case NaN and infinity:

  • According to IEEE 754 -> “5.11 Details of comparison predicates”: NaNs are never equal (they are unordered). Yet their String representations are equal.
  • Based on IEEE 754 -> “5.12.1 External character sequences representing zeros, infinities, and NaNs”: positive infinity has 4 legal representations: "inf", "infinity", "+inf", "+infinity".

Anyway, just don't use String when comparing floats. Even with some “precision” applied.

2 Likes

There are different scenarios, e.g. if I were to store floats as dictionary keys or in a Set, or if I want to be sure the output of some calculation after refactoring is bit exact to what it was before (because the thing is going to Mars and any error would incur an astronomical cost) I'd probably wrap them in a struct and redefine hash/eq on that struct to do the binary comparison.

You're actually making my point with even this example, as the mathematically correct answer is €3.3333... for all three, so the only way to square that is to arbitrarily give the whole penny, er cent, to one of them, a hack to make the rounding math work. But since you are using floating point to model these financial calculations, it is possible for rounding and other inaccuracy to leak out here too, just extremely unlikely because you are always rounding at two sig digits, are likely only performing a few math operations in finance, and likely have many more digits in the calculation.

So yes, you are right that in those contrived scenarios you can check bit equality, because you are usually rounding way above the error bounds, though depending on the particular calculation, the error bounds can leak out there too.

Could very well be, I just skimmed it to make sure it covered some stuff I remembered.

No idea if that particular technique holds water, not one I've used.