From 2762dab8726d09a4564f6d3d3f8c4f0d33583240 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Wed, 26 Feb 2025 09:19:12 -0500 Subject: [PATCH] Some small things. Was working on complex numbers but got bored. --- Nerd_STF/Helpers/TargetHelper.cs | 24 +++++++++++++++++++ .../Mathematics/Algebra/IVectorOperations.cs | 1 - Nerd_STF/Mathematics/MathE.cs | 12 ++++++++++ Nerd_STF/Mathematics/Numbers/Fraction.cs | 7 +++--- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Nerd_STF/Helpers/TargetHelper.cs b/Nerd_STF/Helpers/TargetHelper.cs index 45c5e1e..14fdf46 100644 --- a/Nerd_STF/Helpers/TargetHelper.cs +++ b/Nerd_STF/Helpers/TargetHelper.cs @@ -1,9 +1,32 @@ using System; +using System.Runtime.CompilerServices; namespace Nerd_STF.Helpers { internal static class TargetHelper { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsFinite(double d) + { +#if CS11_OR_GREATER + return double.IsFinite(d); +#else + long bits = BitConverter.DoubleToInt64Bits(d); + return (bits & 0x7FFFFFFFFFFFFFFF) < 0x7FF0000000000000; +#endif + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsInfinity(double d) + { +#if CS11_OR_GREATER + return double.IsInfinity(d); +#else + long bits = BitConverter.DoubleToInt64Bits(d); + return (bits & 0x7FFFFFFFFFFFFFFF) == 0x7FF0000000000000; +#endif + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void WriteLine(string content) { #if NETSTANDARD1_1 @@ -12,6 +35,7 @@ namespace Nerd_STF.Helpers #endif } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] EmptyArray() { #if NETSTANDARD1_1 diff --git a/Nerd_STF/Mathematics/Algebra/IVectorOperations.cs b/Nerd_STF/Mathematics/Algebra/IVectorOperations.cs index 33c1803..d21dc90 100644 --- a/Nerd_STF/Mathematics/Algebra/IVectorOperations.cs +++ b/Nerd_STF/Mathematics/Algebra/IVectorOperations.cs @@ -9,7 +9,6 @@ namespace Nerd_STF.Mathematics.Algebra double Magnitude { get; } static abstract TSelf ClampMagnitude(TSelf val, double minMag, double maxMag); - static abstract void ClampMagnitude(ref TSelf val, double minMag, double maxMag); static abstract double Dot(TSelf a, TSelf b); static abstract double Dot(IEnumerable vals); } diff --git a/Nerd_STF/Mathematics/MathE.cs b/Nerd_STF/Mathematics/MathE.cs index 46cc7e9..142e06b 100644 --- a/Nerd_STF/Mathematics/MathE.cs +++ b/Nerd_STF/Mathematics/MathE.cs @@ -680,26 +680,38 @@ namespace Nerd_STF.Mathematics return flip ? result : -result; } + public static double Sin(Angle angle, int terms = 8) => + Sin(angle.Radians, terms); public static IEquation Sin(IEquation inputRad, int terms = 8) => new Equation((double x) => Sin(inputRad[x], terms)); public static double Cos(double rad, int terms = 8) => Sin(rad + Constants.HalfPi, terms); + public static double Cos(Angle angle, int terms = 8) => + Cos(angle.Radians, terms); public static IEquation Cos(IEquation inputRad, int terms = 8) => new Equation((double x) => Cos(inputRad[x], terms)); public static double Tan(double rad, int terms = 8) => Sin(rad + Constants.HalfPi, terms) / Sin(rad, terms); + public static double Tan(Angle angle, int terms = 8) => + Tan(angle.Radians, terms); public static IEquation Tan(IEquation inputRad, int terms = 8) => new Equation((double x) => Tan(inputRad[x], terms)); public static double Csc(double rad, int terms = 8) => 1 / Sin(rad, terms); + public static double Csc(Angle angle, int terms = 8) => + Csc(angle.Radians, terms); public static IEquation Csc(IEquation inputRad, int terms = 8) => new Equation((double x) => Csc(inputRad[x], terms)); public static double Sec(double rad, int terms = 8) => 1 / Sin(rad + Constants.HalfPi, terms); + public static double Sec(Angle angle, int terms = 8) => + Sec(angle.Radians, terms); public static IEquation Sec(IEquation inputRad, int terms = 8) => new Equation((double x) => Sec(inputRad[x], terms)); public static double Cot(double rad, int terms = 8) => Sin(rad, terms) / Sin(rad + Constants.HalfPi, terms); + public static double Cot(Angle angle, int terms = 8) => + Cot(angle.Radians, terms); public static IEquation Cot(IEquation inputRad, int terms = 8) => new Equation((double x) => Cot(inputRad[x], terms)); diff --git a/Nerd_STF/Mathematics/Numbers/Fraction.cs b/Nerd_STF/Mathematics/Numbers/Fraction.cs index 3dacdf6..3ee425f 100644 --- a/Nerd_STF/Mathematics/Numbers/Fraction.cs +++ b/Nerd_STF/Mathematics/Numbers/Fraction.cs @@ -361,7 +361,7 @@ namespace Nerd_STF.Mathematics.Numbers } public static bool IsEvenInteger(Fraction val) => val.num % val.den == 0 && val.num / val.den % 2 == 0; - public static bool IsFinite(Fraction val) => val.den != 0 || val.num != 0; + public static bool IsFinite(Fraction val) => val.den != 0; public static bool IsInfinity(Fraction val) => val.den == 0 && val.num != 0; public static bool IsInteger(Fraction val) => val.num % val.den == 0; public static bool IsNaN(Fraction val) => val.num == 0 && val.den == 0; @@ -375,9 +375,7 @@ namespace Nerd_STF.Mathematics.Numbers public static bool IsRealNumber(Fraction val) => val.den != 0; public static bool IsZero(Fraction val) => val.num == 0 && val.den != 0; public static Fraction MaxMagnitude(Fraction a, Fraction b) => a > b ? a : b; - public static Fraction MaxMagnitudeNumber(Fraction a, Fraction b) => a > b ? a : b; public static Fraction MinMagnitude(Fraction a, Fraction b) => a < b ? a : b; - public static Fraction MinMagnitudeNumber(Fraction a, Fraction b) => a < b ? a : b; #if CS11_OR_GREATER static bool INumberBase.IsComplexNumber(Fraction val) => false; static bool INumberBase.IsImaginaryNumber(Fraction val) => false; @@ -394,6 +392,9 @@ namespace Nerd_STF.Mathematics.Numbers static Fraction IMultiplicativeIdentity.MultiplicativeIdentity => One; static int INumberBase.Radix => 2; // Not super sure what to put here. + static Fraction INumberBase.MaxMagnitudeNumber(Fraction a, Fraction b) => a > b ? a : b; + static Fraction INumberBase.MinMagnitudeNumber(Fraction a, Fraction b) => a < b ? a : b; + private static bool TryConvertTo(Fraction frac, out T value) { object? tempValue;