You are here: Home Resources CS C Sharp code for big numbers

C Sharp code for big numbers

by Andy Revenko last modified Dec 15, 2009 04:14 PM

BigNumbers.cs — C# source code, 8Kb

File contents

using System;

namespace BigNumbers
{
    public struct BigDecimal
    {
        private decimal x;
        private long order;

        public decimal X
        {
            get { return x; }
        }

        public long Order
        {
            get { return order; }
        }

        public BigDecimal(decimal x_, long power_)
        {
            decimal x1 = x_;
            long pow = (int)Math.Truncate(Math.Log10((double)x1));
            decimal precision = (decimal)(x1 / (decimal)Math.Pow(10, pow));
            x = precision;
            order = pow + power_;
        }

        public BigDecimal(double x_)
        {
            long pow = (int)Math.Truncate(Math.Log10(x_));
            decimal precision = (decimal)(x_ / Math.Pow(10, pow));
            x = precision;
            order = pow;
        }

        public BigDecimal(float x_)
        {
            long pow = (int)Math.Truncate(Math.Log10(x_));
            decimal precision = (decimal)(x_ / Math.Pow(10, pow));
            x = precision;
            order = pow;
        }

        private static BigDecimal RepresentInGivenOrder(BigDecimal a1, long order)
        {
            long difference;
            double d;
            BigDecimal a = new BigDecimal();
            difference = a1.order < order ? order - a1.order : a1.order - order;

            d = a1.order < order ?
                (double)((double)a1.x / Math.Pow(10, (double)difference)) :
                (double)((double)a1.x * Math.Pow(10, (double)difference));
            a1.order = order;

            if (double.IsNegativeInfinity(d) || double.IsNaN(d))
            {
                a1.x = decimal.MinValue;
            }
            else if (double.IsPositiveInfinity(d))
            {
                a1.x = decimal.MaxValue;
            }
            else
            {
                a1.x = (decimal)d;
            }

            a = a1;
            return a;
        }

        public static bool operator >(BigDecimal a1, BigDecimal a2)
        {
            BigDecimal a1_ = new BigDecimal();
            BigDecimal a2_ = new BigDecimal();
            a1_ = a1;
            a2_ = a2;

            if (a1_.order < a2_.order)
            {
                a1_ = BigDecimal.RepresentInGivenOrder(a1_, a2_.order);
            }
            else
            {
                a2_ = BigDecimal.RepresentInGivenOrder(a2_, a1_.order);
            }

            return (a1_.x > a2_.x);
        }

        public static bool operator <(BigDecimal a1, BigDecimal a2)
        {
            BigDecimal a1_ = new BigDecimal();
            BigDecimal a2_ = new BigDecimal();
            a1_ = a1;
            a2_ = a2;

            if (a1_.order < a2_.order)
            {
                a1_ = BigDecimal.RepresentInGivenOrder(a1_, a2_.order);
            }
            else
            {
                a2_ = BigDecimal.RepresentInGivenOrder(a2_, a1_.order);
            }

            return (a1_.x < a2_.x);
        }

        public static BigDecimal operator *(BigDecimal a1, BigDecimal a2)
        {
            long pow0 = a1.order + a2.order;
            decimal precision = a1.x * a2.x;
            long pow1 = (long)Math.Truncate(Math.Log10((double)precision));
            pow0 += pow1;
            precision = (decimal)((double)precision / Math.Pow(10, (double)pow1));
            BigDecimal a = new BigDecimal();
            a.x = precision;
            a.order = pow0;
            return a;
        }

        public static BigDecimal operator *(double d, BigDecimal bd)
        {
            BigDecimal leftBD = new BigDecimal(d);
            BigDecimal a = new BigDecimal();
            a = leftBD * bd;
            return a;
        }

        public static BigDecimal operator *(BigDecimal bd, double d)
        {
            BigDecimal rightBD = new BigDecimal(d);
            BigDecimal a = new BigDecimal();
            a = bd * rightBD;
            return a;
        }

        public static BigDecimal operator /(BigDecimal a1, BigDecimal a2)
        {
            long pow0 = a1.order - a2.order;
            decimal precision = a1.x / a2.x;
            long pow1 = (long)Math.Truncate(Math.Log10((double)precision));
            pow0 += pow1;
            precision = (decimal)((double)precision / Math.Pow(10, (double)pow1));
            BigDecimal a = new BigDecimal();
            a.x = precision;
            a.order = pow0;
            return a;
        }

