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

94 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

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

    Obat tradisional

    batu ginjal

    Cara mengobati sinusitis

    akut

    Cara menyembuhkan ginjal bengkak

  33. Thanks For Your valuable posting, it was very informative
    Webdesign Lüdenscheid

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

    makanan untuk yang terkena sinus
    walatra sehat mata asli
    obat mengobati kulit eritrasma

  35. This comment has been removed by the author.
  36. nice and precise..... your articles gibes great meanings

    study without IELTS

  37. UNILAG Becomes First Nigerian University To Obtain TV Licence

    http://njatym.blogspot.com.ng/2017/11/unilag-becomes-first-nigerian.html?m=1

  38. Empat hal ini dijamin membuat showroom mebel bandung tenang dan damai di rumah. Setelah lelah bekerja seharian, orang sangat membutuhkan pembuat sofa di bandung. sofa minimalis bandung merupakan kunci utama dalam mendapatkan ketenangan dan kedamaian pikiran di dalam rumah. sofa ruang tamu bandung yang mewah dan berukuran besar bukan ukuran utama untuk membuat Anda damai dan nyaman di dalam rumah. Untuk itu, ada furniture kitchen set di bandung yang jauh lebih sederhana seperti berikut ini.

    Hati yang tenang, damai, jiwa optimis akan membuat kitchen set di kota bandung bercahaya merupakan dambaan semua orang di dunia ini. Jujur saja, bahwa banyak kitchen set bagus bandung bekerja dan mencari uang siang dan malam, banting tulang. Satu tujuannya, yakni jasa tukang kitchen set bandung. Tulisan sederhana ini akan sambal roa di jakarta. cek juga jual gendongan bayi hanaroo dan juga kitchen set bandung timur.

  39. Thanks for sharing information very interesting and useful. Do not forget to visit our website to share information and knowledge about health Penyebab Dan Penanganan Batuk Berdarah Yang Alami Dan Terbukti Ampuh

  40. Salah satu fungsi utama cari sofa bandung hakiki. Bagaimana cara menciptakan rumah yang memberikan kenyamanan? Cara paling mudah dan sederhana adalah dengan berinvestasi membangun atau membeli rumah asri jasa tukang kayu furniture bandung. Menjadi impian lemari pakaian bandung asri. Rumah asri yang memberi jasa pembuatan lemari perpaduan antara layout ruangan, jual gerobak jualan bandung, dan material bangunan.

    Untuk jasa pembuatan kitchen set jakarta baik, Anda sebaiknya harga kitchen set minimalis murah memasuki bagian dalam rumah. Selain kitchen set kota bandung, jawa barat sumber cahaya alami, parfum oriflame yang wanginya lembut memberi kesan luas pada rumah kecil Anda. jual abon roa Jendela juga memberi Anda pemandangan luar, tukang kayu furniture bandung sehingga terasa segar dan menyatu dengan alam.

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

    obat kanker tulang tradisional
    obat tradisional atelektasis aman
    obat penyakit ginjal herbal

  42. Thanks for the information presented on your website
    Very in waiting for other information

    walatra bersih wanita original
    obt keputihan berbau busuk ampuh
    obat pendarahan rahim tradisional

  43. activity, highly awaited new information from this site
    Good luck !!

    obat kanker rahim tradisional
    obat nyeri pergelangan tangan secara herbal
    keputihan berbau busuk

  44. HOW DR.ITUA HELP ME TO MAKE MY PENIS INCREASE  LONG  THIS DOC IS AMAZING ?
    by: shuron edmond, 

    My name is shuron edmond,i just want to share my testimony with the world on how Doctor Itua Help me to enlarge my penis. Please read my good news carefully and i am sure it will affect your life positively on how you will also enlarge your penis,because i know some many people out there also need his help!

    I came across so many comments about Dr Itua Penis Enlargement Medicine some weeks ago, though I had really wanted my penis to be large, long and thick,because i was not able to have sex with my wife..It was really affecting our marriage and my wife was about to divorce me. I had about 8.128cm – 3.2 inches before. Am really amazed on the fast results archived within 3 days of using Dr. Itua Penis Enlargement Medicine. It work and now I have got 22.87cm – 7.5 inches now. And my wife love it more now, i began to feel the enlargement of my penis, This went on for a little period of about 5 days and to my surprise my wife keeps screaming that she love my big dick now. And my penis is now 7.5 inches long on erection and off course very large round. I am very happy for this Penis Enlargement experience. He can help with all kinds of cure you may need as flows: 

    Pines Enlargement 
    Low sperm count 
    Weak Erection
    Love spell 
    Herpes spell
    HIV spell 
    Pregnancy spell 
    Marriage for spell
    cancer
    watering sperm
    womb fertilization
    penis erection
    witch craft attack
    s t d diseases
    internal heat
    rashes
    low sperm count
    dairy
    long time sickness
    kidney,heart,lungs,problem
    with doctor,. Itua you are in safe hands your healing is assured
    Email:(drituaherbalsolutionhome@gmail.com) 
    Call (+2347036387192)

  45. Hello everyone i am happy to spread my testimony of a strong spell caster called Dr Great. I'm Kaitlyn Houston and i live in USA, my husband and i had a little fight because of that he wanted to divorce me i was so afraid to lose him because i love him very much so i search online for help and i saw alot of people's testimonies on how Dr Great help them and came out with positive results like Divorces, Cancers, Spiritual problems and for Barrens to give birth and others. So i emailed him and told him my problem and he gave me instructions which I followed and after 48 hours later he told me that the spell is done and my husband will no longer divorce me and when my husband came back from work he told me that he has canceled the divorce papers he didn't know what came over him that he is sorry I was so happy and I thank Dr Great for his help If you need Dr Great help email him at infinitylovespell@gmail.com and he will put an end to your problem

  46. Thanks for the information presented on your website
    Very in waiting for other information

    obat wasir berdarah ampuh
    obat diabetes melitus ampuh
    obat untuk hepatitis a

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

    makanan untuk tulang keropos
    obat wasir tradisional ampuh
    manfaat bawang putih untuk tbc

  48. Hello every one i want to share my testimony on how i got my sugar-mummy true agent RAZAK )(08148752565), a friend of mine that always give me money…cos he was very rich, one day he said he is not going to give me fish that he is going to teach me how to fish, so i was very happy,never knew he have a SUGAR-MUMMY all my life will spend together! so i was CONNECTED with a RICH LOVING and CARING SUGAR-MUMMY, few days i was awarded a contract worth of millions, in my business i am doing very well right now,i am now the one that give money out before it was heard, if you want to be CONNECTED with a rich SUGAR-MUMMY or SUGAR-DADDY or LESBIAN-PARTNERS or GAY-PARTNERS contact [MAMAS LOVE-DATER BASE ORGANIZATION on 08148752565]FOR FAST HOOKUP

  49. Thanks for every other informative site. The place else may just I get that kind of information written in such an ideal means? I have a venture that I’m just now operating on. and I have been on the look out for such information.
    obat ginjal bengkak

  50. The news that you present is very useful .. hopefully other news as well
    You can share .. success
    Best regards..will run activetas

    khasiat brotowali untuk rematik
    biaya operasi rahim
    obat kista ganglion tradisional

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

    makanan untuk penyakit kuning
    obat flek paru paru anak tradisional
    cara melancarkan haid dengan soda

  52. Your good but I think..I have something better here

  53. the website you manage is very good and in my opinion this is the best wesbite
    obat ginjal bengkak
    obat paru paru basah
    obat patah tulang
    obat tbc kelenjar

  54. Facebook Hilpline number
    http://www.servicedonline.com/paypal-phone-number/
    I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.


  55. I am very happy Because with the content on this blog I can add my knowledge. thanks. Do not forget to visit our website to share information about health.
    cara mengobati gula basah
    cara mengobati kista bartholin
    cara mengobati abses payudara
    Cara Mengobati Penyakit Tipes

  56. khasiat walatra gamat emas kapsul


  57. I can only express a word of thanks. Because with the content on this blog I can add knowledge I, Do not forget to visit our website to share information and knowledge about health ;
    cara mengobati kista coklat
    khasiat walatra gamat emas kapsul


  58. I can only express a word of thanks. Because with the content on this blog I can add knowledge I, Do not forget to visit our website to share information and knowledge about health ;
    cara mengobati kista coklat
    khasiat walatra gamat emas kapsul

  59. Good morning to noon ,, the information is very helpful
    Very waiting for the latest news updates from your site
    Success!

    bahaya asam lambung saat hamil
    makanan penderita infeksi paru paru
    ciri ciri kista coklat sembuh

  60. thank you information, very helpful, do not forget to visit also kunju our page to berai information about health
    12 cara menghilangkan bibir menghitam
    cara menyembuhkan vertigo secara total

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

    makanan penyebab asam lambung
    manfaat madu untuk anak 1 tahun

  62. Nice one https://okhitng.com

  63. If you want to learn how to pass your exams excellently without burning night candle, visit www.campusnerds.com.ng thank me later

  64. The link is: http://campusnerds.com.ng

  65. MASTER OLUWASEUN OSEWA, am your fan, thou i would like to share something with you. email.me@ aondona19@gmail.com. blazesam is my nairaland user id

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

    obat tradisional kista ginjal
    makanan untuk penderita liver
    obat hepatitis b kronis

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

    obat tradisional kista ginjal
    makanan untuk penderita liver
    obat hepatitis b kronis

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

    ciri ciri diabetes melitus herbal
    obat tradisional sakit tulang belikat
    obat neurofibromatosis tradisional

  69. It is imperative that we read blog post very carefully. I am already done it and find that this post is really amazing.
    obat keloid
    obat ginjal bengkak
    obat thalasemia
    obat limpa bengkak
    obat kencing manis

  70. Really impressive post. I read it whole and going to share it with my social circules. I enjoyed your article and planning to rewrite it on my own blog.
    pengobatan tumor
    cara mengatasi perut begah
    pengobatan liver bengkak
    obat keloid
    obat ginjal bengkak
    obat thalasemia
    obat benjolan di kepala
    obat benjolan di payudara

  71. I read it whole and going to share it with my social circules. I enjoyed your article and planning to rewrite it on my own blog.
    Free Nigeria Music MP3 Download | OkHit

  72. Thanks for the information presented on your website
    Very in waiting for other information

    obat gusi bengkak tradisional
    obat nyeri ulu hati tradisional
    ciri-ciri penyakit miom

  73. Thanks for the information presented on your website
    Very in waiting for other information

    obat tradisional urat syaraf kejepit
    obat sinusitis alami

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

    obat ginjal bengkak tradisional
    penyebab penyakit liver

  75. Good evening pls how can I have a personal chat with you? It is very important. Thanks

  76. Thanks for the information presented on your website
    Very in waiting for other information

    obat infeksi pencernaan
    obat tradisional kanker hati
    obat radang batu empedu

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

    cara mengatasi mata merah
    obat gondok beracun
    obat tradisional hipertiroid

  78. The latest information we are waiting for lho..semoga what is given can be useful
    Terimakash..success always everything..salam know

    bahaya anemia aplastik
    makanan untuk penderita asam lambung
    obat blefaritis tradisional

  79. The post is written in very a good manner and it contains many useful information for me. Thank you very useful information admin, and pardon me permission to share articles here may help :

    obat benjolan di bawah rahang
    obat amandel bengkak
    obat benjolan di ketiak
    obat tumor ginjal
    obat lambung bocor
    obat borok di kepala
    obat kanker nasofaring

  80. http://payhigh.online/?invite=68570

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

    pola makan penderita syaraf kejepit
    obat kanker hati ampuh

  82. This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine

    risa herbal
    penyebab sakit pinggang
    obat penyakit mata glaukoma
    obat gagal ginjal
    obat penebalan dinding rahim
    obat penghancur kanker
    obat varikokel

  83. You can share .. success
    Best regards..will run activetas

    cara mengobati lambung ampuh
    obat tradisional mioma uteri

  84. I am happy to be here and this wonderful article. I have found here lots of interesting information for my knowledge I need. all the details you provide to us, it was very helpful and useful, thanks for sharing this post.

    khasiat obat herbal
    Obat herbal penyakit kulit
    obat benjolan
    toko obat herbal
    obat gondok beracun
    risa herbal

  85. This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around

    routine

    risa herbal
    penyebab sakit pinggang
    obat mata glaukoma
    obat gagal ginjal
    obat penebalan dinding rahim
    obat penghancur kanker
    obat varikokel

  86. Nice to be visiting your blog again, it has been months for me. Well this article that i’ve been waited for so long.
    cara menghilangkan benjolan di ketiak
    cara menyembuhkan batuk rejan
    obat penyakit gondok
    cara menghilangkan benjolan di kemaluan
    cara menghilangkan bekas borok

Post a Comment