コード例 #1
0
ファイル: NormalisedDecimal.cs プロジェクト: xoposhiy/npoi
        public static NormalisedDecimal Create(BigInteger frac, int binaryExponent)
        {
            // estimate pow2&pow10 first, perform optional mulShift, then normalize
            int pow10;
            if (binaryExponent > 49 || binaryExponent < 46)
            {

                // working with ints (left Shifted 20) instead of doubles
                // x = 14.5 - binaryExponent * log10(2);
                int x = (29 << 19) - binaryExponent * LOG_BASE_10_OF_2_TIMES_2_POW_20;
                x += C_2_POW_19; // round
                pow10 = -(x >> 20);
            }
            else
            {
                pow10 = 0;
            }
            MutableFPNumber cc = new MutableFPNumber(frac, binaryExponent);
            if (pow10 != 0)
            {
                cc.multiplyByPowerOfTen(-pow10);
            }

            switch (cc.Get64BitNormalisedExponent())
            {
                case 46:
                    if (cc.IsAboveMinRep())
                    {
                        break;
                    }
                    goto case 44;
                case 44:
                case 45:
                    cc.multiplyByPowerOfTen(1);
                    pow10--;
                    break;
                case 47:
                case 48:
                    break;
                case 49:
                    if (cc.IsBelowMaxRep())
                    {
                        break;
                    }
                    goto case 50;
                case 50:
                    cc.multiplyByPowerOfTen(-1);
                    pow10++;
                    break;

                default:
                    throw new InvalidOperationException("Bad binary exp " + cc.Get64BitNormalisedExponent() + ".");
            }
            cc.Normalise64bit();

            return cc.CreateNormalisedDecimal(pow10);
        }
コード例 #2
0
        public static NormalisedDecimal Create(BigInteger frac, int binaryExponent)
        {
            // estimate pow2&pow10 first, perform optional mulShift, then normalize
            int pow10;

            if (binaryExponent > 49 || binaryExponent < 46)
            {
                // working with ints (left Shifted 20) instead of doubles
                // x = 14.5 - binaryExponent * log10(2);
                int x = (29 << 19) - binaryExponent * LOG_BASE_10_OF_2_TIMES_2_POW_20;
                x    += C_2_POW_19; // round
                pow10 = -(x >> 20);
            }
            else
            {
                pow10 = 0;
            }
            MutableFPNumber cc = new MutableFPNumber(frac, binaryExponent);

            if (pow10 != 0)
            {
                cc.multiplyByPowerOfTen(-pow10);
            }

            switch (cc.Get64BitNormalisedExponent())
            {
            case 46:
                if (cc.IsAboveMinRep())
                {
                    break;
                }
                goto case 44;

            case 44:
            case 45:
                cc.multiplyByPowerOfTen(1);
                pow10--;
                break;

            case 47:
            case 48:
                break;

            case 49:
                if (cc.IsBelowMaxRep())
                {
                    break;
                }
                goto case 50;

            case 50:
                cc.multiplyByPowerOfTen(-1);
                pow10++;
                break;

            default:
                throw new InvalidOperationException("Bad binary exp " + cc.Get64BitNormalisedExponent() + ".");
            }
            cc.Normalise64bit();

            return(cc.CreateNormalisedDecimal(pow10));
        }