apple / swift-numerics

Advanced mathematical types and functions for Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tau?

davedelong opened this issue · comments

There are a lot of aspects of trigonometry that are easier to express using τ instead of using (see also: https://tauday.com).

Any thoughts on including tau as a constant in this library?

Tau is cute, but it doesn't actually make anything significantly easier to express, in my experience. Marc Reynolds sums up my perspective on the issue pretty well here (thanks Marc!).

At a more basic level than Marc's post, tau is at best not enough better than pi to overthrow convention (and it's very reasonable to argue that it isn't better at all, because -π ... π is a nicer range to work with than 0 ... τ).

Closing this as out of scope for now.

τ is a built-in constant in Python since 2011, and Rust since 2020; it's a built-in constant in the C++ Boost library, the Godot game engine, and the .NET Framework. Some other languages, like Swift, have rejected it on the basis that it's not a popular enough convention, but that's a cheeky stance - if language maintainers aren't willing to accept it, it will never become "popular enough". So it comes down to some maintainer making a tyrannical decision based on personal preference, which just goes to show how broken the "open-source" ecosystem is.

I could argue all day about the many ways in which τ is objectively better, and about how much it makes coding trigonometry easier, but instead will propose the following: developers should have the choice. The cost of adding the constant is infinitesimal, while the benefits, regardless of how small, are measurable.

If you do want some concrete evidence of how τ could be useful, I have a practical example to give. I've been following along the (excellent!) iOS App Dev Tutorials, and came across this section that deals with trigonometry. It uses degrees instead of radians, which makes the math look simple at the cost of some internal precision. That is fine. The point is that if you do choose radians, the code looks like this:

private var radiansPerSpeaker: Double {
    2.0 * Double.pi / Double(totalSpeakers)
}

I think it's non-controversial that this is less nice than the degrees alternative. If τ was defined, one could just do this, though:

private var radiansPerSpeaker: Double {
    Double.tau / Double(totalSpeakers)
}

Which is objectively simpler to read and in intent. Arguably even more clear than the degrees alternative, since we're using a built-in constant instead of the magic 360.0 number.

Anyway, I doubt the Swift team will reconsider, but it honestly disappoints me that a language so focused on user friendliness like Swift ended up taking a user unfriendly stance in this case.

Swift is (at least in theory) an opinionated language, and so rather than throwing everything in the soup, I am making the user-friendly choice of siding with a couple millennia of mathematical precedent and having a single unambiguous option.

τ is an entertaining novelty. It is not meaningfully better or more correct than π. Why not provide both so that users can choose? Because then everyone who reads τ has to scratch their head for a minute and go down a rabbit hole of YouTube videos. If you want to use it in your own code, it is trivial to define:

extension FloatingPoint {
  public static var τ: Self { return 2 * .pi }
}

nothing is stopping you.

I am making the user-friendly choice of siding with a couple millennia of mathematical precedent

Nice use of argumentum ad antiquitatem.

having a single unambiguous option

You're implying having the two constants introduces ambiguity, which is simply not true. Both are distinct constants with distinct symbols and clearly defined values.

It is not meaningfully better or more correct than π

You make this claim without any backing evidence. The Tau manifesto provides a lot of evidence in favour of τ being meaningfully better and more correct than π. I have yet to see any evidence of the contrary that is not based on fallacies.

Why not provide both so that users can choose? Because then everyone who reads τ has to scratch their head for a minute and go down a rabbit hole of YouTube videos.

Not sure how YouTube got into the story. I'll concede that τ is less universally known, but a one line comment saying something like "the value of a full revolution in radians" would more than suffice to avoid any confusion.

By the way I just noticed the documentation for the pi constant says "The mathematical constant pi." which is awfully non-descriptive and assumes prior knowledge by the reader unnecessarily.

If you want to use it in your own code, it is trivial to define (...)

True. And I've done just that. But the fact that there exists a solution does not automatically qualify that solution as satisfactory, much less optimal. We are not discussing the necessity of tau being accepted as a built-in constant, we're discussing the merit.

I've presented evidence that there are benefits to doing it (in the form of an example), and the Manifesto shows that most popular formulas involving π become simpler with the use of τ. So from a practical perspective, there are clear benefits. And from a mathematical perspective, as the Manifesto argues, τ is more axiomatic.

If your concern is that users will be unhappy with the choice, well, Python has had it for over 10 years and there's been no talk of regret. In fact, it's become reasonably popular, with close to 6k distinct usages in GitHub as of today (and that's only including direct math.tau references). So at the minimum, I think it's safe to say that a few thousand Swift developers would be pleased with the addition, if the proportion of would-be τ-users is similar. Rust only currently has about a thousand usages, but the language itself is also much more uncommon, so arguably the ratio of adoption is higher.

There's a reason Unity and Processing have both adopted τ, and it's not mathematical pedantry. It does make graphics code easier to work with.

On the flip side, what is the actual downside of adding tau to Double? A fleeting 2 seconds of confusion for some, upon seeing the mention of a "tau" in code, before clicking through to the documentation to see its definition? Can you comfortably say that this outweighs the benefits of supporting more intuitive code?

As a personal note of motivation, I don't usually argue this for every language I come across. I'm only doing this because I'm genuinely in love with Swift and how concise and elegant it is, and I feel the choice to deny tau goes against those principles.

By the way I just noticed the documentation for the pi constant says "The mathematical constant pi." which is awfully non-descriptive and assumes prior knowledge by the reader unnecessarily.

I would be happy to review a PR against the standard library that improves the documentation.

Done! Let me know if I missed a step.