Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 917
Max Avatar asked Jun 26 '10 19:06

Max


People also ask

How do you force floating point division?

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.

Why are floating point calculations so inaccurate?

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.

How do you calculate floating point operations?

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.


1 Answers

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)
like image 115
pts Avatar answered Oct 11 '22 15:10

pts