简体   繁体   中英

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. For instance, I'd like the expression

1/3

to be evaluated as

1./3. 

with the result 0.333...

I need this to make a simple run-time expression calculator within a C# project by means of IronPython. 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__ :

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. So simply convert them to float instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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