Move math-related extensions to child namespace (#7)

This commit is contained in:
Oliver Booth 2022-04-21 17:43:56 +01:00
parent 95d61eff2c
commit 95020e0db0
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
10 changed files with 370 additions and 354 deletions

View File

@ -1,5 +1,7 @@
using System.Diagnostics;
using System.Numerics;
using X10D.Math;
using X10D.Numerics;
namespace X10D.Tests.Core;

View File

@ -78,6 +78,6 @@ public static class SByteExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this sbyte value)
{
return Math.Sign(value);
return System.Math.Sign(value);
}
}

View File

@ -1,177 +1,10 @@
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace X10D;
namespace X10D;
/// <summary>
/// Extension methods for <see cref="double" />.
/// </summary>
public static class DoubleExtensions
{
/// <summary>
/// Returns the arccosine of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a cosine, which must be greater than or equal to -1, but less than or equal to 1.
/// </param>
/// <returns>
/// The arccosine of <paramref name="value" />, θ, measured in radians; such that 0 ≤ θ ≤ π. If <paramref name="value" />
/// is equal to <see cref="double.NaN" />, less than -1, or greater than 1, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Acos(this double value)
{
return Math.Acos(value);
}
/// <summary>
/// Returns the hyperbolic arccosine of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a hyperbolic cosine, which must be greater than or equal to 1, but less than or equal to
/// <see cref="double.PositiveInfinity" />.
/// </param>
/// <returns>
/// The hyperbolic arccosine of <paramref name="value" />, θ, measured in radians; such that 0 ≤ θ ≤ ∞. If
/// <paramref name="value" /> is less than 1 or equal to <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Acosh(this double value)
{
return Math.Acosh(value);
}
/// <summary>
/// Returns the arcsine of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a sine, which must be greater than or equal to -1, but less than or equal to 1.
/// </param>
/// <returns>
/// The arccosine of <paramref name="value" />, θ, measured in radians; such that π/2 ≤ θ ≤ π/2. If
/// <paramref name="value" /> is equal to <see cref="double.NaN" />, less than -1, or greater than 1,
/// <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Asin(this double value)
{
return Math.Asin(value);
}
/// <summary>
/// Returns the hyperbolic arcsine of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a hyperbolic sine, which must be greater than or equal to 1, but less than or equal to
/// <see cref="double.PositiveInfinity" />.
/// </param>
/// <returns>
/// The hyperbolic arccosine of <paramref name="value" />, measured in radians. If <paramref name="value" /> is equal to
/// <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Asinh(this double value)
{
return Math.Asinh(value);
}
/// <summary>
/// Returns the arctangent of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a tangent, which must be greater than or equal to -1, but less than or equal to 1.
/// </param>
/// <returns>
/// The arctangent of <paramref name="value" />, θ, measured in radians; such that π/2 ≤ θ ≤ π/2. If
/// <paramref name="value" /> is equal to <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Atan(this double value)
{
return Math.Atan(value);
}
/// <summary>
/// Returns the hyperbolic arctangent of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a hyperbolic tangent, which must be greater than or equal to 1, but less than or equal to
/// <see cref="double.PositiveInfinity" />.
/// </param>
/// <returns>
/// The hyperbolic arctangent of <paramref name="value" />, θ, measured in radians; such that -∞ &lt; θ &lt; -1, or 1 &lt;
/// θ &lt; ∞. If <paramref name="value" /> is equal to <see cref="double.NaN" />, less than -1, or greater than 1,
/// <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Atanh(this double value)
{
return Math.Atanh(value);
}
/// <summary>
/// Returns the complex square root of this double-precision floating-point number.
/// </summary>
/// <param name="value">The number whose square root is to be found.</param>
/// <returns>The square root of <paramref name="value" />.</returns>
/// <seealso cref="Sqrt" />
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static Complex ComplexSqrt(this double value)
{
switch (value)
{
case double.PositiveInfinity:
case double.NegativeInfinity:
return Complex.Infinity;
case double.NaN:
return Complex.NaN;
}
double absoluteSqrt = Math.Abs(value).Sqrt();
return new Complex(absoluteSqrt, value >= 0 ? 0 : 1);
}
/// <summary>
/// Returns the cosine of the specified angle.
/// </summary>
/// <param name="value">The angle, measured in radians.</param>
/// <returns>
/// The cosine of <paramref name="value" />. If <paramref name="value" /> is equal to <see cref="double.NaN" />,
/// <see cref="double.NegativeInfinity" />, or <see cref="double.PositiveInfinity" />, this method returns
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Cos(this double value)
{
return Math.Cos(value);
}
/// <summary>
/// Returns the hyperbolic cosine of the specified angle.
/// </summary>
/// <param name="value">The angle, measured in radians.</param>
/// <returns>
/// The hyperbolic cosine of <paramref name="value" />. If <paramref name="value" /> is equal to
/// <see cref="double.NegativeInfinity" /> or <see cref="double.PositiveInfinity" />,
/// <see cref="double.PositiveInfinity" /> is returned. If <paramref name="value" /> is equal to
/// <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Cosh(this double value)
{
return Math.Cosh(value);
}
/// <summary>
/// Converts the current angle in degrees to its equivalent represented in radians.
/// </summary>
@ -179,7 +12,7 @@ public static class DoubleExtensions
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
public static double DegreesToRadians(this double value)
{
return value * (Math.PI / 180.0);
return value * (System.Math.PI / 180.0);
}
/// <summary>
@ -202,7 +35,7 @@ public static class DoubleExtensions
/// </returns>
public static bool IsEven(this double value)
{
return Math.Abs(value % 2.0) < double.Epsilon;
return System.Math.Abs(value % 2.0) < double.Epsilon;
}
/// <summary>
@ -267,7 +100,7 @@ public static class DoubleExtensions
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
public static double RadiansToDegrees(this double value)
{
return value * (180.0 / Math.PI);
return value * (180.0 / System.Math.PI);
}
/// <summary>
@ -288,170 +121,6 @@ public static class DoubleExtensions
/// <returns><paramref name="value" /> rounded to the nearest multiple of <paramref name="nearest" />.</returns>
public static double Round(this double value, double nearest)
{
return Math.Round(value / nearest) * nearest;
}
/// <summary>
/// Returns an integer that indicates the sign of this double-precision floating-point number.
/// </summary>
/// <param name="value">A signed number.</param>
/// <returns>
/// A number that indicates the sign of <paramref name="value" />, as shown in the following table.
///
/// <list type="table">
/// <listheader>
/// <term>Return value</term>
/// <description>Meaning</description>
/// </listheader>
///
/// <item>
/// <term>-1</term>
/// <description><paramref name="value" /> is less than zero.</description>
/// </item>
/// <item>
/// <term>0</term>
/// <description><paramref name="value" /> is equal to zero.</description>
/// </item>
/// <item>
/// <term>1</term>
/// <description><paramref name="value" /> is greater than zero.</description>
/// </item>
/// </list>
/// </returns>
/// <exception cref="ArithmeticException"><paramref name="value" /> is equal to <see cref="double.NaN" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this double value)
{
return Math.Sign(value);
}
/// <summary>
/// Returns the sine of the specified angle.
/// </summary>
/// <param name="value">The angle, in radians.</param>
/// <returns>
/// The sine of <paramref name="value" />. If <paramref name="value" /> is equal to <see cref="double.NaN" />,
/// <see cref="double.NegativeInfinity" />, or <see cref="double.PositiveInfinity" />, this method returns
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Sin(this double value)
{
return Math.Sin(value);
}
/// <summary>
/// Returns the hyperbolic sine of the specified angle.
/// </summary>
/// <param name="value">The angle, in radians.</param>
/// <returns>
/// The hyperbolic sine of <paramref name="value" />. If <paramref name="value" /> is equal to <see cref="double.NaN" />,
/// <see cref="double.NegativeInfinity" />, or <see cref="double.PositiveInfinity" />, this method returns
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Sinh(this double value)
{
return Math.Sinh(value);
}
/// <summary>
/// Returns the square root of this double-precision floating-point number.
/// </summary>
/// <param name="value">The number whose square root is to be found.</param>
/// <returns>
/// One of the values in the following table.
///
/// <list type="table">
/// <listheader>
/// <term>Return value</term>
/// <description>Meaning</description>
/// </listheader>
///
/// <item>
/// <term>The positive square root of <paramref name="value" />.</term>
/// <description><paramref name="value" /> is greater than or equal to 0.</description>
/// </item>
/// <item>
/// <term><see cref="double.NaN" /></term>
/// <description><paramref name="value" /> is equal to <see cref="double.NaN" /> or is negative.</description>
/// </item>
/// <item>
/// <term><see cref="double.PositiveInfinity" /></term>
/// <description><paramref name="value" /> is equal to <see cref="double.PositiveInfinity" />.</description>
/// </item>
/// </list>
/// </returns>
/// <remarks>
/// For negative input, this method returns <see cref="double.NaN" />. To receive a complex number, see
/// <see cref="ComplexSqrt" />.
/// </remarks>
/// <seealso cref="ComplexSqrt" />
/// <author>SLenik https://stackoverflow.com/a/6755197/1467293</author>
/// <license>CC BY-SA 3.0</license>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Sqrt(this double value)
{
switch (value)
{
case 0:
return 0;
case < 0 or double.NaN:
return double.NaN;
case double.PositiveInfinity:
return double.PositiveInfinity;
}
double previous;
double current = Math.Sqrt(value);
do
{
previous = current;
if (previous == 0.0)
{
return 0;
}
current = (previous + value / previous) / 2;
} while (Math.Abs(previous - current) > double.Epsilon);
return current;
}
/// <summary>
/// Returns the tangent of the specified angle.
/// </summary>
/// <param name="value">The angle, measured in radians.</param>
/// <returns>
/// The tangent of <paramref name="value" />. If <paramref name="value" /> is equal to <see cref="double.NaN" />,
/// <see cref="double.NegativeInfinity" />, or <see cref="double.PositiveInfinity" />, this method returns
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Tan(this double value)
{
return Math.Tan(value);
}
/// <summary>
/// Returns the hyperbolic tangent of the specified angle.
/// </summary>
/// <param name="value">The angle, measured in radians.</param>
/// <returns>
/// The hyperbolic tangent of <paramref name="value" />. If <paramref name="value" /> is equal to
/// <see cref="double.NegativeInfinity" />, this method returns -1. If <paramref name="value" /> is equal to
/// <see cref="double.PositiveInfinity" />, this method returns 1. If <paramref name="value" /> is equal to
/// <see cref="double.NaN" />, this method returns <see cref="double.NaN" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Tanh(this double value)
{
return Math.Tanh(value);
return System.Math.Round(value / nearest) * nearest;
}
}

