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


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

Division and multiplication have a different behavior in the overflow case

Started byMarco <m.b@gmail.com>
First post2013-07-27 19:48 +0200
Last post2013-07-27 16:56 -0400
Articles 2 — 2 participants

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


Contents

  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

#51338 — Division and multiplication have a different behavior in the overflow case

FromMarco <m.b@gmail.com>
Date2013-07-27 19:48 +0200
SubjectDivision and multiplication have a different behavior in the overflow case
Message-ID<kt115f$8ec$1@speranza.aioe.org>
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
     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

Why the behavior is different in the case of the multiplication?

     >>> 2 ** 1025 * 2**-10
     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.
-- 
Marco Buttu

[toc] | [next] | [standalone]


#51373

FromTerry Reedy <tjreedy@udel.edu>
Date2013-07-27 16:56 -0400
Message-ID<mailman.5185.1374958628.3114.python-list@python.org>
In reply to#51338
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

[toc] | [prev] | [standalone]


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


csiph-web