From b4f09e0a94b403f9c5154a69be490c365f700d15 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 24 Apr 2022 11:04:30 +0100 Subject: [PATCH] Move math-related extensions to Math namespace (#7) --- X10D.Tests/src/Core/ByteTests.cs | 42 +----- X10D.Tests/src/Core/SByteTests.cs | 43 ------ X10D.Tests/src/Math/ByteTests.cs | 26 ++++ X10D.Tests/src/{Core => Math}/IsPrimeTests.cs | 30 +---- X10D.Tests/src/Math/SByteTests.cs | 26 ++++ X10D/src/ByteExtensions/ByteExtensions.cs | 38 ------ X10D/src/ByteExtensions/SByteExtensions.cs | 83 ------------ X10D/src/DoubleExtensions/DoubleExtensions.cs | 113 +--------------- X10D/src/Math/ByteExtensions.cs | 40 +++++- X10D/src/Math/DoubleExtensions.cs | 127 ++++++++++++++++++ X10D/src/Math/SByteExtensions.cs | 87 +++++++++++- X10D/src/Math/SingleExtensions.cs | 127 ++++++++++++++++++ X10D/src/Numerics/RandomExtensions.cs | 5 +- X10D/src/SingleExtensions/SingleExtensions.cs | 113 +--------------- 14 files changed, 442 insertions(+), 458 deletions(-) delete mode 100644 X10D.Tests/src/Core/SByteTests.cs rename X10D.Tests/src/{Core => Math}/IsPrimeTests.cs (71%) delete mode 100644 X10D/src/ByteExtensions/SByteExtensions.cs diff --git a/X10D.Tests/src/Core/ByteTests.cs b/X10D.Tests/src/Core/ByteTests.cs index 92b4cd3..f782838 100644 --- a/X10D.Tests/src/Core/ByteTests.cs +++ b/X10D.Tests/src/Core/ByteTests.cs @@ -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); } - - /// - /// Tests . - /// - [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); - } - - /// - /// Tests . - /// - [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); - } -} \ No newline at end of file +} diff --git a/X10D.Tests/src/Core/SByteTests.cs b/X10D.Tests/src/Core/SByteTests.cs deleted file mode 100644 index 23e681c..0000000 --- a/X10D.Tests/src/Core/SByteTests.cs +++ /dev/null @@ -1,43 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace X10D.Tests.Core; - -/// -/// Tests for -/// -[CLSCompliant(false)] -[TestClass] -public class SByteTests -{ - /// - /// Tests . - /// - [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); - } - - /// - /// Tests . - /// - [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); - } -} \ No newline at end of file diff --git a/X10D.Tests/src/Math/ByteTests.cs b/X10D.Tests/src/Math/ByteTests.cs index 41bf551..e785fec 100644 --- a/X10D.Tests/src/Math/ByteTests.cs +++ b/X10D.Tests/src/Math/ByteTests.cs @@ -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); + } } diff --git a/X10D.Tests/src/Core/IsPrimeTests.cs b/X10D.Tests/src/Math/IsPrimeTests.cs similarity index 71% rename from X10D.Tests/src/Core/IsPrimeTests.cs rename to X10D.Tests/src/Math/IsPrimeTests.cs index bb55450..04ff694 100644 --- a/X10D.Tests/src/Core/IsPrimeTests.cs +++ b/X10D.Tests/src/Math/IsPrimeTests.cs @@ -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; -/// -/// Tests for . -/// -/// -/// Tests for this extension method are delegated to their own test class because of the non-trivial requirements for -/// loading testing prime numbers. -/// -/// -/// -/// [TestClass] public class IsPrimeTests { @@ -27,9 +18,6 @@ public class IsPrimeTests Assert.AreEqual(1000, _primeNumbers.Count); } - /// - /// Asserts the primality of the first 1000 known prime numbers. - /// [TestMethod] public void First1000Primes() { @@ -41,9 +29,6 @@ public class IsPrimeTests } } - /// - /// Asserts that all negative numbers are not prime. - /// [TestMethod] public void Negatives() { @@ -54,9 +39,6 @@ public class IsPrimeTests } } - /// - /// Asserts that values 0 and 1 are not prime. - /// [TestMethod] public void LessThan2() { @@ -67,9 +49,6 @@ public class IsPrimeTests } } - /// - /// Tests the primality of the numbers 0 - 7919. - /// [TestMethod] public void ZeroTo7919() { @@ -82,9 +61,6 @@ public class IsPrimeTests } } - /// - /// Tests the primality of the numbers 0 - . - /// [TestMethod] public void ZeroToByteMaxValue() { @@ -115,4 +91,4 @@ public class IsPrimeTests return primes.AsReadOnly(); } -} \ No newline at end of file +} diff --git a/X10D.Tests/src/Math/SByteTests.cs b/X10D.Tests/src/Math/SByteTests.cs index c5279bb..1bb9da0 100644 --- a/X10D.Tests/src/Math/SByteTests.cs +++ b/X10D.Tests/src/Math/SByteTests.cs @@ -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() { diff --git a/X10D/src/ByteExtensions/ByteExtensions.cs b/X10D/src/ByteExtensions/ByteExtensions.cs index 6d3700f..7b0e5eb 100644 --- a/X10D/src/ByteExtensions/ByteExtensions.cs +++ b/X10D/src/ByteExtensions/ByteExtensions.cs @@ -14,42 +14,4 @@ public static class ByteExtensions { return new[] { value }; } - - /// - /// Returns a value indicating whether the current value is evenly divisible by 2. - /// - /// The value whose parity to check. - /// - /// if is evenly divisible by 2, or - /// otherwise. - /// - public static bool IsEven(this byte value) - { - return value % 2 == 0; - } - - /// - /// Returns a value indicating whether the current value is not evenly divisible by 2. - /// - /// The value whose parity to check. - /// - /// if is not evenly divisible by 2, or - /// otherwise. - /// - public static bool IsOdd(this byte value) - { - return !value.IsEven(); - } - - /// - /// Returns a value indicating whether the current value is a prime number. - /// - /// The value whose primality to check. - /// - /// if is prime; otherwise, . - /// - public static bool IsPrime(this byte value) - { - return ((short)value).IsPrime(); - } } diff --git a/X10D/src/ByteExtensions/SByteExtensions.cs b/X10D/src/ByteExtensions/SByteExtensions.cs deleted file mode 100644 index 8930448..0000000 --- a/X10D/src/ByteExtensions/SByteExtensions.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System.Diagnostics.Contracts; -using System.Runtime.CompilerServices; - -namespace X10D; - -/// -/// Extension methods for . -/// -[CLSCompliant(false)] -public static class SByteExtensions -{ - /// - /// Returns a value indicating whether the current value is evenly divisible by 2. - /// - /// The value whose parity to check. - /// - /// if is evenly divisible by 2, or - /// otherwise. - /// - public static bool IsEven(this sbyte value) - { - return value % 2 == 0; - } - - /// - /// Returns a value indicating whether the current value is not evenly divisible by 2. - /// - /// The value whose parity to check. - /// - /// if is not evenly divisible by 2, or - /// otherwise. - /// - public static bool IsOdd(this sbyte value) - { - return !value.IsEven(); - } - - /// - /// Returns a value indicating whether the current value is a prime number. - /// - /// The value whose primality to check. - /// - /// if is prime; otherwise, . - /// - public static bool IsPrime(this sbyte value) - { - return ((long)value).IsPrime(); - } - - /// - /// Returns an integer that indicates the sign of this 8-bit signed integer. - /// - /// 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 sbyte value) - { - return System.Math.Sign(value); - } -} diff --git a/X10D/src/DoubleExtensions/DoubleExtensions.cs b/X10D/src/DoubleExtensions/DoubleExtensions.cs index 9c75b42..e2c81f2 100644 --- a/X10D/src/DoubleExtensions/DoubleExtensions.cs +++ b/X10D/src/DoubleExtensions/DoubleExtensions.cs @@ -1,22 +1,10 @@ -using X10D.Math; - -namespace X10D; +namespace X10D; /// /// Extension methods for . /// public static class DoubleExtensions { - /// - /// Converts the current angle in degrees to its equivalent represented in radians. - /// - /// The angle in degrees to convert. - /// The result of π * / 180. - public static double DegreesToRadians(this double value) - { - return value * (System.Math.PI / 180.0); - } - /// /// Converts the to a []. /// @@ -26,103 +14,4 @@ public static class DoubleExtensions { return BitConverter.GetBytes(number); } - - /// - /// Returns a value indicating whether the current value is evenly divisible by 2. - /// - /// The value whose parity to check. - /// - /// if is evenly divisible by 2, or - /// otherwise. - /// - public static bool IsEven(this double value) - { - return System.Math.Abs(value % 2.0) < double.Epsilon; - } - - /// - /// Returns a value indicating whether the current value is not evenly divisible by 2. - /// - /// The value whose parity to check. - /// - /// if is not evenly divisible by 2, or - /// otherwise. - /// - public static bool IsOdd(this double value) - { - return !value.IsEven(); - } - - /// - /// Linearly interpolates to the current value from a specified source using a specified alpha. - /// - /// The interpolation target. - /// The interpolation source. - /// The interpolation alpha. - /// - /// The interpolation result as determined by (1 - alpha) * value + alpha * target. - /// - public static double LerpFrom(this double target, double value, double alpha) - { - return MathUtility.Lerp(value, target, alpha); - } - - /// - /// Linearly interpolates from the current value to a specified target using a specified alpha. - /// - /// The interpolation source. - /// The interpolation target. - /// The interpolation alpha. - /// - /// The interpolation result as determined by (1 - alpha) * value + alpha * target. - /// - public static double LerpTo(this double value, double target, double alpha) - { - return MathUtility.Lerp(value, target, alpha); - } - - /// - /// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value. - /// - /// The interpolation alpha. - /// The interpolation source. - /// The interpolation target. - /// - /// The interpolation result as determined by (1 - alpha) * value + alpha * target. - /// - public static double LerpWith(this double alpha, double value, double target) - { - return MathUtility.Lerp(value, target, alpha); - } - - /// - /// Converts the current angle in radians to its equivalent represented in degrees. - /// - /// The angle in radians to convert. - /// The result of π * / 180. - public static double RadiansToDegrees(this double value) - { - return value * (180.0 / System.Math.PI); - } - - /// - /// Rounds the current value to the nearest whole number. - /// - /// The value to round. - /// rounded to the nearest whole number. - public static double Round(this double value) - { - return value.Round(1.0); - } - - /// - /// Rounds the current value to the nearest multiple of a specified number. - /// - /// The value to round. - /// The nearest multiple to which should be rounded. - /// rounded to the nearest multiple of . - public static double Round(this double value, double nearest) - { - return System.Math.Round(value / nearest) * nearest; - } } diff --git a/X10D/src/Math/ByteExtensions.cs b/X10D/src/Math/ByteExtensions.cs index fdbd177..2cadae1 100644 --- a/X10D/src/Math/ByteExtensions.cs +++ b/X10D/src/Math/ByteExtensions.cs @@ -1,7 +1,7 @@ namespace X10D.Math; /// -/// Extension methods for . +/// Math-related extension methods for . /// public static class ByteExtensions { @@ -25,4 +25,42 @@ public static class ByteExtensions return result; } + + /// + /// Returns a value indicating whether the current value is evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is evenly divisible by 2, or + /// otherwise. + /// + public static bool IsEven(this byte value) + { + return value % 2 == 0; + } + + /// + /// Returns a value indicating whether the current value is not evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is not evenly divisible by 2, or + /// otherwise. + /// + public static bool IsOdd(this byte value) + { + return !value.IsEven(); + } + + /// + /// Returns a value indicating whether the current value is a prime number. + /// + /// The value whose primality to check. + /// + /// if is prime; otherwise, . + /// + public static bool IsPrime(this byte value) + { + return ((long)value).IsPrime(); + } } diff --git a/X10D/src/Math/DoubleExtensions.cs b/X10D/src/Math/DoubleExtensions.cs index 45322f1..23bc74d 100644 --- a/X10D/src/Math/DoubleExtensions.cs +++ b/X10D/src/Math/DoubleExtensions.cs @@ -148,6 +148,133 @@ public static class DoubleExtensions return System.Math.Cosh(value); } + /// + /// Converts the current angle in degrees to its equivalent represented in radians. + /// + /// The angle in degrees to convert. + /// The result of π * / 180. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static double DegreesToRadians(this double value) + { + return value * (System.Math.PI / 180.0); + } + + /// + /// Returns a value indicating whether the current value is evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsEven(this double value) + { + return System.Math.Abs(value % 2.0) < double.Epsilon; + } + + /// + /// Returns a value indicating whether the current value is not evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is not evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsOdd(this double value) + { + return !value.IsEven(); + } + + /// + /// Linearly interpolates to the current value from a specified source using a specified alpha. + /// + /// The interpolation target. + /// The interpolation source. + /// The interpolation alpha. + /// + /// The interpolation result as determined by (1 - alpha) * value + alpha * target. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static double LerpFrom(this double target, double value, double alpha) + { + return MathUtility.Lerp(value, target, alpha); + } + + /// + /// Linearly interpolates from the current value to a specified target using a specified alpha. + /// + /// The interpolation source. + /// The interpolation target. + /// The interpolation alpha. + /// + /// The interpolation result as determined by (1 - alpha) * value + alpha * target. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static double LerpTo(this double value, double target, double alpha) + { + return MathUtility.Lerp(value, target, alpha); + } + + /// + /// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value. + /// + /// The interpolation alpha. + /// The interpolation source. + /// The interpolation target. + /// + /// The interpolation result as determined by (1 - alpha) * value + alpha * target. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static double LerpWith(this double alpha, double value, double target) + { + return MathUtility.Lerp(value, target, alpha); + } + + /// + /// Converts the current angle in radians to its equivalent represented in degrees. + /// + /// The angle in radians to convert. + /// The result of π * / 180. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static double RadiansToDegrees(this double value) + { + return value * (180.0 / System.Math.PI); + } + + /// + /// Rounds the current value to the nearest whole number. + /// + /// The value to round. + /// rounded to the nearest whole number. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static double Round(this double value) + { + return value.Round(1.0); + } + + /// + /// Rounds the current value to the nearest multiple of a specified number. + /// + /// The value to round. + /// The nearest multiple to which should be rounded. + /// rounded to the nearest multiple of . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static double Round(this double value, double nearest) + { + return System.Math.Round(value / nearest) * nearest; + } + /// /// Returns the sine of the specified angle. /// diff --git a/X10D/src/Math/SByteExtensions.cs b/X10D/src/Math/SByteExtensions.cs index d9bc296..4251b0b 100644 --- a/X10D/src/Math/SByteExtensions.cs +++ b/X10D/src/Math/SByteExtensions.cs @@ -1,7 +1,10 @@ -namespace X10D.Math; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; + +namespace X10D.Math; /// -/// Extension methods for . +/// Math-related extension methods for . /// [CLSCompliant(false)] public static class SByteExtensions @@ -12,6 +15,8 @@ public static class SByteExtensions /// The value whose factorial to compute. /// The factorial of . /// is less than 0. + [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; } + + /// + /// Returns a value indicating whether the current value is evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsEven(this sbyte value) + { + return value % 2 == 0; + } + + /// + /// Returns a value indicating whether the current value is not evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is not evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsOdd(this sbyte value) + { + return !value.IsEven(); + } + + /// + /// Returns a value indicating whether the current value is a prime number. + /// + /// The value whose primality to check. + /// + /// if is prime; otherwise, . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsPrime(this sbyte value) + { + return ((long)value).IsPrime(); + } + + /// + /// Returns an integer that indicates the sign of this 8-bit signed integer. + /// + /// 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 sbyte value) + { + return System.Math.Sign(value); + } } diff --git a/X10D/src/Math/SingleExtensions.cs b/X10D/src/Math/SingleExtensions.cs index cfc318a..7c7e22e 100644 --- a/X10D/src/Math/SingleExtensions.cs +++ b/X10D/src/Math/SingleExtensions.cs @@ -148,6 +148,133 @@ public static class SingleExtensions return MathF.Cosh(value); } + /// + /// Converts the current angle in degrees to its equivalent represented in radians. + /// + /// The angle in degrees to convert. + /// The result of π * / 180. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float DegreesToRadians(this float value) + { + return value * (MathF.PI / 180.0f); + } + + /// + /// Returns a value indicating whether the current value is evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsEven(this float value) + { + return value % 2 == 0; + } + + /// + /// Returns a value indicating whether the current value is not evenly divisible by 2. + /// + /// The value whose parity to check. + /// + /// if is not evenly divisible by 2, or + /// otherwise. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static bool IsOdd(this float value) + { + return !value.IsEven(); + } + + /// + /// Linearly interpolates to the current value from a specified source using a specified alpha. + /// + /// The interpolation target. + /// The interpolation source. + /// The interpolation alpha. + /// + /// The interpolation result as determined by (1 - alpha) * value + alpha * target. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float LerpFrom(this float target, float value, float alpha) + { + return MathUtility.Lerp(value, target, alpha); + } + + /// + /// Linearly interpolates from the current value to a specified target using a specified alpha. + /// + /// The interpolation source. + /// The interpolation target. + /// The interpolation alpha. + /// + /// The interpolation result as determined by (1 - alpha) * value + alpha * target. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float LerpTo(this float value, float target, float alpha) + { + return MathUtility.Lerp(value, target, alpha); + } + + /// + /// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value. + /// + /// The interpolation alpha. + /// The interpolation source. + /// The interpolation target. + /// + /// The interpolation result as determined by (1 - alpha) * value + alpha * target. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float LerpWith(this float alpha, float value, float target) + { + return MathUtility.Lerp(value, target, alpha); + } + + /// + /// Converts the current angle in radians to its equivalent represented in degrees. + /// + /// The angle in radians to convert. + /// The result of π * / 180. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float RadiansToDegrees(this float value) + { + return value * (180.0f / MathF.PI); + } + + /// + /// Rounds the current value to the nearest whole number. + /// + /// The value to round. + /// rounded to the nearest whole number. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Round(this float value) + { + return value.Round(1.0f); + } + + /// + /// Rounds the current value to the nearest multiple of a specified number. + /// + /// The value to round. + /// The nearest multiple to which should be rounded. + /// rounded to the nearest multiple of . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static float Round(this float value, float nearest) + { + return MathF.Round(value / nearest) * nearest; + } + /// /// Returns an integer that indicates the sign of this single-precision floating-point number. /// diff --git a/X10D/src/Numerics/RandomExtensions.cs b/X10D/src/Numerics/RandomExtensions.cs index ddc46a2..450d81c 100644 --- a/X10D/src/Numerics/RandomExtensions.cs +++ b/X10D/src/Numerics/RandomExtensions.cs @@ -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); diff --git a/X10D/src/SingleExtensions/SingleExtensions.cs b/X10D/src/SingleExtensions/SingleExtensions.cs index b998c33..9a258d1 100644 --- a/X10D/src/SingleExtensions/SingleExtensions.cs +++ b/X10D/src/SingleExtensions/SingleExtensions.cs @@ -1,22 +1,10 @@ -using X10D.Math; - -namespace X10D; +namespace X10D; /// /// Extension methods for . /// public static class SingleExtensions { - /// - /// Converts the current angle in degrees to its equivalent represented in radians. - /// - /// The angle in degrees to convert. - /// The result of π * / 180. - public static float DegreesToRadians(this float value) - { - return value * (MathF.PI / 180.0f); - } - /// /// Converts the to a []. /// @@ -26,103 +14,4 @@ public static class SingleExtensions { return BitConverter.GetBytes(number); } - - /// - /// Returns a value indicating whether the current value is evenly divisible by 2. - /// - /// The value whose parity to check. - /// - /// if is evenly divisible by 2, or - /// otherwise. - /// - public static bool IsEven(this float value) - { - return value % 2 == 0; - } - - /// - /// Returns a value indicating whether the current value is not evenly divisible by 2. - /// - /// The value whose parity to check. - /// - /// if is not evenly divisible by 2, or - /// otherwise. - /// - public static bool IsOdd(this float value) - { - return !value.IsEven(); - } - - /// - /// Linearly interpolates to the current value from a specified source using a specified alpha. - /// - /// The interpolation target. - /// The interpolation source. - /// The interpolation alpha. - /// - /// The interpolation result as determined by (1 - alpha) * value + alpha * target. - /// - public static float LerpFrom(this float target, float value, float alpha) - { - return MathUtility.Lerp(value, target, alpha); - } - - /// - /// Linearly interpolates from the current value to a specified target using a specified alpha. - /// - /// The interpolation source. - /// The interpolation target. - /// The interpolation alpha. - /// - /// The interpolation result as determined by (1 - alpha) * value + alpha * target. - /// - public static float LerpTo(this float value, float target, float alpha) - { - return MathUtility.Lerp(value, target, alpha); - } - - /// - /// Linearly interpolates to a specified target from a specified source, using the current value as the alpha value. - /// - /// The interpolation alpha. - /// The interpolation source. - /// The interpolation target. - /// - /// The interpolation result as determined by (1 - alpha) * value + alpha * target. - /// - public static float LerpWith(this float alpha, float value, float target) - { - return MathUtility.Lerp(value, target, alpha); - } - - /// - /// Converts the current angle in radians to its equivalent represented in degrees. - /// - /// The angle in radians to convert. - /// The result of π * / 180. - public static float RadiansToDegrees(this float value) - { - return value * (180.0f / MathF.PI); - } - - /// - /// Rounds the current value to the nearest whole number. - /// - /// The value to round. - /// rounded to the nearest whole number. - public static float Round(this float value) - { - return value.Round(1.0f); - } - - /// - /// Rounds the current value to the nearest multiple of a specified number. - /// - /// The value to round. - /// The nearest multiple to which should be rounded. - /// rounded to the nearest multiple of . - public static float Round(this float value, float nearest) - { - return MathF.Round(value / nearest) * nearest; - } }