예제 #1
0
        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);
        }
예제 #2
0
        public static SqlNumber Log(SqlNumber number, SqlNumber newBase)
        {
            if (SqlNumber.IsNumber(number))
            {
                return((SqlNumber)System.Math.Log((double)number, (double)newBase));
            }

            return(number);
        }
예제 #3
0
        public static SqlNumber Sqrt(SqlNumber number)
        {
            if (SqlNumber.IsNumber(number))
            {
                return(DoubleOperation(number, System.Math.Sqrt));
            }

            return(number);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }
예제 #8
0
        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);
        }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        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);
        }