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


Groups > comp.lang.python > #73417 > unrolled thread

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

Started byChris Angelico <rosuav@gmail.com>
First post2014-06-19 21:47 +1000
Last post2014-06-19 21:47 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

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

FromChris Angelico <rosuav@gmail.com>
Date2014-06-19 21:47 +1000
SubjectRe: can I get 0./0. to return nan instead of exception?
Message-ID<mailman.11145.1403178935.18130.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web