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

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

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

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

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

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

>>> inf = maxfloat()

>>> inf + inf

>>> inf - inf

>>> 1 / inf

>>> 1/(-inf)

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

18 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 about simply adding RAM to a server to compensate for poor coding of PHP. Is this true? Can you send me an email at

    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. Great information here Seun! Thanks a lot for this wonderful detail, i think our web hosting servers should also upgrade too.

  7. Nice 1 Seun. In actual sense, minfloat(1.0) resembles 1/minfloat(1.0) in that e-324 is 1/e324 only that the former is infinitesimal and definite while the latter is just inf. Still wondering if √324 = 18 has anything to do with anything being a complete square. *feelingstupid* Lol

  8. Click, then sign up to start making Cash.

  9. Droidphoria focus on Flashing Android ROMs, Firmwares, Custom Kernels, Thermal Controls, and many more Mods for Android.

  10. Best and genuine spell caster online to save your marriage and relationship
    save your marriage if it is heading for divorce now Hello everyone! Life without my lover was a real disaster for me,What on Earth will i do to thank Dr.Ewan for his help. Are you having a hard time searching for a way to get your ex partner back, look no further, this was how i got mine back you can get yours today. My husband and I have been through every top reason for divorce; financial struggles, bankruptcy, stressful jobs, becoming parents when we weren’t ready (neither one of us would give our kids back just the amount of stress is overwhelming) we fought ALL the time over anything and everything. I threatened divorce all the time. One day after a fight I said I was done and filling. He told me he wasn’t in love with me anymore. After a day or two of cooling off I realized that divorce is not what I wanted. No, our marriage was not healthy but we had so much going against us an neither one of us were trying. I begged for him to forgive me and that I didn’t mean it. He told me he loved me but wasn’t in love with me anymore. Those words hurt and I believed him.One day i was online for tutorials, as i was browsing i saw a review about a spell caster who could solve my problem, i have heard about spell casting, and magic, i said i should give it a try i contacted the spell caster his name is Dr Ewan of {}, i contacted him and he told me not to worry that all he had to do for me is a love spell, he did it and told me that i should give my husband 48 hours and that he would realize him self, i waited anxiously and one the second day of casting the spell, he came to me and told me he was sorry for the wrong he did to me, he has agreed to send me to school and we are happy. Thank you Dr Ewan you are the best. If you have any marital problem, winning of lottery and many other problems you can contact him on his email,{COVENANTSOLUTIONTEMPLE@GMAIL.COM} or call his mobile number +2347052958531, he is a good and kind man. Thanks. ELIZABETH STRONG you can also call or text me for questions or anything +17015667707. contact him Via whatsapp+2349057353987.










    Do not cry anymore, contact this powerful spell caster Dr.Ewan on his email: COVENANTSOLUTIONTEMPLE@GMAIL.COM or call him on his mobile number +2347052958531 or contact him Via whatsapp+2349057353987

  11. This was a great and interesting article to read. I have really enjoyed all of this very cool information.
    Computer Network Tutor

  12. This is an excellent post i seen.I have to thanks to you to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post.
    Article Writing Service

Post a Comment