public static NumericValue Power(FractionValue x, FractionValue exponent) { NumericValue result; // Wikipedia has good info on exponentiation with fractional powers. // http://en.wikipedia.org/wiki/Exponentiation#Principal_n-th_root if (exponent.Denominator == 1) { result = new FractionValue(BigRational.Pow(x.value, exponent.Numerator)); } else if (x.Sign < 0 && exponent.Sign > 0 && !exponent.Denominator.IsEven) { // We can do a better job on odd roots of negative fractions than // DoubleValue can. It ends up deferring to Complex.Pow, which // returns non-principal roots. This will return -2 as the cube root // of -8, whereas Complex.Pow would return (1, 1.73205080756888). BigRational radicand = BigRational.Pow(x.value, exponent.Numerator); double rootPower = 1 / (double)exponent.Denominator; double value = radicand.Sign * Math.Pow(Math.Abs((double)radicand), rootPower); result = new DoubleValue(value); } else if (x.Sign > 0 && exponent.Sign > 0) { // Take the power of the numerator and denominator separately. // Then if we end up with two integers, we can make a fraction. double exponentDouble = exponent.ToDouble(); double resultNumeratorDouble = Math.Pow((double)x.Numerator, exponentDouble); double resultDenominatorDouble = Math.Pow((double)x.Denominator, exponentDouble); if (Utility.IsInteger(resultNumeratorDouble, out BigInteger resultNumerator) && Utility.IsInteger(resultDenominatorDouble, out BigInteger resultDenominator)) { result = new FractionValue(resultNumerator, resultDenominator); } else { result = new DoubleValue(resultNumeratorDouble / resultDenominatorDouble); } } else { result = DoubleValue.Power(new DoubleValue(x.ToDouble()), new DoubleValue(exponent.ToDouble())); } return(result); }
private static string GetDecimalFormat(BigRational value, char separator, Calculator calc, out bool isDecimalFormat) { string result; // If converting from a fraction to a double overflows, we'll return the common format instead. double doubleValue = (double)value; if (double.IsInfinity(doubleValue) || double.IsNaN(doubleValue)) { result = GetCommonFormat(value, separator); isDecimalFormat = false; } else { result = DoubleValue.Format(doubleValue, calc); isDecimalFormat = true; } return(result); }
private static string GetAlgebraicFormat(Complex value, Calculator calc) { StringBuilder sb = new(); if (value.Real == 0 && value.Imaginary != 0) { sb.Append(DoubleValue.Format(value.Imaginary, calc)); sb.Append('i'); } else { sb.Append(DoubleValue.Format(value.Real, calc)); if (value.Imaginary != 0) { sb.Append(Math.Sign(value.Imaginary) == -1 ? " - " : " + "); sb.Append(DoubleValue.Format(Math.Abs(value.Imaginary), calc)); sb.Append('i'); } } return(sb.ToString()); }
public static TimeSpanValue Divide(TimeSpanValue x, DoubleValue y) { return(new TimeSpanValue(TimeSpan.FromTicks((long)(x.AsTimeSpan.Ticks / y.AsDouble)))); }
public static TimeSpanValue Multiply(DoubleValue x, TimeSpanValue y) { return(Multiply(y, x)); }
public static TimeSpanValue Multiply(TimeSpanValue x, DoubleValue y) { return(new TimeSpanValue(TimeSpan.FromTicks((long)(x.AsTimeSpan.Ticks * y.AsDouble)))); }