Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add AsinPi, AcosPi, AtanPi and Atan2Pi
  • Loading branch information
huoyaoyuan committed Jun 4, 2021
commit ced1e7e8e90a76349cc2b99986f4749a910514d5
63 changes: 63 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1620,5 +1620,68 @@ public static double TanPi(double x)
double tan = Tan(rem * PI);
return invert ? -tan : tan;
}

// We don't need to special case inverse-trigs as long as getting precise PI/2 and PI.
// Guarded in unit test.

/// <summary>
/// Returns the angle measured in half-turns whose sine is the specified number.
/// </summary>
/// <param name="x">A number representing a sine, where <paramref name="x"/> must be greater than or equal to -1, but
/// less than or equal to 1.</param>
/// <returns>An angle, θ, measured in half-turns, such that -0.5 ≤ θ ≤ 0.5.
/// -or-
/// <see cref="double.NaN"/> if <paramref name="x"/> &lt; -1 or <paramref name="x"/> &gt; 1
/// or <paramref name="x"/> equals <see cref="double.NaN"/>.</returns>
public static double AsinPi(double x) => Asin(x) / PI;

/// <summary>
/// Returns the angle measured in half-turns whose cosine is the specified number.
/// </summary>
/// <param name="x">A number representing a cosine, where <paramref name="x"/> must be greater than or equal to -1, but
/// less than or equal to 1.</param>
/// <returns>An angle, θ, measured in half-turns, such that -0.5 ≤ θ ≤ 0.5.
/// -or-
/// <see cref="double.NaN"/> if <paramref name="x"/> &lt; -1 or <paramref name="x"/> &gt; 1
/// or <paramref name="x"/> equals <see cref="double.NaN"/>.</returns>
public static double AcosPi(double x) => Acos(x) / PI;

/// <summary>
/// Returns the angle measured in half-turns whose tangent is the specified number.
/// </summary>
/// <param name="x">A number representing a tangent.</param>
/// <returns>An angle, θ, measured in half-turns, such that -0.5 ≤ θ ≤ 0.5.
/// -or-
/// <see cref="double.NaN"/> if <paramref name="x"/> equals <see cref="double.NaN"/>,
/// -0.5 if <paramref name="x"/> equals <see cref="double.NegativeInfinity"/>,
/// or 0.5 if <paramref name="x"/> equals <see cref="double.PositiveInfinity"/>.</returns>
public static double AtanPi(double x) => Atan(x) / PI;

/// <summary>
/// Returns the angle whose measured in half-turns tangent is the quotient of two specified numbers.
/// </summary>
/// <param name="y">The y coordinate of a point.</param>
/// <param name="x">The x coordinate of a point.</param>
/// <returns>An angle, θ, measured in half-turns, such that -1 ≤ θ ≤ 1, and tan(θ) = y / x,
/// where (x, y) is a point in the Cartesian plane. Observe the following:
/// <list type="bullet">
/// <item>For (x, y) in quadrant 1, 0 &lt; θ &lt; 0.5. </item>
/// <item>For (x, y) in quadrant 2, 0.5 &lt; θ ≤ 1. </item>
/// <item>For (x, y) in quadrant 3, -1 &lt; θ &lt; -0.5. </item>
/// <item>For (x, y) in quadrant 4, -0.5 &lt; θ &lt; 0. </item>
/// </list>
/// For points on the boundaries of the quadrants, the return value is the following:
/// <list type="bullet">
/// <item>If y is 0 and x is not negative, θ = 0.</item>
/// <item>If y is 0 and x is negative, θ = 1.</item>
/// <item>If y is positive and x is 0, θ = 0.5.</item>
/// <item>If y is negative and x is 0, θ = -0.5.</item>
/// <item>If y is 0 and x is 0, θ = 0.</item>
/// <item>If y is 0 and x is 0, θ = 0.</item>
/// </list>
/// If x or y is <see cref="double.NaN"/>, or if x and y are either
/// <see cref="double.PositiveInfinity"/> or <see cref="double.NegativeInfinity"/>,
/// the method returns <see cref="double.NaN"/>.</returns>
public static double Atan2Pi(double y, double x) => Atan2(y, x) / PI;
}
}
63 changes: 63 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/MathF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -691,5 +691,68 @@ public static float TanPi(float x)
float tan = Tan(rem * PI);
return invert ? -tan : tan;
}

// We don't need to special case inverse-trigs as long as getting precise PI/2 and PI.
// Guarded in unit test.

/// <summary>
/// Returns the angle measured in half-turns whose sine is the specified number.
/// </summary>
/// <param name="x">A number representing a sine, where <paramref name="x"/> must be greater than or equal to -1, but
/// less than or equal to 1.</param>
/// <returns>An angle, θ, measured in half-turns, such that -0.5 ≤ θ ≤ 0.5.
/// -or-
/// <see cref="float.NaN"/> if <paramref name="x"/> &lt; -1 or <paramref name="x"/> &gt; 1
/// or <paramref name="x"/> equals <see cref="float.NaN"/>.</returns>
public static float AsinPi(float x) => Asin(x) / PI;

