コード例 #1
0
        public static string ToExactString(double d)
        {
            if (double.IsPositiveInfinity(d))
            {
                return("+Infinity");
            }
            if (double.IsNegativeInfinity(d))
            {
                return("-Infinity");
            }
            if (double.IsNaN(d))
            {
                return("NaN");
            }
            long num  = BitConverter.DoubleToInt64Bits(d);
            bool flag = num < 0L;
            int  num2 = (int)((num >> 0x34) & 0x7ffL);
            long x    = num & 0xfffffffffffffL;

            if (num2 == 0)
            {
                num2++;
            }
            else
            {
                x |= 0x10000000000000L;
            }
            num2 -= 0x433;
            if (x == 0)
            {
                return("0");
            }
            while ((x & 1L) == 0)
            {
                x = x >> 1;
                num2++;
            }
            ArbitraryDecimal num4 = new ArbitraryDecimal(x);

            if (num2 < 0)
            {
                for (int i = 0; i < -num2; i++)
                {
                    num4.MultiplyBy(5);
                }
                num4.Shift(-num2);
            }
            else
            {
                for (int j = 0; j < num2; j++)
                {
                    num4.MultiplyBy(2);
                }
            }
            if (flag)
            {
                return("-" + num4.ToString());
            }
            return(num4.ToString());
        }
コード例 #2
0
 public static string ToExactString(double d)
 {
     if (double.IsPositiveInfinity(d))
     {
         return "+Infinity";
     }
     if (double.IsNegativeInfinity(d))
     {
         return "-Infinity";
     }
     if (double.IsNaN(d))
     {
         return "NaN";
     }
     long num = BitConverter.DoubleToInt64Bits(d);
     bool flag = num < 0L;
     int num2 = (int) ((num >> 0x34) & 0x7ffL);
     long x = num & 0xfffffffffffffL;
     if (num2 == 0)
     {
         num2++;
     }
     else
     {
         x |= 0x10000000000000L;
     }
     num2 -= 0x433;
     if (x == 0)
     {
         return "0";
     }
     while ((x & 1L) == 0)
     {
         x = x >> 1;
         num2++;
     }
     ArbitraryDecimal num4 = new ArbitraryDecimal(x);
     if (num2 < 0)
     {
         for (int i = 0; i < -num2; i++)
         {
             num4.MultiplyBy(5);
         }
         num4.Shift(-num2);
     }
     else
     {
         for (int j = 0; j < num2; j++)
         {
             num4.MultiplyBy(2);
         }
     }
     if (flag)
     {
         return ("-" + num4.ToString());
     }
     return num4.ToString();
 }
コード例 #3
0
        /// <summary>
        /// Converts the given double to a string representation of its
        /// exact decimal value.
        /// </summary>
        /// <param name="d">The double to convert.</param>
        /// <returns>A string representation of the double's exact decimal value.</returns>
        public static string ToExactString(double d)
        {
            if (double.IsPositiveInfinity(d))
            {
                return("+Infinity");
            }
            if (double.IsNegativeInfinity(d))
            {
                return("-Infinity");
            }
            if (double.IsNaN(d))
            {
                return("NaN");
            }

            // Translate the double into sign, exponent and mantissa.
            long bits     = BitConverter.DoubleToInt64Bits(d);
            bool negative = (bits < 0);
            int  exponent = (int)((bits >> 52) & 0x7ffL);
            long mantissa = bits & 0xfffffffffffffL;

            // Subnormal numbers; exponent is effectively one higher,
            // but there's no extra normalisation bit in the mantissa
            if (exponent == 0)
            {
                exponent++;
            }
            // Normal numbers; leave exponent as it is but add extra
            // bit to the front of the mantissa
            else
            {
                mantissa = mantissa | (1L << 52);
            }

            // Bias the exponent. It's actually biased by 1023, but we're
            // treating the mantissa as m.0 rather than 0.m, so we need
            // to subtract another 52 from it.
            exponent -= 1075;

            if (mantissa == 0)
            {
                return("0");
            }

            /* Normalize */
            while ((mantissa & 1) == 0)
            {                /*  i.e., Mantissa is even */
                mantissa >>= 1;
                exponent++;
            }

            // Construct a new decimal expansion with the mantissa
            ArbitraryDecimal ad = new ArbitraryDecimal(mantissa);

            // If the exponent is less than 0, we need to repeatedly
            // divide by 2 - which is the equivalent of multiplying
            // by 5 and dividing by 10.
            if (exponent < 0)
            {
                for (int i = 0; i < -exponent; i++)
                {
                    ad.MultiplyBy(5);
                }
                ad.Shift(-exponent);
            }
            // Otherwise, we need to repeatedly multiply by 2
            else
            {
                for (int i = 0; i < exponent; i++)
                {
                    ad.MultiplyBy(2);
                }
            }

            // Finally, return the string with an appropriate sign
            if (negative)
            {
                return("-" + ad.ToString());
            }
            else
            {
                return(ad.ToString());
            }
        }
