Thursday, September 12, 2019

Welcome to the float zone...

Consider a REPL with two tuples, a and b.

>>> type(a), type(b)
(<type 'tuple'>, <type 'tuple'>)
>>> a == b
True


So far, so good.  But let's dig deeper...

>>> a[0] == b[0]
False


The tuples are equal, but their contents is not.



>>> a is b
True





In fact, there was only ever one tuple.
What is this madness?

>>> a
(nan,)


Welcome to the float zone.

Many parts of python assume that a is b implies a == b, but floats break this assumption.  They also break the assumption that hash(a) == hash(b) implies a == b.

>>> hash(float('nan')) == hash(float('nan'))
True


Dicts handle this pretty elegantly:

>>> n = float('nan')
>>> {n: 1}[n]
1


>>> a = {float('nan'): 1, float('nan'): 2}
>>> a
{nan: 1, nan: 2}

28 comments:

  1. https://csnip2020.blogspot.com/2020/07/python-installation-steps.html?showComment=1636616273329#c834529903571727403

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I am not a programmer, but I know that it is helpful for beginners. So, I must say that this is a nice guide for computer programmers. They can learn from such blogs and enhance their programming skills. Same as students can enhance their academic writing skills by getting in touch with cheap dissertation writing services.

    ReplyDelete
  4. that is the finish of this article. Here you will locate some web pages that we believe you’ll enjoy, just click the links ove

    온라인카지노사이트
    온라인카지노
    카지노사이트

    ReplyDelete
  5. Howdy just wanted to give you a quick heads up.
    The text in your post seem to be running off
    the screen in Opera. I’m not sure if this is a format issue or something to do
    with browser compatibility but I thought I’d post
    to let you know. The design look great though! Hope you get the problem
    fixed soon. Cheers


    카지노사이트
    안전카지노사이트
    카지노사이트홈

    ReplyDelete
  6. Pretty! This was an extremely wonderful article. Thanks for
    supplying this info.



    스포츠토토
    메이저사이트 목록
    먹튀검증

    ReplyDelete
  7. Awesome write-up. I am a regular visitor of your site and appreciate you taking the time to maintain the excelent site.i will be a frequent visitor for really long time


    스포츠토토티비
    스포츠중계
    토토사이트


    ReplyDelete
  8. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! ยูฟ่า168

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

    ReplyDelete
  10. İnsan böyle şeyler görünce mutlu oluyor

    ReplyDelete
  11. I have been checking out a few of your stories and i can state pretty good stuff. I will definitely bookmark your blog. demolition company

    ReplyDelete
  12. Hi there colleagues, its fantastic piece of writing concerning tutoringand fully defined, keep it up all the time.
    바카라
    토토사이트

    ReplyDelete
  13. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website.
    majortotositepro2
    racesitepro2
    oncasinositenet2
    totopickpro2

    ReplyDelete
  14. Python is a general-purpose language, meaning it can be used to create a variety of different programs and isn't specialized for any specific problems. Check also about https://www.toptreeserviceharrisonburg.com/

    ReplyDelete
  15. If you are looking for the best basement waterproofing Get additional info here.

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. The blog was useful in knowing about book life.Excellent blog thanks for sharing the valuable information..it becomes easy to read and easily understand the information.Create you own project from usmatlab mini project centers in chennai

    ReplyDelete
  18. Are you looking for the best concrete contractor then check out at https://www.concreteharrisonburg.com/.

    ReplyDelete
  19. Python is an object-oriented programming language that is interpreted and interactive. Learn more about Spokane Kitchen Remodel

    ReplyDelete
  20. You have a distinguished talent in explaining things. Thanks for sharing this here. best churches in shawnee

    ReplyDelete
  21. I'm searching for a great deal of information on this theme. The article I've been searching for meanwhile is the ideal article. If it's not too much trouble, visit my site for additional total articles Mesothelioma Class Action

    ReplyDelete