C#/ IronPython的互操作和"浮动"数据类型 [英] C# / IronPython Interop and the "float" data type

查看:211
本文介绍了C#/ IronPython的互操作和"浮动"数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用一些IronPython的脚本来作为插件,利用该功能,在C#编码的项目。在我的C#类之一,我有一个属性,它的类型是:

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>



我设置为从IronPython的代码属性的值,如:

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]

为了让事情变得怪异,原本使用的是C#代码

To make things weirder, originally the C# code used

Dictionary<int, double>



但我无法找到IronPython的一个双型,试图浮动一时兴起和它工作得很好,给人没有错误。但现在,它的使用两端它的错误(它应该已经从一开始就使用的)浮动,并认为C#代码使用的是单数据类型?!

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

我甚至在C#的图书馆,果然对象浏览器检查,它显示为使用浮动式,而不是单次

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"

推荐答案

我真的不看到一个问题在这里,你非常回答自己的一切。我猜你只是问,因为你感到困惑。所以,让我们清楚的事情:

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#有两个浮点类型:浮动(一个关键字,它是短期的 System.Single MSDN ,32位长)和双击(为 System.Double 的MSDN ,64位长)。

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的另一侧使用了浮动关键字/类型来存储一个双精度浮点数,为的 Python文档规定:

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

浮点数实现
在C采用双对他们的精密全盘皆输,除非你碰巧知道你正在使用的机器。

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.

有关的是,在x86架构,这是64位长。这是IronPython的对待蟒蛇的原因浮动作为.NET System.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.

这是说,这两种方法都将会工作:

That said, those two methods will both work:

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})

这篇关于C#/ IronPython的互操作和&QUOT;浮动&QUOT;数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