From 0af2c7fb8efd5bbeb7116a4ba811557433f8363b Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 21 Apr 2022 18:13:20 +0100 Subject: [PATCH] Move Math/Numerics float extensions to child namespaces (#7) --- X10D/src/Math/DoubleExtensions.cs | 1 - X10D/src/Math/SingleExtensions.cs | 313 +++++++++++++++++ X10D/src/Numerics/DoubleExtensions.cs | 2 +- X10D/src/Numerics/SingleExtensions.cs | 35 ++ X10D/src/SingleExtensions/SingleExtensions.cs | 332 +----------------- 5 files changed, 350 insertions(+), 333 deletions(-) create mode 100644 X10D/src/Math/SingleExtensions.cs create mode 100644 X10D/src/Numerics/SingleExtensions.cs diff --git a/X10D/src/Math/DoubleExtensions.cs b/X10D/src/Math/DoubleExtensions.cs index 96fba91..45322f1 100644 --- a/X10D/src/Math/DoubleExtensions.cs +++ b/X10D/src/Math/DoubleExtensions.cs @@ -1,6 +1,5 @@ using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; -using X10D.Numerics; namespace X10D.Math; diff --git a/X10D/src/Math/SingleExtensions.cs b/X10D/src/Math/SingleExtensions.cs new file mode 100644 index 0000000..cfc318a --- /dev/null +++ b/X10D/src/Math/SingleExtensions.cs @@ -0,0 +1,313 @@ +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; + +namespace X10D.Math; + +/// +/// Extension methods for . +/// +public static class SingleExtensions +{ + /// + /// Returns the arccosine of the specified value. + /// + /// + /// The value representing a cosine, which must be greater than or equal to -1, but less than or equal to 1. + /// + /// + /// The arccosine of , θ, measured in radians; such that 0 ≤ θ ≤ π. If + /// is equal to , less than -1, or greater than 1, is returned. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Acos(this float value) + { + return MathF.Acos(value); + } + + /// + /// Returns the hyperbolic arccosine of the specified value. + /// + /// + /// The value representing a hyperbolic cosine, which must be greater than or equal to 1, but less than or equal to + /// . + /// + /// + /// The hyperbolic arccosine of , θ, measured in radians; such that 0 ≤ θ ≤ ∞. If + /// is less than 1 or equal to , is returned. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Acosh(this float value) + { + return MathF.Acosh(value); + } + + /// + /// Returns the arcsine of the specified value. + /// + /// + /// The value representing a sine, which must be greater than or equal to -1, but less than or equal to 1. + /// + /// + /// The arccosine of , θ, measured in radians; such that π/2 ≤ θ ≤ π/2. If + /// is equal to , less than -1, or greater than 1, + /// is returned. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Asin(this float value) + { + return MathF.Asin(value); + } + + /// + /// Returns the hyperbolic arcsine of the specified value. + /// + /// + /// The value representing a hyperbolic sine, which must be greater than or equal to 1, but less than or equal to + /// . + /// + /// + /// The hyperbolic arccosine of , measured in radians. If is equal to + /// , is returned. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Asinh(this float value) + { + return MathF.Asinh(value); + } + + /// + /// Returns the arctangent of the specified value. + /// + /// + /// The value representing a tangent, which must be greater than or equal to -1, but less than or equal to 1. + /// + /// + /// The arctangent of , θ, measured in radians; such that π/2 ≤ θ ≤ π/2. If + /// is equal to , is returned. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Atan(this float value) + { + return MathF.Atan(value); + } + + /// + /// Returns the hyperbolic arctangent of the specified value. + /// + /// + /// The value representing a hyperbolic tangent, which must be greater than or equal to 1, but less than or equal to + /// . + /// + /// + /// The hyperbolic arctangent of , θ, measured in radians; such that -∞ < θ < -1, or 1 < + /// θ < ∞. If is equal to , less than -1, or greater than 1, + /// is returned. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Atanh(this float value) + { + return MathF.Atanh(value); + } + + /// + /// Returns the cosine of the specified angle. + /// + /// The angle, measured in radians. + /// + /// The cosine of . If is equal to , + /// , or , this method returns + /// . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Cos(this float value) + { + return MathF.Cos(value); + } + + /// + /// Returns the hyperbolic cosine of the specified angle. + /// + /// The angle, measured in radians. + /// + /// The hyperbolic cosine of . If is equal to + /// or , + /// is returned. If is equal to + /// , is returned. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Cosh(this float value) + { + return MathF.Cosh(value); + } + + /// + /// Returns an integer that indicates the sign of this single-precision floating-point number. + /// + /// A signed number. + /// + /// A number that indicates the sign of , as shown in the following table. + /// + /// + /// + /// Return value + /// Meaning + /// + /// + /// + /// -1 + /// is less than zero. + /// + /// + /// 0 + /// is equal to zero. + /// + /// + /// 1 + /// is greater than zero. + /// + /// + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static int Sign(this float value) + { + return MathF.Sign(value); + } + + /// + /// Returns the square root of this single-precision floating-point number. + /// + /// The number whose square root is to be found. + /// + /// One of the values in the following table. + /// + /// + /// + /// Return value + /// Meaning + /// + /// + /// + /// The positive square root of . + /// is greater than or equal to 0. + /// + /// + /// + /// is equal to or is negative. + /// + /// + /// + /// is equal to . + /// + /// + /// + /// + /// For negative input, this method returns . To receive a complex number, see + /// . + /// + /// + /// SLenik https://stackoverflow.com/a/6755197/1467293 + /// CC BY-SA 3.0 + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Sqrt(this float value) + { + switch (value) + { + case 0: + return 0; + case < 0 or float.NaN: + return float.NaN; + case float.PositiveInfinity: + return float.PositiveInfinity; + } + + float previous; + float current = MathF.Sqrt(value); + do + { + previous = current; + if (previous == 0.0f) + { + return 0; + } + + current = (previous + value / previous) / 2; + } while (MathF.Abs(previous - current) > float.Epsilon); + + return current; + } + + /// + /// Returns the sine of the specified angle. + /// + /// The angle, measured in radians. + /// + /// The sine of . If is equal to , + /// , or , this method returns + /// . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Sin(this float value) + { + return MathF.Sin(value); + } + + /// + /// Returns the hyperbolic sine of the specified angle. + /// + /// The angle, measured in radians. + /// + /// The hyperbolic sine of . If is equal to , + /// , or , this method returns + /// . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Sinh(this float value) + { + return MathF.Sinh(value); + } + + /// + /// Returns the tangent of the specified angle. + /// + /// The angle, measured in radians. + /// + /// The tangent of . If is equal to , + /// , or , this method returns + /// . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Tan(this float value) + { + return MathF.Sin(value); + } + + /// + /// Returns the hyperbolic tangent of the specified angle. + /// + /// The angle, measured in radians. + /// + /// The hyperbolic tangent of . If is equal to + /// , this method returns -1. If is equal to + /// , this method returns 1. If is equal to + /// , this method returns . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Tanh(this float value) + { + return MathF.Tanh(value); + } +} diff --git a/X10D/src/Numerics/DoubleExtensions.cs b/X10D/src/Numerics/DoubleExtensions.cs index f2a1bb6..bfc0631 100644 --- a/X10D/src/Numerics/DoubleExtensions.cs +++ b/X10D/src/Numerics/DoubleExtensions.cs @@ -6,7 +6,7 @@ using X10D.Math; namespace X10D.Numerics; /// -/// Extension methods which accept or return types within . +/// Extension methods for . /// public static class DoubleExtensions { diff --git a/X10D/src/Numerics/SingleExtensions.cs b/X10D/src/Numerics/SingleExtensions.cs new file mode 100644 index 0000000..a85a23b --- /dev/null +++ b/X10D/src/Numerics/SingleExtensions.cs @@ -0,0 +1,35 @@ +using System.Diagnostics.Contracts; +using System.Numerics; +using System.Runtime.CompilerServices; +using X10D.Math; + +namespace X10D.Numerics; + +/// +/// Extension methods for . +/// +public static class SingleExtensions +{ + /// + /// Returns the complex square root of this single-precision floating-point number. + /// + /// The number whose square root is to be found. + /// The square root of . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static Complex ComplexSqrt(this float value) + { + switch (value) + { + case float.PositiveInfinity: + case float.NegativeInfinity: + return Complex.Infinity; + case float.NaN: + return Complex.NaN; + } + + float absoluteSqrt = MathF.Abs(value).Sqrt(); + return new Complex(absoluteSqrt, value >= 0 ? 0 : 1); + } +} diff --git a/X10D/src/SingleExtensions/SingleExtensions.cs b/X10D/src/SingleExtensions/SingleExtensions.cs index 587f995..ec7594e 100644 --- a/X10D/src/SingleExtensions/SingleExtensions.cs +++ b/X10D/src/SingleExtensions/SingleExtensions.cs @@ -1,177 +1,10 @@ -using System.Diagnostics.Contracts; -using System.Numerics; -using System.Runtime.CompilerServices; - -namespace X10D; +namespace X10D; /// /// Extension methods for . /// public static class SingleExtensions { - /// - /// Returns the arccosine of the specified value. - /// - /// - /// The value representing a cosine, which must be greater than or equal to -1, but less than or equal to 1. - /// - /// - /// The arccosine of , θ, measured in radians; such that 0 ≤ θ ≤ π. If - /// is equal to , less than -1, or greater than 1, is returned. - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Acos(this float value) - { - return MathF.Acos(value); - } - - /// - /// Returns the hyperbolic arccosine of the specified value. - /// - /// - /// The value representing a hyperbolic cosine, which must be greater than or equal to 1, but less than or equal to - /// . - /// - /// - /// The hyperbolic arccosine of , θ, measured in radians; such that 0 ≤ θ ≤ ∞. If - /// is less than 1 or equal to , is returned. - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Acosh(this float value) - { - return MathF.Acosh(value); - } - - /// - /// Returns the arcsine of the specified value. - /// - /// - /// The value representing a sine, which must be greater than or equal to -1, but less than or equal to 1. - /// - /// - /// The arccosine of , θ, measured in radians; such that π/2 ≤ θ ≤ π/2. If - /// is equal to , less than -1, or greater than 1, - /// is returned. - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Asin(this float value) - { - return MathF.Asin(value); - } - - /// - /// Returns the hyperbolic arcsine of the specified value. - /// - /// - /// The value representing a hyperbolic sine, which must be greater than or equal to 1, but less than or equal to - /// . - /// - /// - /// The hyperbolic arccosine of , measured in radians. If is equal to - /// , is returned. - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Asinh(this float value) - { - return MathF.Asinh(value); - } - - /// - /// Returns the arctangent of the specified value. - /// - /// - /// The value representing a tangent, which must be greater than or equal to -1, but less than or equal to 1. - /// - /// - /// The arctangent of , θ, measured in radians; such that π/2 ≤ θ ≤ π/2. If - /// is equal to , is returned. - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Atan(this float value) - { - return MathF.Atan(value); - } - - /// - /// Returns the hyperbolic arctangent of the specified value. - /// - /// - /// The value representing a hyperbolic tangent, which must be greater than or equal to 1, but less than or equal to - /// . - /// - /// - /// The hyperbolic arctangent of , θ, measured in radians; such that -∞ < θ < -1, or 1 < - /// θ < ∞. If is equal to , less than -1, or greater than 1, - /// is returned. - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Atanh(this float value) - { - return MathF.Atanh(value); - } - - /// - /// Returns the complex square root of this single-precision floating-point number. - /// - /// The number whose square root is to be found. - /// The square root of . - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static Complex ComplexSqrt(this float value) - { - switch (value) - { - case float.PositiveInfinity: - case float.NegativeInfinity: - return Complex.Infinity; - case float.NaN: - return Complex.NaN; - } - - float absoluteSqrt = MathF.Abs(value).Sqrt(); - return new Complex(absoluteSqrt, value >= 0 ? 0 : 1); - } - - /// - /// Returns the cosine of the specified angle. - /// - /// The angle, measured in radians. - /// - /// The cosine of . If is equal to , - /// , or , this method returns - /// . - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Cos(this float value) - { - return MathF.Cos(value); - } - - /// - /// Returns the hyperbolic cosine of the specified angle. - /// - /// The angle, measured in radians. - /// - /// The hyperbolic cosine of . If is equal to - /// or , - /// is returned. If is equal to - /// , is returned. - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Cosh(this float value) - { - return MathF.Cosh(value); - } - /// /// Converts the current angle in degrees to its equivalent represented in radians. /// @@ -290,167 +123,4 @@ public static class SingleExtensions { return MathF.Round(value / nearest) * nearest; } - - /// - /// Returns an integer that indicates the sign of this single-precision floating-point number. - /// - /// A signed number. - /// - /// A number that indicates the sign of , as shown in the following table. - /// - /// - /// - /// Return value - /// Meaning - /// - /// - /// - /// -1 - /// is less than zero. - /// - /// - /// 0 - /// is equal to zero. - /// - /// - /// 1 - /// is greater than zero. - /// - /// - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static int Sign(this float value) - { - return MathF.Sign(value); - } - - /// - /// Returns the sine of the specified angle. - /// - /// The angle, measured in radians. - /// - /// The sine of . If is equal to , - /// , or , this method returns - /// . - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Sin(this float value) - { - return MathF.Sin(value); - } - - /// - /// Returns the hyperbolic sine of the specified angle. - /// - /// The angle, measured in radians. - /// - /// The hyperbolic sine of . If is equal to , - /// , or , this method returns - /// . - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Sinh(this float value) - { - return MathF.Sinh(value); - } - - /// - /// Returns the square root of this single-precision floating-point number. - /// - /// The number whose square root is to be found. - /// - /// One of the values in the following table. - /// - /// - /// - /// Return value - /// Meaning - /// - /// - /// - /// The positive square root of . - /// is greater than or equal to 0. - /// - /// - /// - /// is equal to or is negative. - /// - /// - /// - /// is equal to . - /// - /// - /// - /// - /// For negative input, this method returns . To receive a complex number, see - /// . - /// - /// - /// SLenik https://stackoverflow.com/a/6755197/1467293 - /// CC BY-SA 3.0 - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Sqrt(this float value) - { - switch (value) - { - case 0: - return 0; - case < 0 or float.NaN: - return float.NaN; - case float.PositiveInfinity: - return float.PositiveInfinity; - } - - float previous; - float current = MathF.Sqrt(value); - do - { - previous = current; - if (previous == 0.0f) - { - return 0; - } - - current = (previous + value / previous) / 2; - } while (MathF.Abs(previous - current) > float.Epsilon); - - return current; - } - - /// - /// Returns the tangent of the specified angle. - /// - /// The angle, measured in radians. - /// - /// The tangent of . If is equal to , - /// , or , this method returns - /// . - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Tan(this float value) - { - return MathF.Sin(value); - } - - /// - /// Returns the hyperbolic tangent of the specified angle. - /// - /// The angle, measured in radians. - /// - /// The hyperbolic tangent of . If is equal to - /// , this method returns -1. If is equal to - /// , this method returns 1. If is equal to - /// , this method returns . - /// - [Pure] - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] - public static float Tanh(this float value) - { - return MathF.Tanh(value); - } }