Thursday, September 3, 2020

Not counting zeros

We all have our favorite way of intentionally raising an exception in Python. Some like referencing an undefined variable to get a simple NameError, others might import a module that doesn't exist for a bold ImportError.

But the tasteful exceptioneer knows to reach for that classic computer-confounding conundrum: 1/0 for a satisfyingly descriptive DivisionByZero.

So, when does dividing by 0 not raise DivisionByZero?

Why, when you divide 0 by a Decimal(0), of course!

>>> from decimal import Decimal
>>> Decimal(0) / Decimal(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.DivisionUndefined'>]
>>> Decimal(1) / Decimal(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
decimal.DivisionByZero: [<class 'decimal.DivisionByZero'>]

The numerator type doesn't seem to matter either:

>>> 0 / Decimal(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.DivisionUndefined'>]

"InvalidOperation" just doesn't quite have the same ring to it! Well, they can't all be heroes. :)

28 comments:

  1. You get an undefined error because decimal cannot handle NaNs.
    If you do 1 / decimal.Decimal(0) you will get a division by zero error.

    ReplyDelete
    Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete
  2. Actually, I'm wrong, the decimal package can handle both infinity and NaN.

    a = decimal.Decimal(float('Inf'))
    b = decimal.Decimal(float('NaN'))

    The operations are just inconsistent, as seems to be the standard in Python…

    https://wiesmann.codiferes.net/wordpress/?p=13366

    ReplyDelete

  3. It is very ideal to see the best subtleties introduced in a simple and getting way. 360DigiTMG project management training

    ReplyDelete
  4. I think I have never watched such online diaries ever that has absolute things with all nuances which I need. So thoughtfully update this ever for us.
    360DigiTMG big data analytics courses

    ReplyDelete
  5. He gathers all the necessary facts and information before finding a solution to a problem.
    https://360digitmg.com/course/certification-on-robotic-process-automation-rpa

    ReplyDelete
  6. I at long last discovered extraordinary post here.I will get back here. I just added your blog to my bookmark destinations. thanks.Quality presents is the significant on welcome the guests to visit the website page, that is the thing that this page is giving.
    data scientist course

    ReplyDelete
  7. Thanks for posting the best information and the blog is very informative.Data science course in Faridabad

    ReplyDelete
  8. Very awesome!!! When I searched for this I found this website at the top of all blogs in search engines.

    Best Institutes For Digital Marketing in Hyderabad

    ReplyDelete
  9. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one
    Best Data Science courses in Hyderabad

    ReplyDelete
  10. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
    data analytics courses in bangalore

    ReplyDelete
  11. Thanks for posting the best information and the blog is very helpful.digital marketing institute in hyderabad

    ReplyDelete
  12. your blog everyday and try to learn something from your blog. Thank you and I'm waiting for your new post.
    best data science institute in hyderabad

    ReplyDelete
  13. This website and I conceive this internet site is really informative ! Keep on putting up!
    data science course in pune

    ReplyDelete
  14. Thanks for posting the best information and the blog is very important.data science institutes in hyderabad

    ReplyDelete
  15. I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
    Data Science Course Syllabus

    ReplyDelete
  16. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.

    DevOps Training in Hyderabad

    ReplyDelete
  17. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
    artificial intelligence training in chennai

    ReplyDelete
  18. Tremendous blog quite easy to grasp the subject since the content is very simple to understand. Obviously, this helps the participants to engage themselves in to the subject without much difficulty. Hope you further educate the readers in the same manner and keep sharing the content as always you do.

    data science course in faridabad

    ReplyDelete
  19. Your website is really cool and this is a great inspiring article.
    data science course in malaysia

    ReplyDelete
  20. Thanks for posting the best information and the blog is very good.data science course in Lucknow

    ReplyDelete