mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-26 12:58:49 +00:00
Move math-related extensions to Math namespace (#7)
This commit is contained in:
parent
2c2de1e30e
commit
b4f09e0a94
@ -17,45 +17,13 @@ public class ByteTests
|
||||
const byte byteMinValue = byte.MinValue;
|
||||
const byte byteMaxValue = byte.MaxValue;
|
||||
|
||||
var minValueBytes = new[] { byteMinValue };
|
||||
var maxValueBytes = new[] { byteMaxValue };
|
||||
byte[] minValueBytes = {byteMinValue};
|
||||
byte[] maxValueBytes = {byteMaxValue};
|
||||
|
||||
var minValueResult = byteMinValue.GetBytes();
|
||||
var maxValueResult = byteMaxValue.GetBytes();
|
||||
byte[] minValueResult = byteMinValue.GetBytes();
|
||||
byte[] maxValueResult = byteMaxValue.GetBytes();
|
||||
|
||||
CollectionAssert.AreEqual(minValueBytes, minValueResult);
|
||||
CollectionAssert.AreEqual(maxValueBytes, maxValueResult);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests <see cref="ByteExtensions.IsEven" />.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void IsEven()
|
||||
{
|
||||
const byte one = 1;
|
||||
const byte two = 2;
|
||||
|
||||
var oneEven = one.IsEven();
|
||||
var twoEven = two.IsEven();
|
||||
|
||||
Assert.IsFalse(oneEven);
|
||||
Assert.IsTrue(twoEven);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests <see cref="ByteExtensions.IsOdd" />.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void IsOdd()
|
||||
{
|
||||
const byte one = 1;
|
||||
const byte two = 2;
|
||||
|
||||
var oneOdd = one.IsOdd();
|
||||
var twoOdd = two.IsOdd();
|
||||
|
||||
Assert.IsTrue(oneOdd);
|
||||
Assert.IsFalse(twoOdd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,43 +0,0 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace X10D.Tests.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for <see cref="SByteExtensions" />
|
||||
/// </summary>
|
||||
[CLSCompliant(false)]
|
||||
[TestClass]
|
||||
public class SByteTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests <see cref="SByteExtensions.IsEven"/>.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void IsEven()
|
||||
{
|
||||
const sbyte one = 1;
|
||||
const sbyte two = 2;
|
||||
|
||||
var oneEven = one.IsEven();
|
||||
var twoEven = two.IsEven();
|
||||
|
||||
Assert.AreEqual(false, oneEven);
|
||||
Assert.AreEqual(true, twoEven);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests <see cref="SByteExtensions.IsOdd"/>.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void IsOdd()
|
||||
{
|
||||
const sbyte one = 1;
|
||||
const sbyte two = 2;
|
||||
|
||||
var oneOdd = one.IsOdd();
|
||||
var twoOdd = two.IsOdd();
|
||||
|
||||
Assert.AreEqual(true, oneOdd);
|
||||
Assert.AreEqual(false, twoOdd);
|
||||
}
|
||||
}
|
@ -21,4 +21,30 @@ public class ByteTests
|
||||
Assert.AreEqual(362880L, ((byte)9).Factorial());
|
||||
Assert.AreEqual(3628800L, ((byte)10).Factorial());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEvenShouldBeCorrect()
|
||||
{
|
||||
const byte one = 1;
|
||||
const byte two = 2;
|
||||
|
||||
bool oneEven = one.IsEven();
|
||||
bool twoEven = two.IsEven();
|
||||
|
||||
Assert.IsFalse(oneEven);
|
||||
Assert.IsTrue(twoEven);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsOddShouldBeCorrect()
|
||||
{
|
||||
const byte one = 1;
|
||||
const byte two = 2;
|
||||
|
||||
bool oneOdd = one.IsOdd();
|
||||
bool twoOdd = two.IsOdd();
|
||||
|
||||
Assert.IsTrue(oneOdd);
|
||||
Assert.IsFalse(twoOdd);
|
||||
}
|
||||
}
|
||||
|
@ -2,19 +2,10 @@
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using X10D.Math;
|
||||
|
||||
namespace X10D.Tests.Core;
|
||||
namespace X10D.Tests.Math;
|
||||
|
||||
/// <summary>
|
||||
/// Tests for <see cref="Int32Extensions.IsPrime(int)"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Tests for this extension method are delegated to their own test class because of the non-trivial requirements for
|
||||
/// loading testing prime numbers.
|
||||
/// </remarks>
|
||||
/// <seealso cref="Int16Extensions.IsPrime(short)" />
|
||||
/// <seealso cref="Int32Extensions.IsPrime(int)" />
|
||||
/// <seealso cref="Int64Extensions.IsPrime(long)" />
|
||||
[TestClass]
|
||||
public class IsPrimeTests
|
||||
{
|
||||
@ -27,9 +18,6 @@ public class IsPrimeTests
|
||||
Assert.AreEqual(1000, _primeNumbers.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the primality of the first 1000 known prime numbers.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void First1000Primes()
|
||||
{
|
||||
@ -41,9 +29,6 @@ public class IsPrimeTests
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that all negative numbers are not prime.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void Negatives()
|
||||
{
|
||||
@ -54,9 +39,6 @@ public class IsPrimeTests
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that values 0 and 1 are not prime.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void LessThan2()
|
||||
{
|
||||
@ -67,9 +49,6 @@ public class IsPrimeTests
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the primality of the numbers 0 - 7919.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void ZeroTo7919()
|
||||
{
|
||||
@ -82,9 +61,6 @@ public class IsPrimeTests
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests the primality of the numbers 0 - <see cref="byte.MaxValue" />.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void ZeroToByteMaxValue()
|
||||
{
|
||||
@ -115,4 +91,4 @@ public class IsPrimeTests
|
||||
|
||||
return primes.AsReadOnly();
|
||||
}
|
||||
}
|
||||
}
|
@ -23,6 +23,32 @@ public class SByteTests
|
||||
Assert.AreEqual(3628800L, ((sbyte)10).Factorial());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsEvenShouldBeCorrect()
|
||||
{
|
||||
const sbyte one = 1;
|
||||
const sbyte two = 2;
|
||||
|
||||
bool oneEven = one.IsEven();
|
||||
bool twoEven = two.IsEven();
|
||||
|
||||
Assert.AreEqual(false, oneEven);
|
||||
Assert.AreEqual(true, twoEven);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void IsOddShouldBeCorrect()
|
||||
{
|
||||
const sbyte one = 1;
|
||||
const sbyte two = 2;
|
||||
|
||||
bool oneOdd = one.IsOdd();
|
||||
bool twoOdd = two.IsOdd();
|
||||
|
||||
Assert.AreEqual(true, oneOdd);
|
||||
Assert.AreEqual(false, twoOdd);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NegativeFactorialShouldThrow()
|
||||
{
|
||||
|
@ -14,42 +14,4 @@ public static class ByteExtensions
|
||||
{
|
||||
return new[] { value };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsEven(this byte value)
|
||||
{
|
||||
return value % 2 == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsOdd(this byte value)
|
||||
{
|
||||
return !value.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose primality to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
|
||||
/// </returns>
|
||||
public static bool IsPrime(this byte value)
|
||||
{
|
||||
return ((short)value).IsPrime();
|
||||
}
|
||||
}
|
||||
|
@ -1,83 +0,0 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace X10D;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="sbyte" />.
|
||||
/// </summary>
|
||||
[CLSCompliant(false)]
|
||||
public static class SByteExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsEven(this sbyte value)
|
||||
{
|
||||
return value % 2 == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsOdd(this sbyte value)
|
||||
{
|
||||
return !value.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose primality to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
|
||||
/// </returns>
|
||||
public static bool IsPrime(this sbyte value)
|
||||
{
|
||||
return ((long)value).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an integer that indicates the sign of this 8-bit signed integer.
|
||||
/// </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>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static int Sign(this sbyte value)
|
||||
{
|
||||
return System.Math.Sign(value);
|
||||
}
|
||||
}
|
@ -1,22 +1,10 @@
|
||||
using X10D.Math;
|
||||
|
||||
namespace X10D;
|
||||
namespace X10D;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="double" />.
|
||||
/// </summary>
|
||||
public static class DoubleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts the current angle in degrees to its equivalent represented in radians.
|
||||
/// </summary>
|
||||
/// <param name="value">The angle in degrees to convert.</param>
|
||||
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
|
||||
public static double DegreesToRadians(this double value)
|
||||
{
|
||||
return value * (System.Math.PI / 180.0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="double" /> to a <see cref="byte" />[].
|
||||
/// </summary>
|
||||
@ -26,103 +14,4 @@ public static class DoubleExtensions
|
||||
{
|
||||
return BitConverter.GetBytes(number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsEven(this double value)
|
||||
{
|
||||
return System.Math.Abs(value % 2.0) < double.Epsilon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsOdd(this double value)
|
||||
{
|
||||
return !value.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates to the current value from a specified source using a specified alpha.
|
||||
/// </summary>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
public static double LerpFrom(this double target, double value, double alpha)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates from the current value to a specified target using a specified alpha.
|
||||
/// </summary>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
public static double LerpTo(this double value, double target, double alpha)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value.
|
||||
/// </summary>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
public static double LerpWith(this double alpha, double value, double target)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the current angle in radians to its equivalent represented in degrees.
|
||||
/// </summary>
|
||||
/// <param name="value">The angle in radians to convert.</param>
|
||||
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
|
||||
public static double RadiansToDegrees(this double value)
|
||||
{
|
||||
return value * (180.0 / System.Math.PI);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds the current value to the nearest whole number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to round.</param>
|
||||
/// <returns><paramref name="value" /> rounded to the nearest whole number.</returns>
|
||||
public static double Round(this double value)
|
||||
{
|
||||
return value.Round(1.0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds the current value to the nearest multiple of a specified number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to round.</param>
|
||||
/// <param name="nearest">The nearest multiple to which <paramref name="value" /> should be rounded.</param>
|
||||
/// <returns><paramref name="value" /> rounded to the nearest multiple of <paramref name="nearest" />.</returns>
|
||||
public static double Round(this double value, double nearest)
|
||||
{
|
||||
return System.Math.Round(value / nearest) * nearest;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
namespace X10D.Math;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="byte" />.
|
||||
/// Math-related extension methods for <see cref="byte" />.
|
||||
/// </summary>
|
||||
public static class ByteExtensions
|
||||
{
|
||||
@ -25,4 +25,42 @@ public static class ByteExtensions
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsEven(this byte value)
|
||||
{
|
||||
return value % 2 == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsOdd(this byte value)
|
||||
{
|
||||
return !value.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose primality to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
|
||||
/// </returns>
|
||||
public static bool IsPrime(this byte value)
|
||||
{
|
||||
return ((long)value).IsPrime();
|
||||
}
|
||||
}
|
||||
|
@ -148,6 +148,133 @@ public static class DoubleExtensions
|
||||
return System.Math.Cosh(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the current angle in degrees to its equivalent represented in radians.
|
||||
/// </summary>
|
||||
/// <param name="value">The angle in degrees to convert.</param>
|
||||
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static double DegreesToRadians(this double value)
|
||||
{
|
||||
return value * (System.Math.PI / 180.0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static bool IsEven(this double value)
|
||||
{
|
||||
return System.Math.Abs(value % 2.0) < double.Epsilon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static bool IsOdd(this double value)
|
||||
{
|
||||
return !value.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates to the current value from a specified source using a specified alpha.
|
||||
/// </summary>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static double LerpFrom(this double target, double value, double alpha)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates from the current value to a specified target using a specified alpha.
|
||||
/// </summary>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static double LerpTo(this double value, double target, double alpha)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value.
|
||||
/// </summary>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static double LerpWith(this double alpha, double value, double target)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the current angle in radians to its equivalent represented in degrees.
|
||||
/// </summary>
|
||||
/// <param name="value">The angle in radians to convert.</param>
|
||||
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static double RadiansToDegrees(this double value)
|
||||
{
|
||||
return value * (180.0 / System.Math.PI);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds the current value to the nearest whole number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to round.</param>
|
||||
/// <returns><paramref name="value" /> rounded to the nearest whole number.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static double Round(this double value)
|
||||
{
|
||||
return value.Round(1.0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds the current value to the nearest multiple of a specified number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to round.</param>
|
||||
/// <param name="nearest">The nearest multiple to which <paramref name="value" /> should be rounded.</param>
|
||||
/// <returns><paramref name="value" /> rounded to the nearest multiple of <paramref name="nearest" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static double Round(this double value, double nearest)
|
||||
{
|
||||
return System.Math.Round(value / nearest) * nearest;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the sine of the specified angle.
|
||||
/// </summary>
|
||||
|
@ -1,7 +1,10 @@
|
||||
namespace X10D.Math;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace X10D.Math;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="sbyte" />.
|
||||
/// Math-related extension methods for <see cref="sbyte" />.
|
||||
/// </summary>
|
||||
[CLSCompliant(false)]
|
||||
public static class SByteExtensions
|
||||
@ -12,6 +15,8 @@ public static class SByteExtensions
|
||||
/// <param name="value">The value whose factorial to compute.</param>
|
||||
/// <returns>The factorial of <paramref name="value" />.</returns>
|
||||
/// <exception cref="ArithmeticException"><paramref name="value" /> is less than 0.</exception>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static long Factorial(this sbyte value)
|
||||
{
|
||||
if (value < 0)
|
||||
@ -32,4 +37,82 @@ public static class SByteExtensions
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static bool IsEven(this sbyte value)
|
||||
{
|
||||
return value % 2 == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static bool IsOdd(this sbyte value)
|
||||
{
|
||||
return !value.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose primality to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static bool IsPrime(this sbyte value)
|
||||
{
|
||||
return ((long)value).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an integer that indicates the sign of this 8-bit signed integer.
|
||||
/// </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>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static int Sign(this sbyte value)
|
||||
{
|
||||
return System.Math.Sign(value);
|
||||
}
|
||||
}
|
||||
|
@ -148,6 +148,133 @@ public static class SingleExtensions
|
||||
return MathF.Cosh(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the current angle in degrees to its equivalent represented in radians.
|
||||
/// </summary>
|
||||
/// <param name="value">The angle in degrees to convert.</param>
|
||||
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static float DegreesToRadians(this float value)
|
||||
{
|
||||
return value * (MathF.PI / 180.0f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static bool IsEven(this float value)
|
||||
{
|
||||
return value % 2 == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static bool IsOdd(this float value)
|
||||
{
|
||||
return !value.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates to the current value from a specified source using a specified alpha.
|
||||
/// </summary>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static float LerpFrom(this float target, float value, float alpha)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates from the current value to a specified target using a specified alpha.
|
||||
/// </summary>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static float LerpTo(this float value, float target, float alpha)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value.
|
||||
/// </summary>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static float LerpWith(this float alpha, float value, float target)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the current angle in radians to its equivalent represented in degrees.
|
||||
/// </summary>
|
||||
/// <param name="value">The angle in radians to convert.</param>
|
||||
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static float RadiansToDegrees(this float value)
|
||||
{
|
||||
return value * (180.0f / MathF.PI);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds the current value to the nearest whole number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to round.</param>
|
||||
/// <returns><paramref name="value" /> rounded to the nearest whole number.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static float Round(this float value)
|
||||
{
|
||||
return value.Round(1.0f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds the current value to the nearest multiple of a specified number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to round.</param>
|
||||
/// <param name="nearest">The nearest multiple to which <paramref name="value" /> should be rounded.</param>
|
||||
/// <returns><paramref name="value" /> rounded to the nearest multiple of <paramref name="nearest" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static float Round(this float value, float nearest)
|
||||
{
|
||||
return MathF.Round(value / nearest) * nearest;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an integer that indicates the sign of this single-precision floating-point number.
|
||||
/// </summary>
|
||||
|
@ -1,4 +1,4 @@
|
||||
using System.Numerics;
|
||||
using System.Numerics;
|
||||
|
||||
namespace X10D.Numerics;
|
||||
|
||||
@ -57,8 +57,7 @@ public static class RandomExtensions
|
||||
y = seededRandom.NextSingle(-1f, 1f);
|
||||
z = seededRandom.NextSingle(-1f, 1f);
|
||||
normal = (w * w) + (x * x) + (y * y) + (z * z);
|
||||
}
|
||||
while (normal > 1f || normal == 0f);
|
||||
} while (normal > 1f || normal == 0f);
|
||||
|
||||
normal = MathF.Sqrt(normal);
|
||||
return new Quaternion(x / normal, y / normal, z / normal, w / normal);
|
||||
|
@ -1,22 +1,10 @@
|
||||
using X10D.Math;
|
||||
|
||||
namespace X10D;
|
||||
namespace X10D;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="float" />.
|
||||
/// </summary>
|
||||
public static class SingleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts the current angle in degrees to its equivalent represented in radians.
|
||||
/// </summary>
|
||||
/// <param name="value">The angle in degrees to convert.</param>
|
||||
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
|
||||
public static float DegreesToRadians(this float value)
|
||||
{
|
||||
return value * (MathF.PI / 180.0f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="float" /> to a <see cref="byte" />[].
|
||||
/// </summary>
|
||||
@ -26,103 +14,4 @@ public static class SingleExtensions
|
||||
{
|
||||
return BitConverter.GetBytes(number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsEven(this float value)
|
||||
{
|
||||
return value % 2 == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose parity to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsOdd(this float value)
|
||||
{
|
||||
return !value.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates to the current value from a specified source using a specified alpha.
|
||||
/// </summary>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
public static float LerpFrom(this float target, float value, float alpha)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates from the current value to a specified target using a specified alpha.
|
||||
/// </summary>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
public static float LerpTo(this float value, float target, float alpha)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value.
|
||||
/// </summary>
|
||||
/// <param name="alpha">The interpolation alpha.</param>
|
||||
/// <param name="value">The interpolation source.</param>
|
||||
/// <param name="target">The interpolation target.</param>
|
||||
/// <returns>
|
||||
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
|
||||
/// </returns>
|
||||
public static float LerpWith(this float alpha, float value, float target)
|
||||
{
|
||||
return MathUtility.Lerp(value, target, alpha);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the current angle in radians to its equivalent represented in degrees.
|
||||
/// </summary>
|
||||
/// <param name="value">The angle in radians to convert.</param>
|
||||
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
|
||||
public static float RadiansToDegrees(this float value)
|
||||
{
|
||||
return value * (180.0f / MathF.PI);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds the current value to the nearest whole number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to round.</param>
|
||||
/// <returns><paramref name="value" /> rounded to the nearest whole number.</returns>
|
||||
public static float Round(this float value)
|
||||
{
|
||||
return value.Round(1.0f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds the current value to the nearest multiple of a specified number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to round.</param>
|
||||
/// <param name="nearest">The nearest multiple to which <paramref name="value" /> should be rounded.</param>
|
||||
/// <returns><paramref name="value" /> rounded to the nearest multiple of <paramref name="nearest" />.</returns>
|
||||
public static float Round(this float value, float nearest)
|
||||
{
|
||||
return MathF.Round(value / nearest) * nearest;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user