简体   繁体   English

强制进行浮点计算

[英]Forcing to make floating point calculations

In IronPython is there any way to force the expression containing integer values to be calculated as floating point. 在IronPython中有任何方法可以强制将包含整数值的表达式计算为浮点数。 For instance, I'd like the expression 例如,我喜欢这个表达方式

1/3

to be evaluated as 被评估为

1./3. 

with the result 0.333... 结果为0.333 ...

I need this to make a simple run-time expression calculator within a C# project by means of IronPython. 我需要这个通过IronPython在C#项目中创建一个简单的运行时表达式计算器。 I cannot force users to input expression with trailing decimal points. 我无法强制用户输入带有尾随小数点的表达式。

from __future__ import division

print 1 / 3
print 1 // 3

You may force a floating point division like any of these, no matter if anything is imported from __future__ : 无论是否从__future__导入任何内容,您都可以强制执行浮点除法:

print val1 / (val2 + 0.0)
print (val1 + 0.0) / val2
print float(val1) / val2
print val1 / float(val2)

If your users are entering values anyway, then presumably you are converting the values to int s. 如果您的用户仍在输入值,那么您可能正在将值转换为int So simply convert them to float instead. 所以简单地将它们转换为float

val1 = float(raw_input())
val2 = float(raw_input())
print val1/val2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM