C# / IronPython Interop and the “float” data type

こ雲淡風輕ζ 提交于 2019-12-05 11:00:20

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