Skip to content

API Proposal: Add Tau to System.Math(F) class #24678

@aaronfranke

Description

@aaronfranke

This is an API proposal for adding Tau to System.Math and System.MathF. These values would be exactly equal to 2 times Math.PI and 2 times MathF.PI respectively, and would represent a full rotation around the unit circle in radians.

Rationale and usage

The System.Math class currently has PI, but there is one other circle constant that is quite common and useful that is missing: Tau (τ), which is , or about 6.283185307179586476925.

Tau represents a rotation all the way around the circle, in radians. As demonstrated in the Tau manifesto, this value is extremely common throughout numerous mathematical functions.

In my own experience writing code that works with angles and rotations, I have found that using Tau results in much cleaner code. Some examples:

  • If you need to write down a rotation in your code, it is much easier to understand when expressed in terms of Tau.

    • It is immediately clear that double rotation = Math.Tau / 3; is a rotation one third of the way around the unit circle in radians, and double rotation = Math.Tau * 0.6; is a rotation 60% of the way around the unit circle in radians.

    • Using Pi, those values would have to be written as Math.PI * 2 / 3 and Math.PI * 1.2 (or * 2 * 0.6) respectively, which is much less clear.

    • Here's an image pulled from the Tau manifesto to illustrate the point:

  • If you have rotation represented as a ratio (0 to 1) of how far it is around the unit circle, then you can multiply that ratio by Tau to get a rotation around the unit circle of radians.

    • If you have code that works iteratively, and you have a variable called delta which represents the time passed since last frame, code like rotation += delta * Math.Tau; will rotate once per second. This can then simply be multiplied with the desired rate per second. Without Math.Tau, you would need to multiply by 2, adding complexity and confusion.
  • If you are accepting input as a rotation and need to normalize the input, you could use value % Math.Tau to normalize the input. With Pi, you would need to write value % Math.PI * 2

    • Note that if you need to handle negative values, you would also need a helper method.
  • While I'm not an expert on audio, just from a search in Godot's source code, it seems that code working with audio waveforms deals with Tau (or 2*Pi) very frequently as well.

Tau is used commonly in the Godot game engine.

Obviously I could just create my own helper class for this, but it would be better if it was implemented in .NET and C#, available for all users of .NET and C#.

Not everyone would use it, and that's fine, but I think it is best if .NET and C# encourage writing cleaner code by allowing developers to use Tau in addition to PI "out of the box".

Proposed API

The following line would be added to System.Math:

public const double Tau = 6.283185307179586476925;

The following line would be added to System.MathF:

public const float Tau = 6.283185307f;

Details

If you need more details, consider reading through the Tau manifesto.

Open Questions

The name TwoPi has also been suggested. It should be considered, but I think Tau is a better choice. The name Tau has been popularized by articles such as the Tau manifesto. The Greek letter τ (tau) resembles a T, which can stand for "turn", and it is visually similar to the Greek letter π (pi).

Pull Request

None so far.

Updates

#24678 (comment)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions