Smallest And Largest Floating Point Values In Python

In my production code, I have a function that calculates a/(a+b). That function threw a ZeroDivisionError at me this afternoon, so I decided to change the equation to a/(a+b+sys.minfloat), because I felt that was more elegant than writing a conditional function to check if (a + b) == 0.

Turns out there is no 'minfloat' in the sys module, so I decided to write a function to calculate the smallest float myself.
>>> def minfloat(guess):
while(guess * 0.5 != 0):
guess = guess * 0.5
return guess

>>> minfloat(+1.0) # minimum positive value of a float
4.9406564584124654e-324

>>> minfloat(-1.0) # minimum negative value of a float
-4.9406564584124654e-324


But I couldn't stop there. Now I had to write a function to calculate the largest possible floating point value in Python, just for kicks:
>>> def maxfloat(guess = 1.0):
while(guess * 2 != guess):
guess = guess * 2
return guess

>>> maxfloat(+1.0) # maximum positive value of a float
inf

>>> maxfloat(-1.0) # maximum negative value of a float
-inf


This is interesting. Let's find out more about this "inf" value:
>>> inf

Traceback (most recent call last):
File "", line 1, in
inf
NameError: name 'inf' is not defined
>>> float("inf")
inf

>>> inf = maxfloat()

>>> inf + inf
inf

>>> inf - inf
nan

>>> 1 / inf
0.0

>>> 1/(-inf)
-0.0


Finally, check the relationship between minfloat and maxfloat:
>>> 1 / minfloat(1.0)
inf
>>> 1 / minfloat(-1.0)
-inf

35 comments :: Smallest And Largest Floating Point Values In Python

  1. You can actually get the inf float by doing float('inf').

  2. Hey, I saw a post of yours on stackoverflow.com about simply adding RAM to a server to compensate for poor coding of PHP. Is this true? Can you send me an email at whadu@brokencontrollers.com?

    Thanks in advance for a reply.

  3. a/(a+b+minfloat) isn't a complete fix. If a+b==-minfloat, then you still get a divide by zero. For this to be true, you need a and b to be quite small otherwise the smallest amount they can differ by is greater than minfloat (whereas b can always equal -a, whatever the size of a)

  4. It is really nice for me to see you and your great hardwork again.Every piece of your work look excellent.Looking forward to learing more from you!
    Web Apps

  5. funny my own minfloat kept returning 0.5 and -0.5 respectively. any help as to why? i'm a python programmer.

  6. Congratulations reactivities ,, highly awaited new information from this site
    Good luck !!

    obat menyembuhkan stroke ringan
    tanaman obat tradisional kanker hati
    obat nyeri otot tradisional

  7. waiting for lho..semoga what is given can be useful
    .success always everything

    obat ramuan kanker hati
    ramuan obat herbal hipertiroid
    obat glaukoma herbal

  8. I enjoyed over read your blog post. Your blog have nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again
    bloxorz

  9. Your site is very helpful we are very waiting for the latest information
    Good luck to all of us

    obat tukak lambung herbal
    obat varikokel tradisional
    obat tradisional mastitis

  10. thank you very useful information admin, and pardon me permission to share articles here may help :

    Obat infeksi paru

    paru

    Cara menyembuhkan

    abses payudara

    Cara menyembuhkan ginjal bengkak


  11. "The collaboration with replica rolex the Anthony St. Ai Xiu Bai Youth Foundation provides inspiration for several of our special edition pilots Omega replica watches," said Georges Kern, Chief Executive Officer of replica watches. "Since the 70th anniversary of the celebration of the 70th anniversary of the publication of the" Little Prince "two years ago

  12. Bro I tried learning this but i don't seem to understand a thing can you shed more light on it.

    You can check my blog Naijcrackgist

  13. Bro, only programmers will understand

  14. The information from this site is very helpful so it is in waiting for other latest information

    buah infeksi lambung
    gejala radang paru paru
    bahaya hepatitis ibu hamil
    obat ginjal bengkak

  15. may be useful for all, helpful article once and pardon me permission to share also here

    Nurhasanah herbal
    Pengobatan herbal jelly gamat
    Obat kista tradisional

  16. Sites like these I'm looking for
    Thanks for the information, in tunggua keep the latest news

    cara sembuhkan gagal ginjal
    obat kurang darah tradisional manjur
    obat haid tidak teratur manjur

  17. Very helpful advice in this particular post! It’s the little changes that make the largest changes. Thanks for sharing!

    potaup

  18. Wow! I love this!!!!!!!!
    Are you having issues in your relationship?

    Are you looking for better ways of improving you lifestyle?

    Are you in love?

    Visit Mfon Elisha's Blog for enthusiastic articles on how to build a healthy and long lasting relationship.


  19. Your site is the best
    The latest information we are waiting for lho..semoga what is given can be useful
    Terimakash..success always everything.

    ramuan obat hipertiroid herbal
    obat alami kanker payudara aman
    ramuan keputihan tradisional

  20. Good luck ,, in waiting for other information from your site
    send regards for success

    obat batu empedu paling manjur
    cara mengobati gangguan irama jantung

  21. I was very impressed by this post, this site has always been pleasant news. Thank you very much for such an interesting post. Keep working, great job! In my free time, I like play game: imgtaram. What about you?

  22. Online Data Entry Services
    I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

  23. Congratulations reactivities ,, highly awaited new information from this site
    Good luck !!

    obat tradisional jantung rematik manjur
    obat aterosklerosis tradisional

  24. Sites like these I'm looking for
    Thanks for the information, in tunggua keep the latest news

    obat tradisional hipertiroid
    obat flek di paru paru tradisional

  25. given article is very helpful and very useful for my admin, and pardon me permission to share articles here hopefully helped Obat flu menahun herbal

  26. may be useful for all, helpful article once and pardon me permission to share also here

    Obat herbal cantengan
    Cara menyembuhkan arthritis
    Obat anemia paling manjur

  27. Excellent post. I used to be checking continuously this blog and I’m impressed! Cara Mengobati Gangguan Tidur
    Very helpful info particularly the ultimate part
    care for such information much. I used to be seeking this
    particular info for a long time.

  28. given article is very helpful and very useful for my admin, and pardon me permission to share articles here hopefully helped Obat radang telinga

  29. given article is very helpful and very useful for my admin, and pardon me permission to share articles here hopefully helped :

    Obat herbal lambung bocor
    Obat darah rendah
    Cara mengatasi jeungjeuriheun

  30. It seems I'm on the right track, I hope I can do well. The result was something I did and was doing to implement it.
    www.clickjogosclick.com

  31. Dear customer! You have successfully received 4024MB from e-SME81423384 shared data bundle. Your new balance is 4024MB. Expires 4-Oct-2017. Activate to browse.
    Kingsley Chuks Bamah post here that he sells data, his number is 09064425772. The guy is a scam. His account number is

    Kingsley Chuks Bamah 2084337221. Zenith bank. Please beware of him. He's a scumbag and a thief. Please nairalanders beware

Post a Comment