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. :)

20 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
  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. 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
  7. Your website is really cool and this is a great inspiring article.
    data science course in malaysia

    ReplyDelete
  8. It was a very good post indeed. I thoroughly enjoyed reading it in my lunch-time. I Will surely come and visit this blog more often. Feel free to visit my website; 온라인카지노사이트

    ReplyDelete
  9. Nice blog! Thanks for sharing such a great blog. whatsapp mod

    ReplyDelete
  10. IT is a informative and learnable blog site .Thank you so much for sharing this post.
    Best Software Company in Bangladesh

    ReplyDelete
  11. Your blog site is really one of the blog sites I have visited. I hope you get some better updates. Thank you so much for sharing this post with us. Thank you.dehumidifier price in Bangladesh

    ReplyDelete
  12. Your blog site is very informative and learnable site . Thank you so much for sharing this post with us .

    ReplyDelete
  13. Nice post love it check my site for fast Satta King we provide superfast and all time result SattaKing

    ReplyDelete

  14. Thank you very much for publishing this of article, I like your article very much. Your post is very informative and helpful. web development company in Bangladesh

    ReplyDelete
  15. I just found your post by searching on google, I'm fascinated and learned a lot of new things from your post. I try to learn new and always new skills in blogging because I believe that blogging is a full-time job to learn new things day in and day out. Thank you so much for sharing this post with us web development company in Bangladesh


    ReplyDelete
  16. The term Internet Service Provider (ISP) refers to a company that provides Internet access to both personal and business customers. ISPs make it possible for their clients to surf the web, shop online, runs a business, and connect with family and friends - in exchange for everything. ISPs can also provide other services including email services, domain registration, web hosting and browser packages. Our blog site is related to Internet Service Provider (ISP). I believe that my blog site is more helpful than all other blog sites. We provide the best service to everyone. best isp in Dhaka

    ReplyDelete
  17. Thank you so much for sharing this great blog. Very inspiring and helpful too. Hope you continue to share more of your ideas.I will definitely love to read.
    토토사이트
    온라인경마

    ReplyDelete