IronPython/C# Float data comparison

谁都会走 提交于 2019-12-10 23:08:45

问题


We have WPF based application using Prism Framework. We have embedded IronPython and using Python Unit Test framework to automate our application GUI testing.

It works very well. We have difficulties in comparing two float numbers.

Example C#

class MyClass
{
   public object Value { get; set;}
   public MyClass()
   {
        Value = (float) 12.345;
   } 
}

In IronPython When I compare the MyClass Instance's Value property with python float value(12.345), it says it doesn't equal

This Python statement raises assert error

self.assertEqual(myClassInstance.Value, 12.345)

This Python statement works fine.

self.assertEqual(float(myClassInstance.Value.ToString()), 12.345) 

When I check the type of the type(myClassInstance.Value), it returns Single in Python where as type(12.345) returns float. How to handle the C# float to Python comparison without explicit conversions?


回答1:


I'm not familiar with Python but I'm taking a stab at an answer based on what I know about the CLR and C#.

In your example, the float value is "boxed" when it's assigned to the Value property. Boxing is a way of storing a value type (such as a float, int, etc) in an object-typed variable. If the assert method takes two object parameters, it might be doing reference equality in which case the two would not be "equal". You would need to "unbox" the Value property to get a value comparison. In C# this is done by simply casting the object-typed variable back to its original type.

To demonstrate, see the following code. Note that it prints False for the first and True for the second.

void Main()
{
    object value1 = 1234.5F;
    object value2 = 1234.5F;
    Console.WriteLine(AreEqual(value1, value2));
    Console.WriteLine(AreEqual((float)value1, (float)value2));
}

bool AreEqual(object value1, object value2) {
    return value1 == value2;
}

bool AreEqual(float value1, float value2) {
    return value1 == value2;
}

However, I have to agree with Ignacio that you should never compare two floating point numbers for equality. You should always use a method that includes a tolerance since floating point operations can sometimes result in tiny differences.




回答2:


12.345 in C# is a double, unless you explicitly use 12.345f




回答3:


IronPython float is actually a .NET double. Besides, you shouldn't be comparing floating point values for equality anyway.



来源:https://stackoverflow.com/questions/4560066/ironpython-c-float-data-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!