/// <summary>
    /// Returns the adjacent floating-point value in the direction of
    /// positive infinity.
    /// </summary>
    /// <param name="value">Starting floating-point value.</param>
    public static float NextUp(this float value)
    {
#if NETCOREAPP3_1_OR_GREATER
        return(MathF.BitIncrement(value));
#else
        int bits = value.AsInt();

        if ((bits & 0x7F800000) >= 0x7F800000)
        {
            // NaN returns NaN
            // -Infinity returns float.MinValue
            // +Infinity returns +Infinity
            return((bits == unchecked ((int)0xFF800000)) ? float.MinValue : value);
        }

        if (bits == unchecked ((int)0x80000000))
        {
            // -0.0 returns float.Epsilon
            return(float.Epsilon);
        }

        // Negative values need to be decremented
        // Positive values need to be incremented

        bits += (bits < 0) ? -1 : +1;
        return(bits.AsFloat());
#endif
    }
예제 #2
0
파일: Program.cs 프로젝트: gro-ser/useless
        private static void Main(string[] args)
        {
            bool f = getSomeBool();

            Console.WriteLine(f);
            Console.WriteLine(f == true);
            Console.WriteLine(f.Equals(true));

            void print(float x) =>
            Console.WriteLine("x = {0,6} : 0x{1:X}", x,
                              BitConverter.DoubleToInt64Bits(x));

            float x = 0f;

            print(x);
            x = MathF.BitDecrement(x);
            print(x);
            x = MathF.BitIncrement(x);
            print(x);
        }