コード例 #1
0
        public void Test_SingleToRawInt32BitsF()
        {
            int   i = 0x7fc004d2;
            float f = BitConversion.Int32BitsToSingle(i);

            Assert.IsTrue(BitConversion.SingleToRawInt32Bits(f) == i, "Wrong raw bits");
        }
コード例 #2
0
 public static float NextUp(this float value)
 {
     if (float.IsNaN(value) || value == float.PositiveInfinity)
     {
         return(value);
     }
     else
     {
         value += 0.0f;
         return(BitConversion.Int32BitsToSingle(BitConversion.SingleToRawInt32Bits(value) +
                                                ((value >= 0.0f) ? +1 : -1)));
     }
 }
コード例 #3
0
 public static float NextDown(this float value)
 {
     if (float.IsNaN(value) || value == float.NegativeInfinity)
     {
         return(value);
     }
     else
     {
         if (value == 0.0f)
         {
             return(-float.MinValue);
         }
         else
         {
             return(BitConversion.Int32BitsToSingle(BitConversion.SingleToRawInt32Bits(value) +
                                                    ((value > 0.0f) ? -1 : +1)));
         }
     }
 }
コード例 #4
0
        public static float NextAfter(this float start, double direction)
        {
            /*
             * The cases:
             *
             * NextAfter(+infinity, 0)  == MaxValue
             * NextAfter(+infinity, +infinity)  == +infinity
             * NextAfter(-infinity, 0)  == -MaxValue
             * NextAfter(-infinity, -infinity)  == -infinity
             *
             * are naturally handled without any additional testing
             */

            // First check for NaN values
            if (float.IsNaN(start) || double.IsNaN(direction))
            {
                // return a NaN derived from the input NaN(s)
                return(start + (float)direction);
            }
            else if (start == direction)
            {
                return((float)direction);
            }
            else
            {        // start > direction or start < direction
                     // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
                     // then bitwise convert start to integer.
                int transducer = BitConversion.SingleToRawInt32Bits(start + 0.0f);

                /*
                 * IEEE 754 floating-point numbers are lexicographically
                 * ordered if treated as signed- magnitude integers .
                 * Since .NET's integers are two's complement,
                 * incrementing" the two's complement representation of a
                 * logically negative floating-point value *decrements*
                 * the signed-magnitude representation. Therefore, when
                 * the integer representation of a floating-point values
                 * is less than zero, the adjustment to the representation
                 * is in the opposite direction than would be expected at
                 * first.
                 */
                if (direction > start)
                {// Calculate next greater value
                    transducer = transducer + (transducer >= 0 ? 1 : -1);
                }
                else
                { // Calculate next lesser value
                    Debug.Assert(direction < start);
                    if (transducer > 0)
                    {
                        --transducer;
                    }
                    else
                    if (transducer < 0)
                    {
                        ++transducer;
                    }

                    /*
                     * transducer==0, the result is -MinValue
                     *
                     * The transition from zero (implicitly
                     * positive) to the smallest negative
                     * signed magnitude value must be done
                     * explicitly.
                     */
                    else
                    {
                        transducer = SingleSignBitMask | 1;
                    }
                }

                return(BitConversion.Int32BitsToSingle(transducer));
            }
        }
コード例 #5
0
 public static bool IsNegativeZero(this float f)
 {
     return(f == 0 && BitConversion.SingleToRawInt32Bits(f) == BitConversion.SingleToRawInt32Bits(NegativeZero));
 }