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

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

«Oldest   ‹Older   401 – 429 of 429   Newer›   Newest»
  1. How can I take my blog to the next stage


    the reality nation.blogspot.com

  2. How can I take my blthereality
    next level

    therealitynation.blogspot.com

  3. Priya Singh and Mumbai Escorts are up for all your physical and mental needs. Experience the Mumbai Escorts
    Call Girls and make your time full of Erotic Pleasure.I am very beautiful and sexy in appearance and I am 21 years old, and my figure is 37, 30, 36 my height is 5 feet 2 inches. I am a very hot Independent Female Escort Service
    .
    Visit My Website https://escortagencyinmumbai.com/
    Bandra Escorts
    Mumbai Call Girls
    Andheri Escorts
    Juhu Escorts

  4. Enjoy the Passionate service of Hyderabad Escorts. Premium Escorts in Hyderabad for Utmost Entertainment. Truly secured doorway to reach the lovely Hyderabad escort service profiles. Visit now - hyderabadescortsagency.co.in.
    Hyderabad Escorts
    Hyderabad Escort
    Independent escorts in Hyderabad
    Hyderabad independent escorts
    Hyderabad escorts service
    Hyderabad escort services
    Escorts services in Hyderabad
    Hyderabad escort model
    High class escorts in Hyderabad
    Hyderabad escort models

  5. Hi I am Arpita Jain. I am a Mumbai based girl. I offer services and so do my group of girls. There are many models working for me. If you are looking specifically for young girls, I have many College girls.
    Mumbai Call Girls
    Juhu Escorts Service
    Andheri Escorts
    Bandra Escorts Service
    Navi Mumbai Escorts
    Mumbai Hotel Sex
    Russian Escorts In Mumbai
    Bombay Call Girls

  6. Book hot and busty Call Girls in Hyderabad to fulfill your demand & find Hyderabad Escorts service for sex satisfaction at low rate, avail 24/7. Visit us:- https://bit.ly/3yN7pYw
    Secunderabad Escorts
    Hyderguda Escorts
    Lb-Nagar Escorts
    Lingampally Escorts
    Malakpet-Extension Escorts
    Mehdipatnam Escorts
    Moosapet Escorts
    Pathargatti Escorts
    Qutubullapur Escorts

  7. Hurry Up to Book Now a Masterpiece Sizzling Escorts Hyderabad, and enjoy your night.
    We also provide our services in Mumbai: Escort in Mumbai

  8. How to report cyber crime to FBI

    https://inflowpost.com/how-to-report-a-cyber-crime-to-fbi-46

  9. Hello friend, I have read your web page. Really it is relevant to dedicated subject. Keep it continue with this website. My name is Arpita Jain. I am an Mumbai independent female model. I am very hot and sexy girl. I deal in Bandra Escort Service. I provide my Mumbai premium services in five star hotels. I am available for outcall and incall in lowest price. If you want to book my service kindly contact me through my website.
    Bandra Escorts
    Bandra Escorts
    Bandra Escorts

  10. I found the information you provided to be very helpful. I like your blog very much. It was a very useful experience for me. If you are planning an outdoor trip, then I suggest you check out the Best 4 Person Tent. Here are the best tents for four people that you can buy.

  11. Thank you for sharing this wonderful post. Thank you for the informative information. I really appreciate your effort toward the reader. I suggest reading the blog post on Camera and Microphone Hacking and protect yourself from hacking. There are several apps available on your phone or computer that can access your camera, microphone, and other features, including location information. Below are the steps you can follow to check which apps have asked for permission and revoke previously granted permissions.

  12. Hello viewer’s
    I don’t have much to say there are so may scammers going on online so we cant detect the real herbal medicine doctors. Thank GOD for leading me, please don’t ignore this post is real Dr Emmanuel, is a real herbal Doctor, he cured me from Herpes virus, i am living so happy and free , i was fully recovered within 4 weeks of usage of dr Emmanuel herbal medicine ,please viewers out there that have any deadly disease don't fail to contact him via his email; nativehealthclinic@gmail.com or WhatsApp/Call: +2348140073965 thanks once again to dr Emmanuel GOD bless you abundantly
    he also cured HIV/AIDS,CANCER,GENITAL WART,COPD,BRAIN TUMOR

  13. Do you need a sugar mummy or sugar daddy if yes call us on 09037906223

  14. Nice article I love it very informative. you can view Best 10 Moviesrulz Alternatives & Similar Websites thanks

«Oldest ‹Older   401 – 429 of 429   Newer› Newest»

Post a Comment