/// <summary>
/// Returns the angle measured in half-turns whose cosine is the specified number.
/// </summary>
/// <param name="x">A number representing a cosine, where <paramref name="x"/> must be greater than or equal to -1, but
/// less than or equal to 1.</param>
/// <returns>An angle, θ, measured in half-turns, such that -0.5 ≤ θ ≤ 0.5.
/// -or-
/// <see cref="float.NaN"/> if <paramref name="x"/> &lt; -1 or <paramref name="x"/> &gt; 1
/// or <paramref name="x"/> equals <see cref="float.NaN"/>.</returns>
public static float AcosPi(float x) => Acos(x) / PI;

/// <summary>
/// Returns the angle measured in half-turns whose tangent is the specified number.
/// </summary>
/// <param name="x">A number representing a tangent.</param>
/// <returns>An angle, θ, measured in half-turns, such that -0.5 ≤ θ ≤ 0.5.
/// -or-
/// <see cref="float.NaN"/> if <paramref name="x"/> equals <see cref="float.NaN"/>,
/// -0.5 if <paramref name="x"/> equals <see cref="float.NegativeInfinity"/>,
/// or 0.5 if <paramref name="x"/> equals <see cref="float.PositiveInfinity"/>.</returns>
public static float AtanPi(float x) => Atan(x) / PI;

/// <summary>
/// Returns the angle whose measured in half-turns tangent is the quotient of two specified numbers.
/// </summary>
/// <param name="y">The y coordinate of a point.</param>
/// <param name="x">The x coordinate of a point.</param>
/// <returns>An angle, θ, measured in half-turns, such that -1 ≤ θ ≤ 1, and tan(θ) = y / x,
/// where (x, y) is a point in the Cartesian plane. Observe the following:
/// <list type="bullet">
/// <item>For (x, y) in quadrant 1, 0 &lt; θ &lt; 0.5. </item>
/// <item>For (x, y) in quadrant 2, 0.5 &lt; θ ≤ 1. </item>
/// <item>For (x, y) in quadrant 3, -1 &lt; θ &lt; -0.5. </item>
/// <item>For (x, y) in quadrant 4, -0.5 &lt; θ &lt; 0. </item>
/// </list>
/// For points on the boundaries of the quadrants, the return value is the following:
/// <list type="bullet">
/// <item>If y is 0 and x is not negative, θ = 0.</item>
/// <item>If y is 0 and x is negative, θ = 1.</item>
/// <item>If y is positive and x is 0, θ = 0.5.</item>
/// <item>If y is negative and x is 0, θ = -0.5.</item>
/// <item>If y is 0 and x is 0, θ = 0.</item>
/// <item>If y is 0 and x is 0, θ = 0.</item>
/// </list>
/// If x or y is <see cref="float.NaN"/>, or if x and y are either
/// <see cref="float.PositiveInfinity"/> or <see cref="float.NegativeInfinity"/>,
/// the method returns <see cref="float.NaN"/>.</returns>
public static float Atan2Pi(float y, float x) => Atan2(y, x) / PI;
}
}
8 changes: 8 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2751,11 +2751,15 @@ public static partial class Math
public static float Abs(float value) { throw null; }
public static double Acos(double d) { throw null; }
public static double Acosh(double d) { throw null; }
public static double AcosPi(double x) { throw null; }
public static double Asin(double d) { throw null; }
public static double Asinh(double d) { throw null; }
public static double AsinPi(double x) { throw null; }
public static double Atan(double d) { throw null; }
public static double Atan2(double y, double x) { throw null; }
public static double Atan2Pi(double y, double x) { throw null; }
public static double Atanh(double d) { throw null; }
public static double AtanPi(double x) { throw null; }
public static long BigMul(int a, int b) { throw null; }
public static long BigMul(long a, long b, out long low) { throw null; }
[System.CLSCompliantAttribute(false)]
Expand Down Expand Up @@ -2892,11 +2896,15 @@ public static partial class MathF
public static float Abs(float x) { throw null; }
public static float Acos(float x) { throw null; }
public static float Acosh(float x) { throw null; }
public static float AcosPi(float x) { throw null; }
public static float Asin(float x) { throw null; }
public static float Asinh(float x) { throw null; }
public static float AsinPi(float x) { throw null; }
public static float Atan(float x) { throw null; }
public static float Atan2(float y, float x) { throw null; }
public static float Atan2Pi(float y, float x) { throw null; }
public static float Atanh(float x) { throw null; }
public static float AtanPi(float x) { throw null; }
public static float BitDecrement(float x) { throw null; }
public static float BitIncrement(float x) { throw null; }
public static float Cbrt(float x) { throw null; }
Expand Down