Exemple #1
0
 static MathHelpers()
 {
     for (uint i = 0; i < MantissaLogs.Length; i++)
     {
         var n = new Ieee754 {
             UnsignedBits = i | 0x3F800000
         };                                                                     //added the implicit 1 leading bit
         MantissaLogs[i] = (float)Math.Log(n.Single, 2);
     }
 }
Exemple #2
0
 static FastLog()
 {
     //creating lookup table
     for (uint i = 0; i < MantissaLogs.Length; i++)
     {
         var n = new Ieee754 {
             UnsignedBits = i | 0x3F800000
         };                                                     //added the implicit 1 leading bit
         MantissaLogs[i] = (float)Math.Log(n.Single, 2);
     }
 }
Exemple #3
0
        internal static void ClassInitialize()
        {
            MantissaLogs = new float[(int)System.Math.Pow(2, 23)];

            // Initialize a lookup table
            // Size is about 838k and initialization time is approxiately 0.25 seconds
            for (uint i = 0; i < MantissaLogs.Length; i++)
            {
                Ieee754 n = new Ieee754 {
                    UnsignedBits = i | 0x3F800000
                };                                                 //added the implicit 1 leading bit
                MantissaLogs[i] = (float)System.Math.Log(n.Single, 2);
            }
        }
Exemple #4
0
        public static float Log2(float value)
        {
            if (value == 0F)
            {
                return(float.NegativeInfinity);
            }

            Ieee754 number = new Ieee754 {
                Single = value
            };

            if (number.UnsignedBits >> 31 == 1) //NOTE: didn't call Sign property for higher performance
            {
                return(float.NaN);
            }

            return((((number.SignedBits >> 23) & 0xFF) - 127) + MantissaLogs[number.UnsignedBits & 0x007FFFFF]);
            //NOTE: didn't call Exponent and Mantissa properties for higher performance
        }
Exemple #5
0
        public void GetIeee754Representation_DoubleNumber_Ieee754StringRepresentation(double number, string expectedResult)
        {
            string result = Ieee754.GetIeee754Representation(number);

            Assert.That(result, Is.EqualTo(expectedResult));
        }