Ejemplo n.º 1
0
 /// <summary>
 /// Test if two float are equal by testing that the
 /// pattern of bits returned by
 /// BitConverter.ToInt32(BitConverter.GetBytes(float), 0) are equal
 /// (BitConverter does not have SingleToInt32Bits() so we use this
 /// 2-call workaround <see href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=426391"/>
 /// to roll our own implementation).
 /// This handles NaNs, Infinties, and &ltcode>-0.0.
 /// It is compatible with the hash code generated by
 /// HashCodeBuilder.
 /// </summary>
 /// <param name="lhs">the left hand float</param>
 /// <param name="rhs">the right hand float</param>
 /// <returns>EqualsBuilder - used to chain calls.</returns>
 public EqualsBuilder Append(float lhs, float rhs)
 {
     if (isEqual == false)
     {
         return(this);
     }
     // java: return append(Float.floatToIntBits(lhs), Float.floatToIntBits(rhs));
     return(Append(
                BitConverterUtil.SingleToInt32Bits(lhs),
                BitConverterUtil.SingleToInt32Bits(rhs)));
 }
Ejemplo n.º 2
0
        public void TestSingleToInt32Bits()
        {
            Assert.IsTrue(BitConverterUtil.SingleToInt32Bits(24.154f) == 1103182692);
            Assert.IsTrue(BitConverterUtil.SingleToInt32Bits(1.0f) == 1065353216);
            Assert.IsTrue(BitConverterUtil.SingleToInt32Bits(float.PositiveInfinity) == (int)0x7f800000U);

            // Use "unchecked" syntax to avoid compiler error: "Constant value '4286578688' cannot be
            // converted to a 'int' (use 'unchecked' syntax to override). The literal 0xff800000
            // (4286578688)won't fit in a signed int without overflow.
            // C# statements can execute in either checked or unchecked context. In a checked context,
            // arithmetic overflow raises an exception. In an unchecked context, arithmetic overflow
            // is ignored and the result is truncated.
            unchecked
            {
                Assert.IsTrue(BitConverterUtil.SingleToInt32Bits(float.NegativeInfinity) == (int)0xff800000U);
            }
        }