Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2985

Re: Feature suggestion: math.zod for painlessly avoiding ZeroDivisionError

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Feature suggestion: math.zod for painlessly avoiding ZeroDivisionError
Date 2011-04-11 13:27 -0400
References <BANLkTi=k+O2pPPC0ThMoPEtAmkJooroOOQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.220.1302542864.9059.python-list@python.org> (permalink)

Show all headers | View raw


On 4/11/2011 10:10 AM, Natan Yellin wrote:
> Hey everyone,
> This is my first posting to python-list, so be gentle.
>
> I propose the following function for the math module (which can, of
> course, be rewritten in C):
>
>       zod = lambda a, b: b and a / b

This is way too trivial to add.
>
> zod, the zero or divide function, is useful for division where the
> denominator can be 0. For example, here's one line of code
>     stat = x / y
> If y can be zero, that one-liner needs to be rewritten as:
>     if y != 0:
>          stat = x / y
>     else:
>          stat = 0

No. 'stat = y and x/y', which will be faster than 'stat = zod(x,y)', 
which does the same thing but adds a function call/

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | Next | Find similar


Thread

Re: Feature suggestion: math.zod for painlessly avoiding ZeroDivisionError Terry Reedy <tjreedy@udel.edu> - 2011-04-11 13:27 -0400

csiph-web