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:
使用一些IronPython脚本作为插件的项目,使用C#编码的功能。在我的一个C#类中,我有一个类型为的属性:
Dictionary<int, float>
I set the value of that property from the IronPython code, like this:
我从IronPython代码中设置了该属性的值,如下所示:
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
为了使事情变得更奇怪,最初使用的是C#代码
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?!
但是我在IronPython中找不到“双”类型,一想到就“浮动”并且工作正常,没有错误。但是现在它在两端使用float(应该从一开始就使用它)它会出错,并且认为C#代码使用的是“Single”数据类型?!
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"
我甚至在对象浏览器中检查了C#库,当然,它显示为使用“float”类型而不是“Single”
1 个解决方案
#1
7
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).
C#有两种浮点类型:float(System.Single MSDN的缩写,32位长的关键字)和double(System.Double MSDN的关键字,64位长)。
Python on the other side uses the float
keyword/type to store a double precision floating point number, as the python documentation states:
另一方面,Python使用float关键字/类型来存储双精度浮点数,如python文档所述:
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.
浮点数在C中使用double实现。除非您碰巧知道正在使用的机器,否则所有关于其精度的投注都将关闭。
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
.
为此,在x86架构上,它是64位长。这就是IronPython将python float视为.NET System.Double的原因。
That said, those two methods will both work:
也就是说,这两种方法都有效:
C#:
C#:
Dictionary<int, float> single_precision_dict;
Dictionary<int, double> double_precision_dict;
IronPython:
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})
#1
7
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).
C#有两种浮点类型:float(System.Single MSDN的缩写,32位长的关键字)和double(System.Double MSDN的关键字,64位长)。
Python on the other side uses the float
keyword/type to store a double precision floating point number, as the python documentation states:
另一方面,Python使用float关键字/类型来存储双精度浮点数,如python文档所述:
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.
浮点数在C中使用double实现。除非您碰巧知道正在使用的机器,否则所有关于其精度的投注都将关闭。
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
.
为此,在x86架构上,它是64位长。这就是IronPython将python float视为.NET System.Double的原因。
That said, those two methods will both work:
也就是说,这两种方法都有效:
C#:
C#:
Dictionary<int, float> single_precision_dict;
Dictionary<int, double> double_precision_dict;
IronPython:
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})