コード例 #1
0
        public override string /*!*/ ToString()
        {
            Contract.Ensures(Contract.Result <string>() != null);
            if (value == "")
            {
                byte[]        sigBytes = significand.ToByteArray();
                StringBuilder binSig   = new StringBuilder();

                if (exponent == 0)
                {
                    binSig.Append('0');
                }
                else
                {
                    binSig.Append('1'); //hidden bit
                }

                for (int i = significandSize - 2; i >= 0; --i) //little endian
                {
                    if (i / 8 < sigBytes.Length)
                    {
                        binSig.Append((char)('0' + ((sigBytes[i / 8] >> (i % 8)) & 1)));
                    }
                    else
                    {
                        binSig.Append('0');
                    }
                }

                BIM bias = two_n(exponentSize - 1) - 1;
                if (exponent == 0)
                {
                    --bias;
                }

                int moveDec  = ((int)((exponent - bias) % 4) + 4) % 4;
                BIM finalExp = (exponent - bias - moveDec) / 4;

                string leftBinSig  = binSig.ToString().Substring(0, moveDec + 1);
                string rightBinSig = binSig.ToString().Substring(moveDec + 1);

                leftBinSig  = new string('0', 4 - leftBinSig.Length % 4) + leftBinSig;
                rightBinSig = rightBinSig + new string('0', 4 - rightBinSig.Length % 4);

                StringBuilder leftHexSig  = new StringBuilder();
                StringBuilder rightHexSig = new StringBuilder();

                for (int i = 0; i < leftBinSig.Length / 4; ++i)
                {
                    leftHexSig.AppendFormat("{0:X}", Convert.ToByte(leftBinSig.Substring(i * 4, 4), 2));
                }
                for (int i = 0; i < rightBinSig.Length / 4; ++i)
                {
                    rightHexSig.AppendFormat("{0:X}", Convert.ToByte(rightBinSig.Substring(i * 4, 4), 2));
                }

                return(String.Format("{0}0x{1}.{2}e{3}f{4}e{5}", isNeg ? "-" : "", leftHexSig, rightHexSig, finalExp, significandSize, exponentSize));
            }
            return(String.Format("0{0}{1}e{2}", value, significandSize, exponentSize));
        }