        public static BigDecimal operator /(double d, BigDecimal bd)
        {
            BigDecimal leftBD = new BigDecimal(d);
            BigDecimal a = new BigDecimal();
            a = leftBD / bd;
            return a;
        }

        public static BigDecimal operator /(BigDecimal bd, double d)
        {
            BigDecimal rightBD = new BigDecimal(d);
            BigDecimal a = new BigDecimal();
            a = bd / rightBD;
            return a;
        }

        public static BigDecimal operator +(BigDecimal a1, BigDecimal a2)
        {
            long pow0;
            long difference;
            BigDecimal a = new BigDecimal();
            if (a1.order < a2.order)
            {
                difference = a2.order - a1.order;
                a1.order = a2.order;
                a1.x = (decimal)((double)a1.x / Math.Pow(10, (double)difference));
                a.x = a1.x + a2.x;
                pow0 = (long)Math.Truncate(Math.Log10((double)a.x));
                a.order = a1.order + pow0;
                a.x = (decimal)((double)a.x / Math.Pow(10, (double)pow0));
            }
            else
            {
                difference = a1.order - a2.order;
                a2.order = a1.order;
                a2.x = (decimal)((double)a2.x / Math.Pow(10, (double)difference));
                a.x = a1.x + a2.x;
                pow0 = (long)Math.Truncate(Math.Log10((double)a.x));
                a.order = a1.order + pow0;
                a.x = (decimal)((double)a.x / Math.Pow(10, (double)pow0));
            }

            return a;
        }

        public static BigDecimal operator +(BigDecimal a1, int i)
        {
            long pow0;
            long difference;
            BigDecimal a2 = new BigDecimal(i);
            BigDecimal a = new BigDecimal();
            if (a1.order < a2.order)
            {
                difference = a2.order - a1.order;
                a1.order = a2.order;
                a1.x = (decimal)((double)a1.x / Math.Pow(10, (double)difference));
                a.x = a1.x + a2.x;
                pow0 = (long)Math.Truncate(Math.Log10((double)a.x));
                a.order = a1.order + pow0;
                a.x = (decimal)((double)a.x / Math.Pow(10, (double)pow0));
            }
            else
            {
                difference = a1.order - a2.order;
                a2.order = a1.order;
                a2.x = (decimal)((double)a2.x / Math.Pow(10, (double)difference));
                a.x = a1.x + a2.x;
                pow0 = (long)Math.Truncate(Math.Log10((double)a.x));
                a.order = a1.order + pow0;
                a.x = (decimal)((double)a.x / Math.Pow(10, (double)pow0));
            }

            return a;
        }

        public static BigDecimal operator -(BigDecimal a1, BigDecimal a2)
        {
            long pow0;
            long difference;
            BigDecimal a = new BigDecimal();
            if (a1.order < a2.order)
            {
                difference = a2.order - a1.order;
                a1.order = a2.order;
                a1.x = (decimal)((double)a1.x / Math.Pow(10, (double)difference));
                a.x = a1.x - a2.x;
                pow0 = (long)Math.Truncate(Math.Log10(Math.Abs((double)a.x)));
                a.order = a1.order + pow0;
                a.x = (decimal)((double)a.x / Math.Pow(10, (double)pow0));
            }
            else
            {
                difference = a1.order - a2.order;
                a2.order = a1.order;
                a2.x = (decimal)((double)a2.x / Math.Pow(10, (double)difference));
                a.x = a1.x - a2.x;
                pow0 = (long)Math.Truncate(Math.Log10(Math.Abs((double)a.x)));
                a.order = a1.order + pow0;
                a.x = (decimal)((double)a.x / Math.Pow(10, (double)pow0));
            }
            return a;
        }

        public static BigDecimal Factorial(long digit)
        {
            BigDecimal bd = new BigDecimal(1);
            if (digit == 0)
            {
                return bd;
            }
            for (int i = 1; i <= digit; i++)
            {
                bd *= i;
            }
            return bd;
        }

        public override string ToString()
        {
            string s;
            s = this.x.ToString() + " E" + this.order.ToString();
            return s;
        }
    }

    
}
Document Actions
« May 2024 »
May
MoTuWeThFrSaSu
12345
6789101112
13141516171819
20212223242526
2728293031