Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# / IronPython Interop and the "float" data type

Working on a project that uses some IronPython scripts to as plug-ins, that utilize functionality coded in C#. In one of my C# classes, I have a property that is of type:

Dictionary<int, float>

I set the value of that property from the IronPython code, like this:

mc = MyClass()
mc.ValueDictionary = Dictionary[int, float]({1:0.0, 2:0.012, 3:0.024})

However, when this bit of code is run, it throws the following exception:

Microsoft.Scripting.ArgumentTypeException was unhandled by user code
  Message=expected Dictionary[int, Single], got Dictionary[int, float]

To make things weirder, originally the C# code used

Dictionary<int, double>

but I could not find a "double" type in IronPython, tried "float" on a whim and it worked fine, giving no errors. But now that it's using float on both ends (which it should have been using from the start) it errors, and thinks that the C# code is using the "Single" data type?!

I've even checked in the object browser for the C# library and, sure enough, it shows as using a "float" type and not "Single"

like image 538
Adam Haile Avatar asked Jun 09 '10 13:06

Adam Haile


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

I dont really see a question here, you very much answered everything yourself. I guess you are just asking because you are confused. So lets clear things up:

C# has the two floating point types: float (a keyword that is short for System.Single MSDN, 32 bit long) and double (keyword for System.Double MSDN, 64 bit long).

Python on the other side uses the float keyword/type to store a double precision floating point number, as the python documentation states:

Floating point numbers are implemented using double in C. All bets on their precision are off unless you happen to know the machine you are working with.

For that, on a x86 architecture, it is 64 bit long. This is the reason IronPython treats a python float as a .NET System.Double.

That said, those two methods will both work:

C#:

Dictionary<int, float> single_precision_dict;
Dictionary<int, double> double_precision_dict;

IronPython:

single_precision_dict = Dictionary[int, Single]({1:0.0, 2:0.012, 3:0.024})
double_precision_dict = Dictionary[int, float]({1:0.0, 2:0.012, 3:0.024})
like image 136
Philip Daubmeier Avatar answered Sep 20 '22 16:09

Philip Daubmeier