internal static ComplexNumber FindGoodRoot(ComplexNumber @base, IntegerNumber power) { var list = new List <ComplexNumber>(); foreach (var root in GetAllRoots(@base, (long)power.Value).FiniteSet()) { MathS.Settings.FloatToRationalIterCount.Set(15); MathS.Settings.PrecisionErrorZeroRange.Set(1e-6m); var downcasted = Functional.Downcast((root as NumberEntity).Value) as ComplexNumber; MathS.Settings.PrecisionErrorZeroRange.Unset(); MathS.Settings.FloatToRationalIterCount.Unset(); if (downcasted.IsRational() && Number.IsZero(Number.Pow(downcasted, power) - @base)) // To keep user's desired precision { return(downcasted); } list.Add(downcasted); } foreach (var el in list) { if (el.IsReal() && (el as RealNumber) > 0) { return(el); } } foreach (var el in list) { if (el.IsReal()) { return(el); } } return(list[0]); }
protected internal new void Init() { if (Denominator.Value < 0) { Numerator = new IntegerNumber(-Numerator.Value); Denominator = new IntegerNumber(-Denominator.Value); } if (!EDecimalWrapper.IsEqual(Denominator.Value, 0)) { this.Value = CtxDivide((EDecimal)Numerator.Value, (EDecimal)Denominator.Value); } else { if (EDecimalWrapper.IsEqual(Numerator.Value, 0)) { State = UndefinedState.NAN; } else if (Numerator.Value > 0) { State = UndefinedState.POSITIVE_INFINITY; } else { State = UndefinedState.NEGATIVE_INFINITY; } } base.Init(); }
private void InitClass(IntegerNumber numerator, IntegerNumber denominator) { Numerator = numerator; Denominator = denominator; Type = HierarchyLevel.RATIONAL; Init(); }
protected internal new void Init() { if (Denominator.Value < 0) { Numerator = new IntegerNumber(-Numerator.Value); Denominator = new IntegerNumber(-Denominator.Value); } if (Denominator.Value != 0) { this.Value = (decimal)Numerator.Value / (decimal)Denominator.Value; } else { if (Numerator.Value == 0) { State = UndefinedState.NAN; } else if (Numerator.Value > 0) { State = UndefinedState.POSITIVE_INFINITY; } else { State = UndefinedState.NEGATIVE_INFINITY; } } base.Init(); }
public static IntegerNumber One() { var a = new IntegerNumber(); a.Value = 1; a.Denominator = a; a.Numerator = a; ((RationalNumber)a).Init(); return(a); }
internal static bool TryParse(string s, out IntegerNumber dst) { if (long.TryParse(s, out long res)) { dst = Number.Create(res); return(true); } dst = null; return(false); }
internal static bool TryParse(string s, out RationalNumber dst) { dst = null; var pos = s.IndexOf("/", StringComparison.Ordinal); if (pos == -1) { return(false); } var numString = s.Substring(0, pos).Replace(" ", ""); var denString = s.Substring(pos + 1).Replace(" ", ""); if (IntegerNumber.TryParse(numString, out var num) && IntegerNumber.TryParse(denString, out var den)) { dst = new RationalNumber(num, den); return(true); } return(false); }
public static bool operator ==(Number a, Number b) { if (a is null && b is null) { return(true); } if (a is null || b is null) { return(false); } (a, b, _) = Functional.MakeEqual(a, b); if (a.IsReal() && b.IsReal()) { var aAsReal = (a as RealNumber); var bAsReal = (b as RealNumber); if (!aAsReal.IsDefinite() && !bAsReal.IsDefinite()) { return(aAsReal.State == bAsReal.State); } else if (!aAsReal.IsDefinite() || !bAsReal.IsDefinite()) { return(false); } // else both are defined } if (a.Type != b.Type) { return(false); } return(SuperSwitch( num => IntegerNumber.AreEqual(num[0], num[1]), num => RationalNumber.AreEqual(num[0], num[1]), num => RealNumber.AreEqual(num[0], num[1]), num => ComplexNumber.AreEqual(num[0], num[1]), a.Type, a, b )); }
public static bool AreEqual(IntegerNumber a, IntegerNumber b) => a.Value.Equals(b.Value);
/// <summary> /// Use Number.Create(IntegerNumber, IntegerNumber) instead /// </summary> /// <param name="numerator"></param> /// <param name="denominator"></param> internal RationalNumber(IntegerNumber numerator, IntegerNumber denominator) { InitClass(numerator, denominator); }
public static bool AreEqual(IntegerNumber a, IntegerNumber b) => a.Value == b.Value;
/// <summary> /// Creates an instance of RationalNumber of two IntegerNumbers /// </summary> /// <param name="numerator"></param> /// <param name="denominator"></param> /// <returns> /// RationalNumber /// </returns> public static RationalNumber CreateRational(IntegerNumber numerator, IntegerNumber denominator) => Number.Functional.Downcast(new RationalNumber(numerator, denominator)) as RationalNumber;