Пример #1
0
        /**
         * Convert to an equivalent {@link ExpandedDouble} representation (binary frac and exponent).
         * The resulting transformed object is easily Converted to a 64 bit IEEE double:
         * <ul>
         * <li>bits 2-53 of the {@link #GetSignificand()} become the 52 bit 'fraction'.</li>
         * <li>{@link #GetBinaryExponent()} is biased by 1023 to give the 'exponent'.</li>
         * </ul>
         * The sign bit must be obtained from somewhere else.
         * @return a new {@link NormalisedDecimal} normalised to base 2 representation.
         */

        public ExpandedDouble NormaliseBaseTwo()
        {
            MutableFPNumber cc = new MutableFPNumber(ComposeFrac(), 39);

            cc.multiplyByPowerOfTen(_relativeDecimalExponent);
            cc.Normalise64bit();
            return(cc.CreateExpandedDouble());
        }
Пример #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));
        }