private static SqlNumber DoubleOperation(SqlNumber number, Func <double, double> op) { if (!SqlNumber.IsNumber(number)) { return(number); } var value = (double)number; var result = op(value); if (Double.IsNaN(result)) { return(SqlNumber.NaN); } if (Double.IsPositiveInfinity(result)) { return(SqlNumber.PositiveInfinity); } if (Double.IsNegativeInfinity(result)) { return(SqlNumber.NegativeInfinity); } return((SqlNumber)result); }
public static SqlNumber Log(SqlNumber number, SqlNumber newBase) { if (SqlNumber.IsNumber(number)) { return((SqlNumber)System.Math.Log((double)number, (double)newBase)); } return(number); }
public static SqlNumber Sqrt(SqlNumber number) { if (SqlNumber.IsNumber(number)) { return(DoubleOperation(number, System.Math.Sqrt)); } return(number); }
public static SqlNumber Round(SqlNumber value, int precision) { if (SqlNumber.IsNumber(value)) { var result = BigMath.Round(value.innerValue, new MathContext(precision, RoundingMode.HalfUp)); return(new SqlNumber(result)); } return(value); }
public static SqlNumber Pow(SqlNumber number, SqlNumber exp) { if (SqlNumber.IsNumber(number)) { var result = BigMath.Pow(number.innerValue, exp.innerValue); return(new SqlNumber(result)); } return(number); }
public static SqlNumber Abs(SqlNumber number) { if (SqlNumber.IsNumber(number)) { return(new SqlNumber(SqlNumber.NumericState.None, BigMath.Abs(number.innerValue))); } if (SqlNumber.IsNegativeInfinity(number)) { return(SqlNumber.PositiveInfinity); } return(number); }
public static SqlNumber Multiply(SqlNumber a, SqlNumber b) { if (SqlNumber.IsNumber(a)) { if (SqlNumber.IsNumber(b)) { var result = BigMath.Multiply(a.innerValue, b.innerValue); return(new SqlNumber(SqlNumber.NumericState.None, result)); } return(b); } return(a); }
public static SqlNumber Subtract(SqlNumber a, SqlNumber b, int precision) { if (SqlNumber.IsNumber(a)) { if (SqlNumber.IsNumber(b)) { var context = new MathContext(precision); var result = BigMath.Subtract(a.innerValue, b.innerValue, context); return(new SqlNumber(SqlNumber.NumericState.None, result)); } return(new SqlNumber(b.InverseState(), null)); } return(a); }
public static SqlNumber Add(SqlNumber a, SqlNumber b, int precision) { if (SqlNumber.IsNumber(a)) { if (SqlNumber.IsNumber(b)) { var context = new MathContext(precision); var result = BigMath.Add(a.innerValue, b.innerValue, context); return(new SqlNumber(SqlNumber.NumericState.None, result)); } return(b); } return(a); }
public static SqlNumber Remainder(SqlNumber a, SqlNumber b) { if (SqlNumber.IsNumber(a)) { if (SqlNumber.IsNumber(b)) { BigDecimal divBy = b.innerValue; if (divBy.CompareTo(BigDecimal.Zero) != 0) { var remainder = BigMath.Remainder(a.innerValue, divBy); return(new SqlNumber(SqlNumber.NumericState.None, remainder)); } } } return(SqlNumber.NaN); }
public static SqlNumber Divide(SqlNumber a, SqlNumber b, int precision) { if (SqlNumber.IsNumber(a)) { if (SqlNumber.IsNumber(b)) { BigDecimal divBy = b.innerValue; if (divBy.CompareTo(BigDecimal.Zero) != 0) { var context = new MathContext(precision); var result = BigMath.Divide(a.innerValue, divBy, context); return(new SqlNumber(SqlNumber.NumericState.None, result)); } throw new DivideByZeroException(); } } // Return NaN if we can't divide return(SqlNumber.NaN); }