-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
API Proposal: Add Tau to System.Math(F) class #24678
Comments
Only |
Why not have more exposed constants? They are useful, after all. |
Are they? Let me turn that around and ask, why? Is it super common to maintain your own tau and half-pi constants, such that this would lift a burden from people? That's new to me. The fact that you seem to not be recognizing xkcd’s ‘pau’ as a joke raises a red flag in my mind. |
I don't have any specific preference for or against exposing them. There are some libm implementations that expose similar constants, but they also haven't been exposed as part of the "public surface area" in the actual C/C++ standards. They do represent core values that various math libraries may need to consume/produce; However, they are also just constants with well-defined values, so it isn't difficult for any particular library to define (and has no impact on the IL generated outside of having a member you can discover via reflection). |
If they're simple to define in any library, why not define them in this library? |
|
@aaronfranke, if you do want to "officially" propose CoreFX add the APIs, you should follow the steps here and submit a "formal" API proposal (probably by just updating the OP, rather than opening a new issue). |
How common are these constants? How often are they used by System.Math users?
We don't add every API we can, but only APIs, which are useful for a wider developer audience and are truly used / useful in real-world scenarios. Otherwise we would drown in lots of marginal APIs - making it harder for developers to choose which APIs to use. Everything comes at a cost - general API surface ease-of-use in this case. |
Tau is not very commonly used, but it has its advocates (though the symbol has also been used for ½π and for the golden ratio instead of φ). Pau was invented by Randal Munroe to make fun of people who argue about whether or not we should use Tau. |
@karelz, they are fairly core mathematical constants that are used in multiple places (we use many of them for validating correctness in our Math APIs, and I've seen a few scattered about in both managed and native land for CoreFX/CoreCLR). They are generally defined to account for minor inaccuracies in the floating-point format (e.g. However, as indicated above, since these are well-defined constants, it is also trivial to add them to your own code base (but equally as trivial to add them to CoreFX). LibM defines the following: https://www.gnu.org/software/libc/manual/html_node/Mathematical-Constants.html#Mathematical-Constants. If we were to add any constants, I would think they should be these I would also vote we use names like |
In theory, TwoPi and HalfPi have the exact same precision as PI but the exponent is incremented or decremented, right? Things like M_1_PI make a lot more sense to me. |
Basically, yes. const double pi_d = 3.1415926535897932; // 0x400921FB54442D18
const double two_pi_d = 6.2831853071795865; // 0x401921FB54442D18
const double half_pi_d = 1.5707963267948966; // 0x3FF921FB54442D18
const float pi_f = 3.14159265f; // 0x40490FDB
const float two_pi_f = 6.28318531f; // 0x40C90FDB
const float half_pi_f = 1.57079633f; // 0x3FC90FDB |
And, for the default rounding mode, |
In default rounding mode, how is it possible for the most precise representations of |
Denormal numbers, where the exponent is zero 😄 |
Oh, gotcha. But we aren't talking about adding constants that small, right? |
No. But there are other multipliers ( |
Yes. I would expect actual multiplication to cause that. Though in the particular case of 0.75*pi, I'm getting 0x4002D97C7F3321D2 no matter how I calculate it as well as when letting the compiler pick the representation given a literal with more than enough digits. (That makes particular sense to me because the only prime factor in the divisor is two, so we're only doing integer multiplication in the mantissa.) So the only advantage in adding any of these eighths increments of a circle would have to be as Karel said– they have to be of routine interest to people looking through the API. |
There are numerous constants available in math.h / cmath.h... https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx when you define _USE_MATH_DEFINES for c/c++. Seems like it'd be pretty simple to include these in the .Net System.Math and they are obviously useful. |
This issue hasn't seen activity in over a year. Is there demand for this API? If not, we should probably close the issue if we're not still collecting information on whether these would be useful in the public surface area. |
There really isn't much to discuss, it's a simple feature. I use There is demand from me. I usually use |
I'm against exposing Furthermore it's quite easy to expose "derived" constants via So let's keep it simple and clear. |
@aaronfranke, I'll take this to API review if you could update the original post to follow the "good example" listed under step 1 of our [API Review Process](https://github.com/dotnet/runtime/blob/master/docs/project/api-review-process.md. You don't strictly speaking need all the sections, but providing the rationale/usage and proposed API sections would be ideal as it helps the API review process overall. For |
@tannergooding Thanks, I've went ahead and done that (using my Color proposal as a reference). |
@tannergooding Interestingly, the CRT math constants you linked to define all kind of other variants, but not the specific value 2π. :) |
@GrabYourPitchforks, ah you're right, from the // Definitions of useful mathematical constants
//
// Define _USE_MATH_DEFINES before including <math.h> to expose these macro
// definitions for common math constants. These are placed under an #ifdef
// since these commonly-defined names are not part of the C or C++ standards
#define M_E 2.71828182845904523536 // e
#define M_LOG2E 1.44269504088896340736 // log2(e)
#define M_LOG10E 0.434294481903251827651 // log10(e)
#define M_LN2 0.693147180559945309417 // ln(2)
#define M_LN10 2.30258509299404568402 // ln(10)
#define M_PI 3.14159265358979323846 // pi
#define M_PI_2 1.57079632679489661923 // pi/2
#define M_PI_4 0.785398163397448309616 // pi/4
#define M_1_PI 0.318309886183790671538 // 1/pi
#define M_2_PI 0.636619772367581343076 // 2/pi
#define M_2_SQRTPI 1.12837916709551257390 // 2/sqrt(pi)
#define M_SQRT2 1.41421356237309504880 // sqrt(2)
#define M_SQRT1_2 0.707106781186547524401 // 1/sqrt(2) |
@tannergooding It sounds like we also have the opportunity to introduce this constant with the |
Approved as Tau: public partial class Math
{
public const double Tau = 6.283185307179586476925;
}
public partial class MathF
{
public const float Tau = 6.283185307f;
} |
Can I take this? |
Assigned out, thanks @john-h-k! |
This is an API proposal for adding
Tau
toSystem.Math
andSystem.MathF
. These values would be exactly equal to 2 timesMath.PI
and 2 timesMathF.PI
respectively, and would represent a full rotation around the unit circle in radians.Rationale and usage
The
System.Math
class currently hasPI
, but there is one other circle constant that is quite common and useful that is missing: Tau (τ
), which is2π
, or about6.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, anddouble 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
andMath.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.
delta
which represents the time passed since last frame, code likerotation += delta * Math.Tau;
will rotate once per second. This can then simply be multiplied with the desired rate per second. WithoutMath.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 writevalue % Math.PI * 2
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 toPI
"out of the box".Proposed API
The following line would be added to
System.Math
:The following line would be added to
System.MathF
: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 thinkTau
is a better choice. The nameTau
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)
The text was updated successfully, but these errors were encountered: