IronPython/C# Float data comparison

Posted by Gopalakrishnan Subramani on Stack Overflow See other posts from Stack Overflow or by Gopalakrishnan Subramani
Published on 2010-12-30T04:44:07Z Indexed on 2010/12/30 4:54 UTC
Read the original article Hit count: 352

Filed under:
|
|
|
|

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

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 statement raises assert error

self.assertEqual(myClassInstance.Value, 12.345)

This 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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about python