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


Groups > comp.lang.python > #73417

Re: can I get 0./0. to return nan instead of exception?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'anyway.': 0.05; 'explicitly': 0.05; 'float': 0.07; 'try:': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'exception?': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nan': 0.16; 'numpy': 0.16; 'subject:exception': 0.16; 'types,': 0.16; 'flexibility': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'cc:addr:python.org': 0.22; 'error': 0.23; 'module,': 0.24; 'cc:2**0': 0.24; 'handling': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'that.': 0.31; 'division': 0.31; 'probably': 0.32; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'raising': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'should': 0.36; 'pm,': 0.38; 'expect': 0.39; 'subject:can': 0.39; 'system.': 0.39; 'either': 0.39; "you're": 0.61; 'more': 0.64; 'subject:. ': 0.67; 'behavior': 0.77; 'heavy': 0.81; 'subject:get': 0.81; 'joel': 0.91; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=LUAREXMo5S9Y8SO/tJjj6knbaZ/5GccCpqSpFjYZIOg=; b=XJP/uCzXx8rFWg/OiBDFtcMfPkvz6fGCrTj71HfkrmQJRhSudkAQ/u66j7COt8y5/N mr0GRA4BZAVFz+6rQPrrTTDLp4LeAa1hu/H7dGUU3MoCchlzq+gKWsylsyEVskADUYfc N6uBF0XNT5J9K0w1cVkHpA/K6qFb8WndnJiqJjMVxgGrIzdI413azJtazTg3f5emaMcb Yr5b3G1EZuc4rekQ2pVp542NCzoGmSmez8XrIpNBk4EoAoSOX9h2fkIgHwyWTmPLPDLY pdrWwt9dIqZH4Z65pvi92q8P+0RoZWCf2BFQqcUrLnk4zTt5uOa0tJph2cy/qUhD5sFy H7sQ==
MIME-Version 1.0
X-Received by 10.220.166.9 with SMTP id k9mr3610863vcy.20.1403178449021; Thu, 19 Jun 2014 04:47:29 -0700 (PDT)
In-Reply-To <CAPM-O+zuBfLBE=tVhb=fYC+yr3W+a4-wWauqQ4xkH3xZSenfVw@mail.gmail.com>
References <lnug3b$k2v$1@ger.gmane.org> <CAPM-O+zuBfLBE=tVhb=fYC+yr3W+a4-wWauqQ4xkH3xZSenfVw@mail.gmail.com>
Date Thu, 19 Jun 2014 21:47:28 +1000
Subject Re: can I get 0./0. to return nan instead of exception?
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11145.1403178935.18130.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1403178935 news.xs4all.nl 2951 [2001:888:2000:d::a6]:60823
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73417

Show key headers only | View raw


On Thu, Jun 19, 2014 at 9:31 PM, Joel Goldstick
<joel.goldstick@gmail.com> wrote:
> On Jun 19, 2014 7:05 AM, "Neal Becker" <ndbecker2@gmail.com> wrote:
>>
>> Can I change behavior of py3 to return nan for 0./0. instead of raising an
>> exception?
>
> There is no nan in python.

Yes, there is, but it's not normal to get it as a division result like that.

One way is to explicitly try/except:

try:
    result = operand_1 / operand_2
except ZeroDivisionError:
    result = float("nan")

You may also be able to use the fpectl module, if it's available on
your system. Alternatively, use either decimal.Decimal or one of the
numpy types, both of which give you more flexibility in error handling
than the inbuilt float type gives. If you're doing heavy computational
work in Python and expect exact IEEE floating point semantics, you
should probably be using numpy anyway.

ChrisA

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


Thread

Re: can I get 0./0. to return nan instead of exception? Chris Angelico <rosuav@gmail.com> - 2014-06-19 21:47 +1000

csiph-web