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


Groups > comp.lang.python > #51373

Re: Division and multiplication have a different behavior in the overflow case

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'float': 0.07; 'result,': 0.07; 'skip:` 10': 0.07; 'converted': 0.09; 'operand': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'jan': 0.12; '1:48': 0.16; 'exception:': 0.16; 'marco': 0.16; 'operands': 0.16; 'range.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject:case': 0.16; 'subject:skip:m 10': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'least': 0.26; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'converting': 0.30; '"",': 0.31; 'division': 0.31; 'file': 0.32; '(most': 0.33; 'subject:the': 0.34; 'possible.': 0.35; 'convert': 0.35; 'but': 0.35; 'doing': 0.36; 'should': 0.36; 'example,': 0.37; 'too': 0.37; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'expect': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'conversion': 0.61; 'received:173': 0.61; 'first': 0.61; 'different': 0.65; 'answer.': 0.68; 'behavior': 0.77; 'subject:have': 0.80; 'float,': 0.84; 'irrelevant': 0.84; 'limits,': 0.84; 'received:fios.verizon.net': 0.84; 'thing,': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Division and multiplication have a different behavior in the overflow case
Date Sat, 27 Jul 2013 16:56:48 -0400
References <kt115f$8ec$1@speranza.aioe.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7
In-Reply-To <kt115f$8ec$1@speranza.aioe.org>
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5185.1374958628.3114.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1374958628 news.xs4all.nl 15944 [2001:888:2000:d::a6]:58987
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51373

Show key headers only | View raw


On 7/27/2013 1:48 PM, Marco wrote:
> In Python 3, when we hava a division and both the result and at least
> one operand are too large to convert to float, we get an exception:
>
>      >>> 2**1028 / 2**-2

int/float gets converted to float/float and the int to float conversion 
is not possible. The result is irrelevant since it never gets calculated.

>      Traceback (most recent call last):
>        File "<input>", line 1, in <module>
>      OverflowError: long int too large to convert to float
>
> When the result is inside the limits, we get the right value:
>
>      >>> 2 ** 1025 / 2**10
>      3.511119404027961e+305

This must be doing integer division and then converting the result to 
float, which it can.

> Why the behavior is different in the case of the multiplication?
>
>      >>> 2 ** 1025 * 2**-10

Mathematically this is the same thing, but computationally, it is not 
and you cannot expect to get the same answer. Like the first example, 
2**-10 is a float, so 2**1025 must be converted to float to do float 
multiplication. But that is not possible.

>      Traceback (most recent call last):
>        File "<input>", line 1, in <module>
>      OverflowError: long int too large to convert to float
>
> I think the multiplication should have the same behavior than the
> division:
>
> * `inf` or `-inf` when the operands are inside the limits,
>    but the result is not
> * `OverflowError`  when the result, and at least one operand,
>    are out of range.

-- 
Terry Jan Reedy

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


Thread

Division and multiplication have a different behavior in the overflow case Marco <m.b@gmail.com> - 2013-07-27 19:48 +0200
  Re: Division and multiplication have a different behavior in the overflow case Terry Reedy <tjreedy@udel.edu> - 2013-07-27 16:56 -0400

csiph-web