public NumberClass Pow(NumberClass n) { if (n == One || this == One || this == Zero) { return(this); } if (n == Zero) { return(One); } if (exponent == 0 && n.exponent == 0) { return(Math.Pow(mantissa, n.mantissa)); } var tempExpo = exponent + Math.Log10(mantissa); if (Math.Max(Math.Log10(exponent), 0) + n.exponent < 300) { tempExpo *= n.GetRealMantissa(); return(tempExpo < 1e17 ? new NumberClass(Math.Pow(10, tempExpo % 1), Math.Floor(tempExpo)) : new NumberClass(mantissa, tempExpo)); } tempExpo = Math.Log10(tempExpo); tempExpo += n.exponent + Math.Log10(n.exponent); return(new NumberClass(mantissa, tempExpo)); }
public string FormatNc(Format format) { if (exponent < beforeSciCut) { return($"{mantissa * Math.Pow(10, exponent):#,##0.##}"); } var useMan = !IsMantissaUseless(); // if take mantissa or leave it // does not catch engineering but w.e. is probs fine string CutOff1Check(string s) => !cutOff1E ? s : RegReplace.Replace(s, "ee"); // get proper format // can be #.000 or #.### string GetFormatFromCount(int count, bool optional = false) => $"#.{string.Join("", Enumerable.Repeat(optional ? '#' : '0', count))}"; string formatMantissa; string formatExponent; switch (format) { case Format.Engineering: var extended = exponent % 3; formatMantissa = useMan ? $"{mantissa * Math.Pow(10, extended):##0.00#}" : ""; formatExponent = new NumberClass(exponent - extended).FormatNc(Format.Engineering) .Replace("1e", "e"); return(CutOff1Check($"{formatMantissa}e{formatExponent}")); case Format.ScientificStatic: // format to keep numclass the same leng formatExponent = new NumberClass(exponent).FormatNc(Format.Scientific); formatMantissa = useMan ? $"{mantissa.ToString(GetFormatFromCount(sciStaticLeng))}" : ""; return(CutOff1Check($"{formatMantissa}e{formatExponent}")); default: formatMantissa = useMan ? $"{mantissa.ToString(GetFormatFromCount(format != Format.ScientificExtended ? 2 : 6))}" : ""; formatExponent = new NumberClass(exponent).FormatNc(Format.Scientific); return(CutOff1Check($"{formatMantissa}e{formatExponent}")); } }
public NumberClass(NumberClass nc) : this(nc.mantissa, nc.exponent) { }
public NumberClass Min(NumberClass n) => n < this ? n : this;
public NumberClass Max(NumberClass n) => n > this ? n : this;
private int BaseNegComp(NumberClass n) => !IsNeg() && n.IsNeg() ? 1 : IsNeg() && !n.IsNeg() ? -1 : 0;
public NumberClass Log(NumberClass @base) => this == Zero ? Zero : Log10() / @base.Log10();