Upcoming SlideShare
×

Like this presentation? Why not share!

# NaN, Zero, & Infinities

## on Sep 12, 2011

• 452 views

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

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

### Views

Total Views
452
Views on SlideShare
450
Embed Views
2

Likes
0
4
0

### Report content

• Comment goes here.
Are you sure you want to
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n
• \n

## NaN, Zero, & InfinitiesPresentation Transcript

• NaN, Zero, & Inﬁnities Methods of the Month from Ruby’s Float Kevin Munc - @muncmanMonday, September 19, 2011
• Numbers. Non-Numbers.Monday, September 19, 2011
• nan? • True if the ﬂoat is not a valid IEEE 754 ﬂoating point number. • 0.0.nan? => false • (0.0/0).nan? => true • Float::NANMonday, 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 somethingMonday, September 19, 2011
• zero? • Only true for 0.0 or -0.0 • 0.0.zero? => true • -0.0.zero? => true • 0.0001.zero? => falseMonday, 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.ﬂickr.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
• inﬁnite? ﬁnite?Monday, September 19, 2011
• ﬁnite? • True if the ﬂoat is a valid IEEE ﬂoating point number; true if not Inﬁnity and not NaN. • 1.0.ﬁnite? => true • 0.0.ﬁnite? => true • (0.0/0).ﬁnite? => false (NaN) • (1.0/0).ﬁnite? => false (Inﬁnity)Monday, September 19, 2011
• Inﬁnity is a little weirdMonday, September 19, 2011
• inﬁnite? • Returns nil if ﬂoat is ﬁnite • Returns nil if ﬂoat is NaN • Returns -1 if ﬂoat is -inﬁnity • Returns +1 if ﬂoat is +inﬁnityMonday, September 19, 2011
• inﬁnite? • (0.0).inﬁnite? => nil (ﬁnite) • (0.0/0).inﬁnite? => nil (NaN) • (1.0/0).inﬁnite? => 1 • (-1.0/0).inﬁnite? => -1Monday, September 19, 2011
• inﬁnite? • (1.0/0) => Inﬁnity • (-1.0/0) => -Inﬁnity • Float::INFINITYMonday, September 19, 2011
• inﬁnite? • Float::INFINITY + 100 => Inﬁnity • Float::INFINITY - 100 => Inﬁnity • Float::INFINITY * Float::INFINITY => Inﬁnity • Float::INFINITY == Float::INFINITY => trueMonday, September 19, 2011
• inﬁnite? • Float::INFINITY == (1.0/0) => true • Float::INFINITY == (-1.0/0) => false • Float::INFINITY - Float::INFINITY => NaNMonday, September 19, 2011
• Uses for Inﬁnity? • Range? • everything = -Inﬁnity..Inﬁnity • Versus Float::MAX ? • To represent something completely unbounded? • Others?Monday, September 19, 2011
• questions.zero? Photo Credits • http://www.ﬂickr.com/photos/shanedk/3959630757/ • http://www.ﬂickr.com/photos/cogdog/3160739508/ • http://www.ﬂickr.com/photos/shyald/3182510815/ • http://www.ﬂickr.com/photos/affers/3183414031/ • http://www.ﬂickr.com/photos/doug88888/4542924531/ • http://www.ﬂickr.com/photos/libraryman/12835119/ • http://www.ﬂickr.com/photos/xerones/3887271255/ • http://www.ﬂickr.com/photos/jcolman/334602926/Monday, September 19, 2011