NaN, Zero, & Infinities

626 views

Published on

My Method of the Month lightning talk for the Columbus Ruby Brigade's September 2011 meeting.

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

NaN, Zero, & Infinities

  1. 1. NaN, Zero, & Infinities Methods of the Month from Ruby’s Float Kevin Munc - @muncmanMonday, September 19, 2011
  2. 2. Numbers. Non-Numbers.Monday, September 19, 2011
  3. 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::NANMonday, September 19, 2011
  4. 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
  5. 5. Monday, September 19, 2011
  6. 6. Zero is worth somethingMonday, September 19, 2011
  7. 7. zero? • Only true for 0.0 or -0.0 • 0.0.zero? => true • -0.0.zero? => true • 0.0001.zero? => falseMonday, September 19, 2011
  8. 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 & ScarboroughMonday, September 19, 2011
  9. 9. infinite? finite?Monday, September 19, 2011
  10. 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. 11. Infinity is a little weirdMonday, September 19, 2011
  12. 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 +infinityMonday, September 19, 2011
  13. 13. infinite? • (0.0).infinite? => nil (finite) • (0.0/0).infinite? => nil (NaN) • (1.0/0).infinite? => 1 • (-1.0/0).infinite? => -1Monday, September 19, 2011
  14. 14. infinite? • (1.0/0) => Infinity • (-1.0/0) => -Infinity • Float::INFINITYMonday, September 19, 2011
  15. 15. infinite? • Float::INFINITY + 100 => Infinity • Float::INFINITY - 100 => Infinity • Float::INFINITY * Float::INFINITY => Infinity • Float::INFINITY == Float::INFINITY => trueMonday, September 19, 2011
  16. 16. infinite? • Float::INFINITY == (1.0/0) => true • Float::INFINITY == (-1.0/0) => false • Float::INFINITY - Float::INFINITY => NaNMonday, September 19, 2011
  17. 17. Uses for Infinity? • Range? • everything = -Infinity..Infinity • Versus Float::MAX ? • To represent something completely unbounded? • Others?Monday, September 19, 2011
  18. 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

×