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?
#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'>]
>>>
Gribouillis and Skaperen like this post
Reply


Messages In This Thread
shouldn't this raise an exception? - by Skaperen - Dec-07-2021, 07:52 PM
RE: shouldn't this raise an exception? - by casevh - Dec-08-2021, 05:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  arg and keyword arg conflict: what to raise Skaperen 5 2,879 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,596 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