Exemple #1
0
        public static Number Round(Number value, int digits)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(value, out Number result))
            {
                return(result);
            }

            int    i;
            Number b, e, j, m;

            b = value;
            for (i = 0; b >= Number.One; ++i)
            {
                b /= new Number(10);
            }

            int targetDigits = digits + 1 - i;

            b  = value;
            b *= NumberAlgorithms.Pow(new Number(10), targetDigits);
            e  = b + new Number(1, 2);
            if (e == Ceiling(b))
            {
                BigInteger f = Ceiling(b).Numerator;
                BigInteger h = f - new BigInteger(2);
                if (h % 2 != 0)
                {
                    e -= Number.One;
                }
            }
            j = Floor(e);
            m = NumberAlgorithms.Pow(new Number(10), targetDigits);
            return(j / m);
        }
Exemple #2
0
        /// <summary>
        /// Converts an angle specified in radians to gradians
        /// </summary>
        /// <param name="rad">angle in radians</param>
        /// <returns>angle in gradians</returns>
        public static Number RadToGrad(Number rad)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(rad, out Number result))
            {
                return(result);
            }

            return((rad * 200) / Pi);
        }
Exemple #3
0
        /// <summary>
        /// Converts an angle specified in gradians to degress
        /// </summary>
        /// <param name="grad">angle in gradians</param>
        /// <returns>angle in radians</returns>
        public static Number GradToRad(Number grad)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(grad, out Number result))
            {
                return(result);
            }

            return((grad * Pi) / 200.0);
        }
Exemple #4
0
        /// <summary>
        /// Converts an angle specified in degress to gradians
        /// </summary>
        /// <param name="deg">angle in gradians</param>
        /// <returns>angle in gradians</returns>
        public static Number DegToGrad(Number deg)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(deg, out Number result))
            {
                return(result);
            }

            return((400 * deg) / 360);
        }
Exemple #5
0
        /// <summary>
        /// Returns the cosine of the specified angle
        /// </summary>
        /// <param name="input">An angle, measured in units, specified by the AngleMode property</param>
        /// <returns>The cosine of input</returns>
        public static Number Cos(Number input)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(input, out Number result))
            {
                return(result);
            }

            return(ExecuteTrigometry(Math.Cos, input));
        }
Exemple #6
0
        /// <summary>
        /// Returns the cotangent of the specified angle
        /// </summary>
        /// <param name="input">An angle, measured in units, specified by the AngleMode property</param>
        /// <returns>The cotangent of input</returns>
        public static Number Ctg(Number input)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(input, out Number result))
            {
                return(result);
            }

            return(Cos(input) / Round(Sin(input), 21));
        }
        public static Number operator /(Number a, Number b)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(a, b, out Number value))
            {
                return(value);
            }

            return(a * new Number(b.Denominator, b.Numerator));
        }
Exemple #8
0
        /// <summary>
        /// Converts an angle specified in radians to degress
        /// </summary>
        /// <param name="deg">angle in degrees</param>
        /// <returns>angle in radians</returns>
        public static Number DegToRad(Number deg)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(deg, out Number result))
            {
                return(result);
            }

            return(Pi * (deg / 180.0));
        }
Exemple #9
0
        public static Number Floor(Number value)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(value, out Number result))
            {
                return(result);
            }

            return(Ceiling(value) - Number.One);
        }
        public static Number operator -(Number a, Number b)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(a, b, out Number value))
            {
                return(value);
            }

            return(a + -b);
        }
        public static Number operator -(Number a)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(a, out Number value))
            {
                return(value);
            }

            return(new Number(-a.Numerator, a.Denominator));
        }
Exemple #12
0
        /// <summary>
        ///  Returns the angle (measured in units, specified by the AngleMode property) whose cotangent is the specified number.
        /// </summary>
        /// <param name="input">An angle, measured in units, specified by the AngleMode property</param>
        /// <returns>An angle, measured in units, specified by the AngleMode property</returns>
        public static Number ArcCtg(Number input)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(input, out Number result))
            {
                return(result);
            }

            return(ExecuteInvertedTrigonometry((x) => Math.Atan((1.0 / Ctg(x)).ToDouble()), input));
        }
Exemple #13
0
        /// <summary>
        /// Converts an angle specified in gradians to degress
        /// </summary>
        /// <param name="grad">angle in gradians</param>
        /// <returns>angle in degrees</returns>
        public static Number GradToDeg(Number grad)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(grad, out Number result))
            {
                return(result);
            }

            return((360 * grad) / 400);
        }
Exemple #14
0
        /// <summary>
        /// Returns the angle (measured in units, specified by the AngleMode property) whose tangent is the specified number.
        /// </summary>
        /// <param name="input">A number representing a tangent</param>
        /// <returns>An angle, measured in units, specified by the AngleMode property</returns>
        public static Number ArcTan(Number input)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(input, out Number result))
            {
                return(result);
            }

            return(ExecuteInvertedTrigonometry(Math.Atan, input));
        }
Exemple #15
0
        public static Number Abs(Number value)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(value, out Number result))
            {
                return(result);
            }

            BigInteger num = BigInteger.Abs(value.Numerator);

            return(new Number(num, value.Denominator));
        }
        public static Number operator *(Number a, Number b)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(a, b, out Number value))
            {
                return(value);
            }

            Number fraction  = new Number(a.Numerator, b.Denominator);
            Number fraction2 = new Number(b.Numerator, a.Denominator);

            return(new Number(fraction.Numerator * fraction2.Numerator, fraction.Denominator * fraction2.Denominator));
        }
Exemple #17
0
        public static Number Truncate(Number value)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(value, out Number result))
            {
                return(result);
            }

            BigInteger numerator = value.Numerator;

            numerator -= BigInteger.Remainder(numerator, value.Denominator);
            return(new Number(numerator, value.Denominator));
        }
        public static Number operator +(Number a, Number b)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(a, b, out Number value))
            {
                return(value);
            }

            BigInteger num         = NumberAlgorithms.Gcd(a.Denominator, b.Denominator);
            BigInteger numerator   = a.Numerator * (b.Denominator / num) + b.Numerator * (a.Denominator / num);
            BigInteger denominator = a.Denominator * (b.Denominator / num);

            return(new Number(numerator, denominator));
        }
Exemple #19
0
        public static Number Ceiling(Number value)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(value, out Number result))
            {
                return(result);
            }

            BigInteger numerator   = value.Numerator;
            BigInteger denominator = value.Denominator;

            if (numerator < 0)
            {
                numerator -= BigInteger.Remainder(numerator, denominator);
            }
            else
            {
                numerator += denominator - BigInteger.Remainder(numerator, denominator);
            }

            return(new Number(numerator, denominator));
        }