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

73 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. 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.
    Http://affiliate.mobofree.com/idevaffiliate.php?id=10404

  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 {covenantsolutiontemple@gmail.com}, 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.

    IF YOU HAVE ANY PROBLEMS LIKE....

    1. LOTTERY SPELLS

    2. GETTING EX BACK SPELLS

    3. PREGNANCY SPELLS

    4. BROKEN MARRIAGE SPELLS

    5.CASINO SPELLS

    6. HIV CURE SPELLS

    7. PROTECTION SPELLS

    8. GETTING BACK TO GATHER AFTER A BREAKUP OR DIVORCE SPELL

    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

  13. This comment has been removed by the author.
  14. A GREAT SPELL CASTER (DR. AGADOGU ) THAT HELP ME BRING BACK MY EX HusbandBACK TO ME.
    Am so happy to testify about a great spell caster that helped me when all hope was lost for me to unite with my ex girl friend that i love so much. Am from CANADA and my name is jasica William, i had a Husband that love me so much but something terrible happen to our relationship one afternoon when her girl friend that was always trying to get to me was trying to force me to make love to he just because he was been jealous of his friend that i was dating and on the scene my girl friend just walk in and he thought we had something special doing together, i tried to explain things to he that her friend always do this when ever she is not with me and i always refuse he but i never told he because
    i did not want the both of them to be enemies to each other but he never believed me. he broke up with me and i tried times with out numbers to make he believe me but he never believed me until one day i heard about the GREAT DR. Agadogu email contact ineneapabe2@gmail.com

  15. This was a great and interesting article to read. I have really enjoyed all of this very cool information. Thanks a lot for providing this great info. Increase Butt Size

  16. Great one commenting from Safaxnet.com

  17. This comment has been removed by the author.
  18. “When ever i Vist Your Blog” to Read Just like Other “MP3 Download” Sites , i feel so “Happy by Reading Through” your Lines , Just like the Power To “Extract MP3 Download” Contents from “Many Sites” , Do you Know that The “MP3 Download Of Some Sites” Can be ““Powerful Than the One of Other Sites” , Although i Can Now Notice the Exact Place to “Download MP3 Songs”

     

    “”

  19. Human Resource Dissertation Experts
    Hi buddy, your blog' s design is simple and clean and i like it. Your blog posts about Online Dissertation Help are superb. Please keep them coming. Greets!!

  20. Hi buddy, your blog' s design is simple and clean and i like it. Your blog posts about Online Dissertation Help are superb. Please keep them coming. Greets!!
    Accounting Homework Assignment Help

  21. Hi buddy, your blog' s design is simple and clean and i like it.java programming assignments
    Your blog posts about Online Dissertation Help are superb. Please keep them coming. Greets!!

  22. Hi buddy, your blog' s design is simple and clean and i like it. Your blog posts about Online Dissertation Help are superb. Please keep them coming. Greets!!
    Android Project Help

  23. Using the Filter Data Task in SAS Studio Assignment Help
    Hi buddy, your blog' s design is simple and clean and i like it. Your blog posts about Online Dissertation Help are superb. Please keep them coming. Greets!!

  24. Hi buddy,Latent Function Sociology Homework Help
    your blog' s design is simple and clean and i like it. Your blog posts about Online Dissertation Help are superb. Please keep them coming. Greets!!

  25. Hi buddy, your blog' s design is simple and clean and i like it. Your blog posts about Online Dissertation Help are superb. Please keep them coming. Greets!!
    Linear Programming Homework Help

  26. GET YOUR EX WIFE, EX GIRLFRIEND, EX BOYFRIEND AND SOLVE ALL TYPE OF PROBLEM THROUGH COVENANTSOLUTIONTEMPLE@GMAIL.COM. My name is Enoch Davies
    .my wife and I got married 6 years ago and we were so much in love with each other, we have 2 kids(josh and rebecca). One day my wife came back home from her friends place and she told me that she wants us to file for a divorce that she's no longer interested in our marriage, i was surprise and everything seem to me like a joke. after then i tried all my best to get her back but there was no way. but to no avail, my heart was broken, i wondered whats happening .i was on facebook when i saw a post on how Dr Ewan helped a lady to bring back her lost husband. i was scared but because i love my wife so much and I am ready to do anything to bring her back. I decided to give it a try by contacting him via email:covenantsolutiontemple@gmail.com, So when i contacted him i narrated everything to him and he said my case is not a serious one that my husband will come back to me within 18 hours if i believe in his power.he told me to do some things to do which i did before 18 hours, my wife called me on phone and was crying for forgiveness. right now we are now a new branded family with more happiness, we are even going on vacation on xmas. please if any one of you out there is passing through same issue in your marriage, relationship issues contact Dr Ewan today or call or add him on whatsapp on +234905735398 or check out his webpage http://allwomenstalk.com/p/58034af4f725a97d108b4569. contact him today on covenantsolutiontemple@gmail.com now.
    . I will keep on sharing the good news about this. Thanks Dr.

  27. Your webiste was very nice and interesting, I've seen your post and it was very informative and very helpful to me obat sakit pinggang obat asam lambung tinggi obat patah tulang

  28. Very good write-up. I definitely appreciate this website. Continue the good work!
    Devops Online Training
    Adobe cq5 Training
    Dell Boomi Training

  29. John arnold is an academic writer of the Dissertation-Guidance. Who writes quality academic papers for students to help them in accomplishing their goals.
    DO My C programming Assignment

  30. Awesome work you have done here, I am very happy to read this nice post. You are a great writer and give us much information.
    Mechanical Engineering MBA Capstone Project Help

  31. DO My C Assignment
    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.

  32. Thanks for sharing the info, keep up the good work going.... I really enjoyed exploring your site. good resource. 
    University Essay Help

  33. Thanks for sharing the info,econ homework help
    keep up the good work going.... I really enjoyed exploring your site. good resource. 

  34. John arnold is an academic writer of the Dissertation-Guidance. Who writes quality academic papers for students to help them in accomplishing their goals.
    HR Assignment Writing Service

  35. Value Added Theory Assignment Help
    John arnold is an academic writer of the Dissertation-Guidance. Who writes quality academic papers for students to help them in accomplishing their goals.

  36. John arnold is an academic writer of the Dissertation-Guidance. Who writes quality academic papers for students to help them in accomplishing their goals.
    Depreciation Accounting Help

  37. I am so inspired by your website. Its contents are highly qualified . continued success in the waiting yes the newest update. obat infeksi saluran kencing

  38. I expect this website to be more creative and innovative full thank you very much for the article you create, it is very nice interesting obat ginjal bengkak

  39. Wow what a Great Information about World Day its very nice informative post. Thank you for sharing and I hope this website you manage to be the best obat gagal ginjal

  40. Thanks for sharing. I hope it will be helpful for too many people that are searching for this topic. obat kelenjar getah bening di leher

  41. Article was very nice and helpful at all for me thank you very much and sorry admin me permission to share

    articles herein may be useful and helpful Obat radang lambung

  42. Great article thanks for sharing and we have the other articles for sharing on the topic
    biztalk
    AWS
    Power Bi
    Tableau
    .NET

  43. A given article is very interesting and add my insights, thank you very much admin and pardon me permission to share the article here Cara menyembuhkan gerd

  44. a given article is very interesting and useful for me and sorry selali me permission to share articles herein may be useful and help you in particular are looking for :

    Cara menyembuhkan tbc
    Cara menyembuhkan tbc
    Cara menyembuhkan hidrokel

  45. Great to come to your site as the information shared is good and is explained in simple words. Good stuff you are created, thank you for sharing a nice article. obat miom

  46. Thank you so much admin already provided information so useful and pardon me permission to share articles herein may be useful and help you, especially those who are looking for :

    Cara menyembuhkan

    penebalan dinding rahim

    Cara menyembuhkan radang panggul
    Cara menyembuhkan

    ablasio retina

  47. This post has helped me for an article which I am writing. Thank you for giving me another point of view on this topic. Now I can easily complete my article. My friend recommended this blog and he was totally right keep up the fantastic work! obat benjolan di tangan

  48. Thank you so much admin is already providing the information to us and here we are sorry permission to share articles may be useful and help you in particular are looking for :

    Cara mengatasi gatal selangkangan
    Obat ruam popok
    Obat emboli paru

  49. Thank you so much admin already provide information and share permissions are sorry here we hopefully article particularly helpful for all of you who are looking for Obat radang ginjal

  50. Thank you so much admin already provide information and share permissions are sorry here we hopefully article particularly helpful for all of you who are looking for

    cara mengobati syaraf kejepit
    Cara menghilangkan jerawat
    Cara menghilangkan benjolan di leher

  51. John arnold is an academic writer of the Dissertation-Guidance. Who writes quality academic papers for students to help them in accomplishing their goals.
    Help With MathCad Online Homework

  52. I really impressed after read this because of some quality work and informative thoughts. obat sinusitis

  53. a given article is very interesting and very useful for me, thank you very much admin and pardon me permission to share the article here :

    Obat keloid menahun
    Cara mengatasi keputihan
    obat jantung berdebar

  54. By the way, Thank you for sharing with us, and we sincerely hope you will continue to update or post other articles.
    obat penghilang luka bekas operasi

  55. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write obat infeksi saluran kencing

  56. Highly qualified information because it can provide useful info thanks.
    cara alami menyembuhkan leptospirosis



  57. Wonderful blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Many thanks.
    2048 online | tanki online 3

Post a Comment