Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public Number(BigInteger numerator, BigInteger denominator)
        {
            if (denominator == BigInteger.Zero)
            {
                numerator   = 0;
                denominator = 0;
                State       = NumberState.NaN;
            }
            BigInteger num = NumberAlgorithms.Gcd(numerator, denominator);

            if (num == BigInteger.Zero)
            {
                State = NumberState.PositiveInfinity;
            }
            else if (denominator < BigInteger.Zero)
            {
                Numerator   = -numerator / num;
                Denominator = -denominator / num;
            }
            else
            {
                Numerator   = numerator / num;
                Denominator = denominator / num;
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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));
        }
Ejemplo n.º 7
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));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 9
0
        public static Number operator -(Number a, Number b)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(a, b, out Number value))
            {
                return(value);
            }

            return(a + -b);
        }
Ejemplo n.º 10
0
        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));
        }
Ejemplo n.º 11
0
        public static Number operator -(Number a)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(a, out Number value))
            {
                return(value);
            }

            return(new Number(-a.Numerator, a.Denominator));
        }
Ejemplo n.º 12
0
        public static Number Floor(Number value)
        {
            if (NumberAlgorithms.TryHandleSpecialCase(value, out Number result))
            {
                return(result);
            }

            return(Ceiling(value) - Number.One);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 14
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));
        }
Ejemplo n.º 15
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));
        }
Ejemplo n.º 16
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));
        }
Ejemplo n.º 17
0
        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));
        }
Ejemplo n.º 18
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));
        }
Ejemplo n.º 19
0
        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));
        }
Ejemplo n.º 20
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));
        }
Ejemplo n.º 21
0
        public string ToString(IFormatProvider formatProvider)
        {
            if (NumberAlgorithms.TryHandleSpecialToString(this, out string special))
            {
                return(special);
            }

            BigInteger integerPart = BigInteger.DivRem(Numerator, Denominator, out BigInteger remainder);

            int padding = (Denominator.ToString().Length - Numerator.ToString().Length);

            if (Numerator.Sign != -1)
            {
                padding -= 1;
            }

            string pad = string.Empty;

            if (padding > 0)
            {
                pad = pad.PadLeft(padding, '0');
            }

            if (remainder == BigInteger.Zero)
            {
                return(integerPart.ToString());
            }

            BigInteger num = Numerator;

            if (Numerator.Sign == -1 && integerPart == 0)
            {
                num *= -1;
            }
            BigInteger decimals = (num * BigInteger.Pow(10, Precision)) / Denominator;

            if (decimals == BigInteger.Zero)
            {
                return(integerPart.ToString());
            }

            Stack <char> digits = new Stack <char>();

            int i = Precision;

            while (i-- > 0 && decimals > 0)
            {
                var digit = (decimals % 10).ToString();
                digits.Push(digit[0]);
                decimals /= 10;
            }

            string floatPart = new string(digits.ToArray()).TrimEnd('0');

            if (Numerator.Sign == -1 && integerPart == 0)
            {
                return($"-{integerPart}.{pad}{floatPart}");
            }

            return($"{integerPart}.{pad}{floatPart}");
        }