View File

@ -242,7 +242,7 @@ public static class Int16Extensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this short value)
{
return Math.Sign(value);
return System.Math.Sign(value);
}
/// <summary>
@ -317,7 +317,7 @@ public static class Int16Extensions
}
else
{
short absStep = Math.Abs(step);
short absStep = System.Math.Abs(step);
for (short i = start; i >= end; i -= absStep)
{
yield return i;

View File

@ -294,7 +294,7 @@ public static class Int32Extensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this int value)
{
return Math.Sign(value);
return System.Math.Sign(value);
}
/// <summary>
@ -369,7 +369,7 @@ public static class Int32Extensions
}
else
{
int absStep = Math.Abs(step);
int absStep = System.Math.Abs(step);
for (int i = start; i >= end; i -= absStep)
{
yield return i;

View File

@ -267,7 +267,7 @@ public static class Int64Extensions
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this long value)
{
return Math.Sign(value);
return System.Math.Sign(value);
}
/// <summary>
@ -342,7 +342,7 @@ public static class Int64Extensions
}
else
{
long absStep = Math.Abs(step);
long absStep = System.Math.Abs(step);
for (long i = start; i >= end; i -= absStep)
{
yield return i;

View File

@ -0,0 +1,315 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using X10D.Numerics;
namespace X10D.Math;
/// <summary>
/// Mathematical extension methods.
/// </summary>
public static class DoubleExtensions
{
/// <summary>
/// Returns the arccosine of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a cosine, which must be greater than or equal to -1, but less than or equal to 1.
/// </param>
/// <returns>
/// The arccosine of <paramref name="value" />, θ, measured in radians; such that 0 ≤ θ ≤ π. If <paramref name="value" />
/// is equal to <see cref="double.NaN" />, less than -1, or greater than 1, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Acos(this double value)
{
return System.Math.Acos(value);
}
/// <summary>
/// Returns the hyperbolic arccosine of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a hyperbolic cosine, which must be greater than or equal to 1, but less than or equal to
/// <see cref="double.PositiveInfinity" />.
/// </param>
/// <returns>
/// The hyperbolic arccosine of <paramref name="value" />, θ, measured in radians; such that 0 ≤ θ ≤ ∞. If
/// <paramref name="value" /> is less than 1 or equal to <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Acosh(this double value)
{
return System.Math.Acosh(value);
}
/// <summary>
/// Returns the arcsine of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a sine, which must be greater than or equal to -1, but less than or equal to 1.
/// </param>
/// <returns>
/// The arccosine of <paramref name="value" />, θ, measured in radians; such that π/2 ≤ θ ≤ π/2. If
/// <paramref name="value" /> is equal to <see cref="double.NaN" />, less than -1, or greater than 1,
/// <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Asin(this double value)
{
return System.Math.Asin(value);
}
/// <summary>
/// Returns the hyperbolic arcsine of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a hyperbolic sine, which must be greater than or equal to 1, but less than or equal to
/// <see cref="double.PositiveInfinity" />.
/// </param>
/// <returns>
/// The hyperbolic arccosine of <paramref name="value" />, measured in radians. If <paramref name="value" /> is equal to
/// <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Asinh(this double value)
{
return System.Math.Asinh(value);
}
/// <summary>
/// Returns the arctangent of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a tangent, which must be greater than or equal to -1, but less than or equal to 1.
/// </param>
/// <returns>
/// The arctangent of <paramref name="value" />, θ, measured in radians; such that π/2 ≤ θ ≤ π/2. If
/// <paramref name="value" /> is equal to <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Atan(this double value)
{
return System.Math.Atan(value);
}
/// <summary>
/// Returns the hyperbolic arctangent of the specified value.
/// </summary>
/// <param name="value">
/// The value representing a hyperbolic tangent, which must be greater than or equal to 1, but less than or equal to
/// <see cref="double.PositiveInfinity" />.
/// </param>
/// <returns>
/// The hyperbolic arctangent of <paramref name="value" />, θ, measured in radians; such that -∞ &lt; θ &lt; -1, or 1 &lt;
/// θ &lt; ∞. If <paramref name="value" /> is equal to <see cref="double.NaN" />, less than -1, or greater than 1,
/// <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Atanh(this double value)
{
return System.Math.Atanh(value);
}
/// <summary>
/// Returns the cosine of the specified angle.
/// </summary>
/// <param name="value">The angle, measured in radians.</param>
/// <returns>
/// The cosine of <paramref name="value" />. If <paramref name="value" /> is equal to <see cref="double.NaN" />,
/// <see cref="double.NegativeInfinity" />, or <see cref="double.PositiveInfinity" />, this method returns
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Cos(this double value)
{
return System.Math.Cos(value);
}
/// <summary>
/// Returns the hyperbolic cosine of the specified angle.
/// </summary>
/// <param name="value">The angle, measured in radians.</param>
/// <returns>
/// The hyperbolic cosine of <paramref name="value" />. If <paramref name="value" /> is equal to
/// <see cref="double.NegativeInfinity" /> or <see cref="double.PositiveInfinity" />,
/// <see cref="double.PositiveInfinity" /> is returned. If <paramref name="value" /> is equal to
/// <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Cosh(this double value)
{
return System.Math.Cosh(value);
}
/// <summary>
/// Returns the sine of the specified angle.
/// </summary>
/// <param name="value">The angle, in radians.</param>
/// <returns>
/// The sine of <paramref name="value" />. If <paramref name="value" /> is equal to <see cref="double.NaN" />,
/// <see cref="double.NegativeInfinity" />, or <see cref="double.PositiveInfinity" />, this method returns
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Sin(this double value)
{
return System.Math.Sin(value);
}
/// <summary>
/// Returns the hyperbolic sine of the specified angle.
/// </summary>
/// <param name="value">The angle, in radians.</param>
/// <returns>
/// The hyperbolic sine of <paramref name="value" />. If <paramref name="value" /> is equal to <see cref="double.NaN" />,
/// <see cref="double.NegativeInfinity" />, or <see cref="double.PositiveInfinity" />, this method returns
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Sinh(this double value)
{
return System.Math.Sinh(value);
}
/// <summary>
/// Returns an integer that indicates the sign of this double-precision floating-point number.
/// </summary>
/// <param name="value">A signed number.</param>
/// <returns>
/// A number that indicates the sign of <paramref name="value" />, as shown in the following table.
///
/// <list type="table">
/// <listheader>
/// <term>Return value</term>
/// <description>Meaning</description>
/// </listheader>
///
/// <item>
/// <term>-1</term>
/// <description><paramref name="value" /> is less than zero.</description>
/// </item>
/// <item>
/// <term>0</term>
/// <description><paramref name="value" /> is equal to zero.</description>
/// </item>
/// <item>
/// <term>1</term>
/// <description><paramref name="value" /> is greater than zero.</description>
/// </item>
/// </list>
/// </returns>
/// <exception cref="ArithmeticException"><paramref name="value" /> is equal to <see cref="double.NaN" />.</exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int Sign(this double value)
{
return System.Math.Sign(value);
}
/// <summary>
/// Returns the square root of this double-precision floating-point number.
/// </summary>
/// <param name="value">The number whose square root is to be found.</param>
/// <returns>
/// One of the values in the following table.
///
/// <list type="table">
/// <listheader>
/// <term>Return value</term>
/// <description>Meaning</description>
/// </listheader>
///
/// <item>
/// <term>The positive square root of <paramref name="value" />.</term>
/// <description><paramref name="value" /> is greater than or equal to 0.</description>
/// </item>
/// <item>
/// <term><see cref="double.NaN" /></term>
/// <description><paramref name="value" /> is equal to <see cref="double.NaN" /> or is negative.</description>
/// </item>
/// <item>
/// <term><see cref="double.PositiveInfinity" /></term>
/// <description><paramref name="value" /> is equal to <see cref="double.PositiveInfinity" />.</description>
/// </item>
/// </list>
/// </returns>
/// <remarks>
/// For negative input, this method returns <see cref="double.NaN" />. To receive a complex number, see
/// <see cref="Numerics.DoubleExtensions.ComplexSqrt" />.
/// </remarks>
/// <seealso cref="Numerics.DoubleExtensions.ComplexSqrt" />
/// <author>SLenik https://stackoverflow.com/a/6755197/1467293</author>
/// <license>CC BY-SA 3.0</license>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Sqrt(this double value)
{
switch (value)
{
case 0:
return 0;
case < 0 or double.NaN:
return double.NaN;
case double.PositiveInfinity:
return double.PositiveInfinity;
}
double previous;
double current = System.Math.Sqrt(value);
do
{
previous = current;
if (previous == 0.0)
{
return 0;
}
current = (previous + value / previous) / 2;
} while (System.Math.Abs(previous - current) > double.Epsilon);
return current;
}
/// <summary>
/// Returns the tangent of the specified angle.
/// </summary>
/// <param name="value">The angle, measured in radians.</param>
/// <returns>
/// The tangent of <paramref name="value" />. If <paramref name="value" /> is equal to <see cref="double.NaN" />,
/// <see cref="double.NegativeInfinity" />, or <see cref="double.PositiveInfinity" />, this method returns
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Tan(this double value)
{
return System.Math.Tan(value);
}
/// <summary>
/// Returns the hyperbolic tangent of the specified angle.
/// </summary>
/// <param name="value">The angle, measured in radians.</param>
/// <returns>
/// The hyperbolic tangent of <paramref name="value" />. If <paramref name="value" /> is equal to
/// <see cref="double.NegativeInfinity" />, this method returns -1. If <paramref name="value" /> is equal to
/// <see cref="double.PositiveInfinity" />, this method returns 1. If <paramref name="value" /> is equal to
/// <see cref="double.NaN" />, this method returns <see cref="double.NaN" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Tanh(this double value)
{
return System.Math.Tanh(value);
}
}

View File

@ -0,0 +1,35 @@
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
using X10D.Math;
namespace X10D.Numerics;
/// <summary>
/// Extension methods which accept or return types within <see cref="System.Numerics" />.
/// </summary>
public static class DoubleExtensions
{
/// <summary>
/// Returns the complex square root of this double-precision floating-point number.
/// </summary>
/// <param name="value">The number whose square root is to be found.</param>
/// <returns>The square root of <paramref name="value" />.</returns>
/// <seealso cref="X10D.Math.DoubleExtensions.Sqrt" />
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static Complex ComplexSqrt(this double value)
{
switch (value)
{
case double.PositiveInfinity:
case double.NegativeInfinity:
return Complex.Infinity;
case double.NaN:
return Complex.NaN;
}
double absoluteSqrt = System.Math.Abs(value).Sqrt();
return new Complex(absoluteSqrt, value >= 0 ? 0 : 1);
}
}

View File

@ -80,7 +80,7 @@ public class StringBuilderReader : TextReader
return -1;
}
int length = Math.Min(_stringBuilder.Length - _index, count);
int length = System.Math.Min(_stringBuilder.Length - _index, count);
_stringBuilder.CopyTo(_index, buffer, index, length);
_index += length;
return length;
@ -89,7 +89,7 @@ public class StringBuilderReader : TextReader
/// <inheritdoc />
public override int Read(Span<char> buffer)
{
int count = Math.Min(buffer.Length, _stringBuilder.Length - _index);
int count = System.Math.Min(buffer.Length, _stringBuilder.Length - _index);
for (var index = 0; index < count; index++)
{
buffer[index] = _stringBuilder[index + _index];
@ -138,7 +138,7 @@ public class StringBuilderReader : TextReader
return -1;
}
int length = Math.Min(count, _stringBuilder.Length - _index);
int length = System.Math.Min(count, _stringBuilder.Length - _index);
_stringBuilder.CopyTo(_index, buffer, index, length);
_index += length;
return length;

View File

@ -454,14 +454,9 @@ public static class StringExtensions
throw new ArgumentNullException(nameof(value));
}
return SplitInternal();
IEnumerable<string> SplitInternal()
for (var i = 0; i < value.Length; i += chunkSize)
{
for (var i = 0; i < value.Length; i += chunkSize)
{
yield return value.Substring(i, Math.Min(chunkSize, value.Length - i));
}
yield return value[i..System.Math.Min(i + chunkSize, value.Length - 1)];
}
}