Пример #1
0
        public void WriteFloat(double value)
        {
            this.PrepareValue();

            // ReSharper disable once CompareOfFloatsByEqualityOperator
            if (!this.forceFloat64 && value == (float)value)
            {
                // TODO: Increase testing coverage
                this.containerStack.IncreaseCurrentContainerLength(5);
                this.dataBuffer.WriteByte(TidFloatByte | 4);
                this.dataBuffer.WriteUint32(BitConverterEx.SingleToInt32Bits((float)value));
            }
            else
            {
                this.containerStack.IncreaseCurrentContainerLength(9);
                this.dataBuffer.WriteByte(TidFloatByte | 8);

                if (double.IsNaN(value))
                {
                    // Double.NaN is different between C# and Java
                    // For consistency, map NaN to the long value for NaN in Java
                    this.dataBuffer.WriteUint64(0x7ff8000000000000L);
                }
                else
                {
                    this.dataBuffer.WriteUint64(BitConverter.DoubleToInt64Bits(value));
                }
            }

            this.FinishValue();
        }
Пример #2
0
        public void WriteFloat(double value)
        {
            PrepareValue();

            // ReSharper disable once CompareOfFloatsByEqualityOperator
            if (value == (float)value)
            {
                //TODO requires careful testing
                _containerStack.IncreaseCurrentContainerLength(5);
                _dataBuffer.WriteByte(TidFloatByte | 4);
                _dataBuffer.WriteUint32(BitConverterEx.SingleToInt32Bits((float)value));
            }
            else
            {
                _containerStack.IncreaseCurrentContainerLength(9);
                _dataBuffer.WriteByte(TidFloatByte | 8);
                _dataBuffer.WriteUint64(BitConverterEx.DoubleToInt64Bits(value));
            }

            FinishValue();
        }
Пример #3
0
        public static unsafe bool IsNegative(float f)
        {
            var bits = unchecked ((uint)BitConverter.SingleToInt32Bits(f));

            return((bits & 0x80000000) == 0x80000000);
        }
Пример #4
0
        public static unsafe bool IsNaN(float f)
        {
            var bits = BitConverter.SingleToInt32Bits(f);

            return((bits & 0x7FFFFFFF) > 0x7F800000);
        }
Пример #5
0
        public static bool IsFinite(float f)
        {
            var bits = BitConverter.SingleToInt32Bits(f);

            return((bits & 0x7FFFFFFF) < 0x7F800000);
        }
Пример #6
0
        public static float Round(float x)
        {
            // ************************************************************************************
            // IMPORTANT: Do not change this implementation without also updating MathF.Round(float),
            //            FloatingPointUtils::round(double), and FloatingPointUtils::round(float)
            // ************************************************************************************

            // This is based on the 'Berkeley SoftFloat Release 3e' algorithm

            uint bits     = (uint)BitConverter.SingleToInt32Bits(x);
            int  exponent = float.ExtractExponentFromBits(bits);

            if (exponent <= 0x7E)
            {
                if ((bits << 1) == 0)
                {
                    // Exactly +/- zero should return the original value
                    return(x);
                }

                // Any value less than or equal to 0.5 will always round to exactly zero
                // and any value greater than 0.5 will always round to exactly one. However,
                // we need to preserve the original sign for IEEE compliance.

                float result = ((exponent == 0x7E) && (float.ExtractSignificandFromBits(bits) != 0)) ? 1.0f : 0.0f;
                return(CopySign(result, x));
            }

            if (exponent >= 0x96)
            {
                // Any value greater than or equal to 2^23 cannot have a fractional part,
                // So it will always round to exactly itself.

                return(x);
            }

            // The absolute value should be greater than or equal to 1.0 and less than 2^23
            Debug.Assert((0x7F <= exponent) && (exponent <= 0x95));

            // Determine the last bit that represents the integral portion of the value
            // and the bits representing the fractional portion

            uint lastBitMask   = 1U << (0x96 - exponent);
            uint roundBitsMask = lastBitMask - 1;

            // Increment the first fractional bit, which represents the midpoint between
            // two integral values in the current window.

            bits += lastBitMask >> 1;

            if ((bits & roundBitsMask) == 0)
            {
                // If that overflowed and the rest of the fractional bits are zero
                // then we were exactly x.5 and we want to round to the even result

                bits &= ~lastBitMask;
            }
            else
            {
                // Otherwise, we just want to strip the fractional bits off, truncating
                // to the current integer value.

                bits &= ~roundBitsMask;
            }

            return(BitConverter.Int32BitsToSingle((int)bits));
        }
Пример #7
0
 public static unsafe bool IsNegative(float f)
 {
     return(BitConverter.SingleToInt32Bits(f) < 0);
 }
Пример #8
0
        public static unsafe bool IsInfinity(float f)
        {
            int bits = BitConverter.SingleToInt32Bits(f);

            return((bits & 0x7FFFFFFF) == 0x7F800000);
        }
Пример #9
0
 public Variant(float val)
 {
     _objref = null;
     _flags  = CV_R4;
     _data   = (uint)BitConverter.SingleToInt32Bits(val);
 }