コード例 #4
0
ファイル: DoubleConverter.cs プロジェクト: dioptre/nkd
		/// <summary>
		/// Converts the given double to a string representation of its
		/// exact decimal value.
		/// </summary>
		/// <param name="d">The double to convert.</param>
		/// <returns>A string representation of the double's exact decimal value.</returns>
		public static string ToExactString (double d)
		{
			if (double.IsPositiveInfinity(d))
				return "+Infinity";
			if (double.IsNegativeInfinity(d))
				return "-Infinity";
			if (double.IsNaN(d))
				return "NaN";

			// Translate the double into sign, exponent and mantissa.
			long bits = BitConverter.DoubleToInt64Bits(d);
			bool negative = (bits < 0);
			int exponent = (int) ((bits >> 52) & 0x7ffL);
			long mantissa = bits & 0xfffffffffffffL;

			// Subnormal numbers; exponent is effectively one higher,
			// but there's no extra normalisation bit in the mantissa
			if (exponent==0)
			{
				exponent++;
			}
			// Normal numbers; leave exponent as it is but add extra
			// bit to the front of the mantissa
			else
			{
				mantissa = mantissa | (1L<<52);
			}
	        
			// Bias the exponent. It's actually biased by 1023, but we're
			// treating the mantissa as m.0 rather than 0.m, so we need
			// to subtract another 52 from it.
			exponent -= 1075;
	        
			if (mantissa == 0) 
			{
				return "0";
			}
	        
			/* Normalize */
			while((mantissa & 1) == 0) 
			{    /*  i.e., Mantissa is even */
				mantissa >>= 1;
				exponent++;
			}
	        
			// Construct a new decimal expansion with the mantissa
			ArbitraryDecimal ad = new ArbitraryDecimal (mantissa);
	        
			// If the exponent is less than 0, we need to repeatedly
			// divide by 2 - which is the equivalent of multiplying
			// by 5 and dividing by 10.
			if (exponent < 0) 
			{
				for (int i=0; i < -exponent; i++)
					ad.MultiplyBy(5);
				ad.Shift(-exponent);
			} 
				// Otherwise, we need to repeatedly multiply by 2
			else
			{
				for (int i=0; i < exponent; i++)
					ad.MultiplyBy(2);
			}
	        
			// Finally, return the string with an appropriate sign
			if (negative)
				return "-"+ad.ToString();
			else
				return ad.ToString();
		}
コード例 #5
0
        public static string ToExactString(double d)
        {
            if (double.IsPositiveInfinity(d))
            {
                return("+Infinity");
            }
            if (double.IsNegativeInfinity(d))
            {
                return("-Infinity");
            }
            if (double.IsNaN(d))
            {
                return("NaN");
            }

            long bits     = BitConverter.DoubleToInt64Bits(d);
            bool negative = (bits < 0);
            int  exponent = (int)((bits >> 52) & 0x7ffL);
            long mantissa = bits & 0xfffffffffffffL;

            if (exponent == 0)
            {
                exponent++;
            }
            else
            {
                mantissa = mantissa | (1L << 52);
            }

            exponent -= 1075;

            if (mantissa == 0)
            {
                return("0");
            }

            while ((mantissa & 1) == 0)
            {
                mantissa >>= 1;
                exponent++;
            }

            ArbitraryDecimal ad = new ArbitraryDecimal(mantissa);

            if (exponent < 0)
            {
                for (int i = 0; i < -exponent; i++)
                {
                    ad.MultiplyBy(5);
                }
                ad.Shift(-exponent);
            }

            else
            {
                for (int i = 0; i < exponent; i++)
                {
                    ad.MultiplyBy(2);
                }
            }

            if (negative)
            {
                return("-" + ad.ToString());
            }
            else
            {
                return(ad.ToString());
            }
        }
コード例 #6
0
        /// <summary>
        /// Converts the given double to a string representation of its
        /// exact decimal value.
        /// </summary>
        /// <param name="d">The double to convert.</param>
        /// <returns>A string representation of the double's exact decimal value.</returns>
        public static ArbitraryDecimal ToArbitaryDecimal(double d, int precision = 6)
        {
            // Translate the double into sign, exponent and mantissa.
            long bits = BitConverter.DoubleToInt64Bits(d);
            // Note that the shift is sign-extended, hence the test against -1 not 1
            bool negative = (bits < 0);
            int  exponent = (int)((bits >> 52) & 0x7ffL);
            long mantissa = bits & 0xfffffffffffffL;

            // Subnormal numbers; exponent is effectively one higher,
            // but there's no extra normalisation bit in the mantissa
            if (exponent == 0)
            {
                exponent++;
            }
            // Normal numbers; leave exponent as it is but add extra
            // bit to the front of the mantissa
            else
            {
                mantissa = mantissa | (1L << 52);
            }

            // Bias the exponent. It's actually biased by 1023, but we're
            // treating the mantissa as m.0 rather than 0.m, so we need
            // to subtract another 52 from it.
            exponent -= 1075;

            if (mantissa == 0)
            {
                throw new NotImplementedException();
                //return "0";
            }

            /* Normalize */
            while ((mantissa & 1) == 0)
            {                /*  i.e., Mantissa is even */
                mantissa >>= 1;
                exponent++;
            }

            // Construct a new decimal expansion with the mantissa
            ArbitraryDecimal ad = new ArbitraryDecimal(mantissa);

            // If the exponent is less than 0, we need to repeatedly
            // divide by 2 - which is the equivalent of multiplying
            // by 5 and dividing by 10.
            if (exponent < 0)
            {
                for (int i = 0; i < -exponent; i++)
                {
                    ad.MultiplyBy(5);
                }
                ad.Shift(-exponent);
            }
            // Otherwise, we need to repeatedly multiply by 2
            else
            {
                for (int i = 0; i < exponent; i++)
                {
                    ad.MultiplyBy(2);
                }
            }
            return(ad);
        }