0

The code

eval("7/2")

yields the result 3. I would prefer that it returned 3.5. How can I achieve this please?

Update: I have now realised that the eval() is a 'red herring' and this is a duplicate of How can I force division to be floating point? Division keeps rounding down to 0 and probably about a hundred other questions.

Community
  • 1
  • 1
Caltor
  • 2,538
  • 1
  • 27
  • 55
  • 1
    Why are you using `eval`? – Wooble Sep 28 '13 at 00:27
  • @Wooble to evaluate a postfix expression after converting it back to infix notation. If you can point me to a Python algorithm for evaluating postfix notation I could probably get rid of it. What is wrong with eval? It appears from the answers received that the eval is a red herring in this problem anyway and the integer result is the natural effect of dividing two integers in Python 2. – Caltor Sep 28 '13 at 00:37
  • Postfix arithmetic is really simple to evaluate with a stack, assuming you can tokenize the values and operators first. Go through the sequence of tokens one by one. If a token is a value, push it onto the stack. If a token is an operator, pop the appropriate number of values (e.g. 2, for a binary operator) off the stack and run the operator on them, then push the result back on. Wikipedia [describes the algorithm](http://en.wikipedia.org/wiki/Reverse_Polish_notation#Postfix_algorithm) pretty well. – Blckknght Sep 28 '13 at 01:27
  • @Blckknght the input is helpfully already tokenized. I've just started writing my own variation of http://stackoverflow.com/a/3866502/470014 which seems to be a pretty good algorithm. – Caltor Sep 28 '13 at 02:29

4 Answers4

2

In Python 2, the result of dividing two integers is an integer:

In [1]: 7/2
Out[1]: 3

If you introduce a floating point number, the result is a floating-point number:

In [2]: 7/2.0
Out[2]: 3.5

In Python 3, dividing integers yields floats, see PEP 238.

You can achieve the same behaviour in Python 2 like this:

In [3]: from __future__ import division

In [4]: 7/2
Out[4]: 3.5

And to perform integer division:

In [5]: 7//2
Out[5]: 3
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • I assumed it was a side-effect of the eval() but I can see what's going on now, thanks. Looks like a good reason for me to upgrade to Python 3 otherwise it's going to be difficult and a bit messy to code what I am trying to achieve. – Caltor Sep 28 '13 at 00:32
  • Is there a way I can force operator.div to return a float as well? – Caltor Sep 30 '13 at 15:13
  • @Caltor: I don't believe so, but you could use [`operator.truediv()`](http://docs.python.org/2/library/operator.html#operator.truediv) instead. – johnsyweb Sep 30 '13 at 23:35
1

Python is dynamically typed, and you're passing it integers implicitly by leaving off the decimal.

Just use either 7.0/2 or 7/2.0.

This will let python know that you're using floating points.

Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39
1

Python 2.7 divides based on the types of input variables. To get the desired result, you either:

  • "Transform" one of the variables into float-point 7/2.0 or 7.0/2; or
  • Import the division feature from the future implementation package:

    In[1]: from __future__ import division

Then do the division like you're doing:

In[2]: 7/2
Out[2]: 3.5
SonicARG
  • 499
  • 9
  • 14
0
python -c 'print eval("float(7)/float(2)")'
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
Buzz Moschetti
  • 7,057
  • 3
  • 23
  • 33
  • 1
    I appreciate the implication to use the float() function here. Not sure why someone has downvoted it. Maybe the answer is a bit terse for their liking. – Caltor Sep 28 '13 at 00:46