Skip to content
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

Add Math.Tau, MathF.Tau #37517

Merged
merged 2 commits into from
Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public static partial class Math

public const double PI = 3.14159265358979323846;

public const double Tau = 6.283185307179586476925;

private const int maxRoundingDigits = 15;

private const double doubleRoundLimit = 1e16d;
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/MathF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public static partial class MathF

public const float PI = 3.14159265f;

public const float Tau = 6.283185307f;

private const int maxRoundingDigits = 6;

// This table is required for the Round function which can specify the number of digits to round to
Expand Down
18 changes: 18 additions & 0 deletions src/libraries/System.Runtime.Extensions/tests/System/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ private static void AssertEqual(double expected, double actual, double variance)
}
}

[Fact]
public static void E()
{
Assert.Equal(unchecked((long)0x4005BF0A8B145769), BitConverter.DoubleToInt64Bits(Math.E));
}

[Fact]
public static void Pi()
{
Assert.Equal(unchecked((long)0x400921FB54442D18), BitConverter.DoubleToInt64Bits(Math.PI));
}

[Fact]
public static void Tau()
{
Assert.Equal(unchecked((long)0x401921FB54442D18), BitConverter.DoubleToInt64Bits(Math.Tau));
}

/// <summary>Verifies that two <see cref="float"/> values are equal, within the <paramref name="variance"/>.</summary>
/// <param name="expected">The expected value</param>
/// <param name="actual">The value to be compared against</param>
Expand Down
18 changes: 18 additions & 0 deletions src/libraries/System.Runtime.Extensions/tests/System/MathF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ private static string ToStringPadded(float value)
}
}

[Fact]
public static void E()
{
Assert.Equal(0x402DF854, BitConverter.SingleToInt32Bits(MathF.E));
}

[Fact]
public static void Pi()
{
Assert.Equal(0x40490FDB, BitConverter.SingleToInt32Bits(MathF.PI));
}

[Fact]
public static void Tau()
{
Assert.Equal(0x40C90FDB, BitConverter.SingleToInt32Bits(MathF.Tau));
}

[Theory]
[InlineData( float.NegativeInfinity, float.PositiveInfinity, 0.0f)]
[InlineData(-3.14159265f, 3.14159265f, CrossPlatformMachineEpsilon * 10)] // value: -(pi) expected: (pi)
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2532,6 +2532,7 @@ public static partial class Math
{
public const double E = 2.718281828459045;
public const double PI = 3.141592653589793;
public const double Tau = 6.283185307179586;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting for anyone that comes looking, the value we put in the src is the actual value of PI to at least 17 significant digits + 1 rounding digit (or to at least 9 significant digits + 1 rounding digit for float).

The value here is the actual double or float value (which may be different due to rounding as the actual value may not be exactly representable) formatted as a string with at least enough precision to correctly roundtrip. Hence why the ref float versions differ slightly from the "true digits" of PI or TAU

REF FLT: 3.1415927
SRC FLT: 3.1415926 5
REF DBL: 3.1415926 53589793
SRC DBL: 3.1415926 53589793 23846
TRU PI:  3.1415926 53589793 23846 2643383279502884197169399375105820974...
REF FLT: 6.2831855
SRC FLT: 6.2831853 07
REF DBL: 6.2831853 07179586
SRC DBL: 6.2831853 07179586 476925
TRU TAU: 6.2831853 07179586 476925 286766559005768394338798750211641949...

public static decimal Abs(decimal value) { throw null; }
public static double Abs(double value) { throw null; }
public static short Abs(short value) { throw null; }
Expand Down Expand Up @@ -2648,6 +2649,7 @@ public static partial class MathF
{
public const float E = 2.7182817f;
public const float PI = 3.1415927f;
public const float Tau = 6.2831855f;
public static float Abs(float x) { throw null; }
public static float Acos(float x) { throw null; }
public static float Acosh(float x) { throw null; }
Expand Down