Commit 36024abaf09d6db3b0696f7619f579126df9df75

added Infinity and NaN names and specs for them

Commit diff

lib/strokedb/core_ext.rb

 
55require 'core_ext/enumerable'
66require 'core_ext/hash'
77require 'core_ext/fixnum'
8require 'core_ext/float'
89require 'core_ext/blank'
toggle raw diff

lib/strokedb/core_ext/float.rb

 
1class Float
2 ::Infinity = 1.0 / 0.0 unless defined? ::Infinity
3 ::NaN = 0.0 / 0.0 unless defined? ::NaN
4end
toggle raw diff

spec/lib/strokedb/core_ext/float_spec.rb

 
1require File.dirname(__FILE__) + '/spec_helper'
2
3describe Infinity do
4
5 it "should correctly compare with integers and floats" do
6 Infinity.should > 1
7 Infinity.should > 1.0
8 end
9
10 it "should correctly work with ranges" do
11 (-Infinity..1).should include(-100)
12 (-Infinity..Infinity).should include(Float::MAX)
13 (0..Infinity).should include(Float::MAX)
14 end
15
16 it "should return Infinity in arithmetics" do
17 (Infinity + Infinity).should == Infinity
18 (Infinity * Infinity).should == Infinity
19 (Infinity ** Infinity).should == Infinity
20 (Infinity / 0).should == Infinity
21 (Infinity / 0.0).should == Infinity
22 end
23
24 it "should return NaN in arithmetics" do
25 (Infinity - Infinity).should be_nan
26 (Infinity / Infinity).should be_nan
27 (Infinity * 0).should be_nan
28 (Infinity * 0.0).should be_nan
29 end
30
31end
32
33describe NaN do
34
35 it "should correctly compare with numerics" do
36 (NaN < 1).should be_false
37 (NaN > 1).should be_false
38 (NaN == 1).should be_false
39 end
40
41 it "should not compare with itself" do
42 NaN.should_not == NaN
43 NaN.should be_nan
44 NaN.object_id.should == NaN.object_id
45 (NaN + 1).object_id.should_not == NaN.object_id
46 end
47
48 it "should produce NaN in all the operations except NaN**0 == 1" do
49 (NaN + 1).should be_nan
50 (NaN + Infinity).should be_nan
51 (NaN - 1).should be_nan
52 (NaN * 0).should be_nan
53 (NaN * Infinity).should be_nan
54 (NaN ** 0).should == 1 # <-- WTF? (Ruby 1.8.6 rel. 2007-03-13)
55 (NaN ** 1).should be_nan
56 (NaN ** Infinity).should be_nan
57 (NaN / 0).should be_nan
58 (NaN / 1).should be_nan
59 (NaN / Infinity).should be_nan
60 end
61
62end
toggle raw diff