Python Forum
shouldn't this raise an exception?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
shouldn't this raise an exception?
#1
shouldn't this raise some kind of exception, like a Decimal.UNDERFLOW or something like that, new?
lt2a/phil /home/phil 10> python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import *
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
>>> Decimal('1E999999')*Decimal('1E99')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
decimal.Overflow: [<class 'decimal.Overflow'>]
>>> Decimal('1E-999999')*Decimal('1E-99')
Decimal('0E-1000026')
>>> Decimal('1E-1000026')
Decimal('1E-1000026')
>>> 
it does not appear to be a representation issue, so i assume it is an operation issue.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
>>> from decimal import *
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
>>> Decimal('1E-999999')*Decimal('1E-99')
Decimal('0E-1000026')
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[Clamped, Inexact, Rounded, Subnormal, Underflow], traps=[InvalidOperation, DivisionByZero, Overflow])
>>> 
Compare the differences in the output of getcontext() before and after operation that generates the underflow. flags now has a list of the special conditions. The Underflow flag was set. traps is a list of the flags, that when set, raise an exception. You can add Underflow to the traps if you want to raise an exception.

>>> setcontext(DefaultContext)
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
>>> getcontext().traps[Underflow] = True
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow, Underflow])
>>> Decimal('1E-999999')*Decimal('1E-99')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
decimal.Underflow: [<class 'decimal.Underflow'>]
>>>
Skaperen and Gribouillis like this post
Reply
#3
from decimal import localcontext, Decimal


with localcontext() as ctx:
    ctx.Emin=-999999999999999999
    ctx.Emax=+999999999999999999

    print(Decimal('1E-999999')*Decimal('1E-99'))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
i did not know about the traps. i guess there really are cases where you need to just continue after an underflow. having a way to suppress exceptions in a try/except would have been an alternate way. not all exceptions would be safe for that.

what raised my concern was the funny value that had a 0 mantissa. i remember ages ago having an HP-35 calculator where i found a way to get an invalid value like that and dividing a number by that would hard freeze the calculator (a divide by zero it failed to catch).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  arg and keyword arg conflict: what to raise Skaperen 5 2,693 Jun-22-2020, 12:43 AM
Last Post: Skaperen
  Python 3.701b released yesterday--who should install it and who shouldn't? league55 7 4,336 Feb-02-2018, 08:11 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020