division

Erik Max Francis max at alcyone.com
Sun Jun 22 23:25:34 EDT 2003


Ben Finney wrote:

> I think your program design needs re-thinking.  If I typed "4 / 3"
> into
> an arithmetic evaluator, and was told that was an error, I'd certainly
> consider it a design bug in the program.
> 
> Can you explain more fully what it is you want to do?  Why do you
> consider a fractional result to be an error?

It sounds like he simply wants to have the functionality of a calculator
that uses floating division instead of integer division.  (His desire to
make integer division an error seems like it's only arising because he
doesn't know how to transform it into float division.)

What he probably wants is new float division semantics, which he can get
by passing in a -Q new argument to the python interpreter, or by
starting his script with from __future__ import division:

max at oxygen:~% python -Q new
Python 2.2.3 (#1, May 31 2003, 21:31:33) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
0.5
^D
max at oxygen:~% python
Python 2.2.3 (#1, May 31 2003, 21:31:33) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import division
>>> 1/2
0.5

If he really _does_ want a warning for old division, he can use -Q warn
or -Q warnall:

max at oxygen:~% python -Q warnall
Python 2.2.3 (#1, May 31 2003, 21:31:33) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
__main__:1: DeprecationWarning: classic int division
0


-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ The most exhausting thing in life is being insincere.
\__/  Anne Morrow Lindbergh




More information about the Python-list mailing list