SlideShare a Scribd company logo
1 of 18
Download to read offline
NaN, Zero,
                              & Infinities
                         Methods of the Month from Ruby’s Float
                              Kevin Munc - @muncman




Monday, September 19, 2011
Numbers. Non-Numbers.
Monday, September 19, 2011
nan?
                   • True if the float is not a valid IEEE 754
                             floating point number.
                   • 0.0.nan? => false
                   • (0.0/0).nan? => true
                   • Float::NAN

Monday, September 19, 2011
nan?

                   • 0.0/0 => NaN
                   • This is in contrast to the
                             ZeroDivisionError that results when using
                             Fixnums to divide by zero.




Monday, September 19, 2011
Monday, September 19, 2011
Zero is worth something
Monday, September 19, 2011
zero?
                   • Only true for 0.0 or -0.0
                   • 0.0.zero? => true
                   • -0.0.zero? => true
                   • 0.0001.zero? => false

Monday, September 19, 2011
Tangent::Trivia
       “The centre of York station was used as
       the zero point for distance measurement
       for much of the rail network in the North
       East of England.”                 - http://www.flickr.com/photos/xerones/3887271255/




       The lines, from the top:
           1. Longlands Loop (Northallerton)
           2. Market Weighton & Beverley
           3. Micklefield Branch
           4. Raskelf Curve
           5. Sherburn Branch
           6. York & Harrogate
           7. York & Market Weighton
           8. York & Newcastle
           9. York & North Midland
           10. York & Scarborough




Monday, September 19, 2011
infinite? finite?
Monday, September 19, 2011
finite?
                   • True if the float is a valid IEEE floating point
                             number; true if not Infinity and not NaN.
                   • 1.0.finite? => true
                   • 0.0.finite? => true
                   • (0.0/0).finite? => false        (NaN)
                   • (1.0/0).finite? => false        (Infinity)


Monday, September 19, 2011
Infinity is a little weird
Monday, September 19, 2011
infinite?


           •      Returns nil if float is finite

           •      Returns nil if float is NaN

           •      Returns -1 if float is -infinity

           •      Returns +1 if float is +infinity




Monday, September 19, 2011
infinite?
                   • (0.0).infinite? => nil (finite)
                   • (0.0/0).infinite? => nil (NaN)
                   • (1.0/0).infinite? => 1
                   • (-1.0/0).infinite? => -1

Monday, September 19, 2011
infinite?

                   • (1.0/0) => Infinity
                   • (-1.0/0) => -Infinity
                   • Float::INFINITY


Monday, September 19, 2011
infinite?
                   • Float::INFINITY + 100 => Infinity
                   • Float::INFINITY - 100 => Infinity
                   • Float::INFINITY * Float::INFINITY => Infinity
                   • Float::INFINITY == Float::INFINITY => true

Monday, September 19, 2011
infinite?

                   • Float::INFINITY == (1.0/0) => true
                   • Float::INFINITY == (-1.0/0) => false
                   • Float::INFINITY - Float::INFINITY => NaN


Monday, September 19, 2011
Uses for Infinity?
                   • Range?
                    • everything = -Infinity..Infinity
                    • Versus Float::MAX ?
                   • To represent something completely
                             unbounded?
                   • Others?

Monday, September 19, 2011
questions.zero?
                  Photo Credits
        •      http://www.flickr.com/photos/shanedk/3959630757/


        •      http://www.flickr.com/photos/cogdog/3160739508/


        •      http://www.flickr.com/photos/shyald/3182510815/


        •      http://www.flickr.com/photos/affers/3183414031/


        •      http://www.flickr.com/photos/doug88888/4542924531/


        •      http://www.flickr.com/photos/libraryman/12835119/


        •      http://www.flickr.com/photos/xerones/3887271255/


        •      http://www.flickr.com/photos/jcolman/334602926/




Monday, September 19, 2011

More Related Content

More from Kevin Munc (8)

Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)
 
Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)
 
cycle (MOTM 2010.07)
cycle (MOTM 2010.07)cycle (MOTM 2010.07)
cycle (MOTM 2010.07)
 
empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)
 
Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)
 
Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)
 
The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)
 
Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

NaN, Zero, & Infinities

  • 1. NaN, Zero, & Infinities Methods of the Month from Ruby’s Float Kevin Munc - @muncman Monday, September 19, 2011
  • 3. nan? • True if the float is not a valid IEEE 754 floating point number. • 0.0.nan? => false • (0.0/0).nan? => true • Float::NAN Monday, September 19, 2011
  • 4. nan? • 0.0/0 => NaN • This is in contrast to the ZeroDivisionError that results when using Fixnums to divide by zero. Monday, September 19, 2011
  • 6. Zero is worth something Monday, September 19, 2011
  • 7. zero? • Only true for 0.0 or -0.0 • 0.0.zero? => true • -0.0.zero? => true • 0.0001.zero? => false Monday, September 19, 2011
  • 8. Tangent::Trivia “The centre of York station was used as the zero point for distance measurement for much of the rail network in the North East of England.” - http://www.flickr.com/photos/xerones/3887271255/ The lines, from the top: 1. Longlands Loop (Northallerton) 2. Market Weighton & Beverley 3. Micklefield Branch 4. Raskelf Curve 5. Sherburn Branch 6. York & Harrogate 7. York & Market Weighton 8. York & Newcastle 9. York & North Midland 10. York & Scarborough Monday, September 19, 2011
  • 10. finite? • True if the float is a valid IEEE floating point number; true if not Infinity and not NaN. • 1.0.finite? => true • 0.0.finite? => true • (0.0/0).finite? => false (NaN) • (1.0/0).finite? => false (Infinity) Monday, September 19, 2011
  • 11. Infinity is a little weird Monday, September 19, 2011
  • 12. infinite? • Returns nil if float is finite • Returns nil if float is NaN • Returns -1 if float is -infinity • Returns +1 if float is +infinity Monday, September 19, 2011
  • 13. infinite? • (0.0).infinite? => nil (finite) • (0.0/0).infinite? => nil (NaN) • (1.0/0).infinite? => 1 • (-1.0/0).infinite? => -1 Monday, September 19, 2011
  • 14. infinite? • (1.0/0) => Infinity • (-1.0/0) => -Infinity • Float::INFINITY Monday, September 19, 2011
  • 15. infinite? • Float::INFINITY + 100 => Infinity • Float::INFINITY - 100 => Infinity • Float::INFINITY * Float::INFINITY => Infinity • Float::INFINITY == Float::INFINITY => true Monday, September 19, 2011
  • 16. infinite? • Float::INFINITY == (1.0/0) => true • Float::INFINITY == (-1.0/0) => false • Float::INFINITY - Float::INFINITY => NaN Monday, September 19, 2011
  • 17. Uses for Infinity? • Range? • everything = -Infinity..Infinity • Versus Float::MAX ? • To represent something completely unbounded? • Others? Monday, September 19, 2011
  • 18. questions.zero? Photo Credits • http://www.flickr.com/photos/shanedk/3959630757/ • http://www.flickr.com/photos/cogdog/3160739508/ • http://www.flickr.com/photos/shyald/3182510815/ • http://www.flickr.com/photos/affers/3183414031/ • http://www.flickr.com/photos/doug88888/4542924531/ • http://www.flickr.com/photos/libraryman/12835119/ • http://www.flickr.com/photos/xerones/3887271255/ • http://www.flickr.com/photos/jcolman/334602926/ Monday, September 19, 2011

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n