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

23 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

Post a Comment