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.
To divide float values in Python, use the / operator. The Division operator / takes two parameters and returns the float division. Float division produces a floating-point conjecture of the result of a division. If you are working with Python 3 and you need to perform a float division, then use the division operator.
Floating-point decimal values generally do not have an exact binary representation. This is a side effect of how the CPU represents floating point data. For this reason, you may experience some loss of precision, and some floating-point operations may produce unexpected results.
Then count all simple floating-point additions, multiplications, divisions, etc. For example, y = x * 2 * (y + z*w) is 4 floating-point operations. Multiply the resulting number by the number of iterations. The result will be the number of instructions you're searching for.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With