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]); }
/// <summary> /// If the difference between value & round(value) is zero (see Number.IsZero), we consider value as an integer /// </summary> /// <param name="value"></param> /// <param name="res"></param> /// <returns></returns> private static bool TryCastToInt(decimal value, out BigInteger res) { var intPart = Math.Round(value); var rest = value - intPart; if (Number.IsZero(rest)) { res = (BigInteger)intPart; return(true); } else { return(false); } }