large_binomials 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +674 -0
- data/README.md +82 -0
- data/Rakefile +45 -0
- data/bin/test_performance_binomial.rb +37 -0
- data/large_binomials.gemspec +37 -0
- data/lib/core_ext/integer.rb +127 -0
- data/lib/large_binomials.rb +26 -0
- data/lib/large_binomials/large_float.rb +129 -0
- data/spec/spec_helper.rb +32 -0
- data/spec/unit/integer_spec.rb +247 -0
- data/spec/unit/large_float_spec.rb +281 -0
- metadata +62 -0
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Ruby Library to Calculate Large Binomials
|
4
|
+
# Copyright © 2013 Filip van Laenen <f.a.vanlaenen@ieee.org>
|
5
|
+
#
|
6
|
+
# This file is part of the Ruby Library to Calculate Large Binomials.
|
7
|
+
#
|
8
|
+
# The Ruby Library to Calculate Large Binomials is free software: you can
|
9
|
+
# redistribute it and/or modify it under the terms of the GNU General Public
|
10
|
+
# License as published by the Free Software Foundation, either version 3 of the
|
11
|
+
# License, or (at your option) any later version.
|
12
|
+
#
|
13
|
+
# The Ruby Library to Calculate Large Binomials is distributed in the hope that
|
14
|
+
# it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
15
|
+
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
16
|
+
# Public License for more details.
|
17
|
+
#
|
18
|
+
# You can find a copy of the GNU General Public License in /LICENSE
|
19
|
+
#
|
20
|
+
|
21
|
+
$LOAD_PATH << 'lib'
|
22
|
+
require 'large_binomials'
|
23
|
+
|
24
|
+
require 'timeout'
|
25
|
+
|
26
|
+
RSpec.configure do |config|
|
27
|
+
config.around do |example|
|
28
|
+
Timeout.timeout(1) do
|
29
|
+
example.run
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,247 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Ruby Library to Calculate Large Binomials
|
4
|
+
# Copyright © 2013 Filip van Laenen <f.a.vanlaenen@ieee.org>
|
5
|
+
#
|
6
|
+
# This file is part of the Ruby Library to Calculate Large Binomials.
|
7
|
+
#
|
8
|
+
# The Ruby Library to Calculate Large Binomials is free software: you can
|
9
|
+
# redistribute it and/or modify it under the terms of the GNU General Public
|
10
|
+
# License as published by the Free Software Foundation, either version 3 of the
|
11
|
+
# License, or (at your option) any later version.
|
12
|
+
#
|
13
|
+
# The Ruby Library to Calculate Large Binomials is distributed in the hope that
|
14
|
+
# it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
15
|
+
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
16
|
+
# Public License for more details.
|
17
|
+
#
|
18
|
+
# You can find a copy of the GNU General Public License in /LICENSE
|
19
|
+
#
|
20
|
+
|
21
|
+
require 'spec_helper'
|
22
|
+
|
23
|
+
describe Integer, '#binomial' do
|
24
|
+
it 'calculates the binomial C0,1 as 1' do
|
25
|
+
1.binomial(0).should eq(1)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'calculates the binomial C1,2 as 2' do
|
29
|
+
2.binomial(1).should eq(2)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'calculates the binomial C2,3 as 3' do
|
33
|
+
3.binomial(2).should eq(3)
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'calculates the binomial C2,4 as 6' do
|
37
|
+
4.binomial(2).should eq(6)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'calculates the binomial C1030,515 as 2.859641372997808×10³⁰⁸' do
|
41
|
+
1030.binomial(515).should eq(LargeBinomials::LargeFloat.new(2.859641372997808, 308))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe Integer, '#binomial_by_product_of_divisions' do
|
46
|
+
it 'calculates the binomial C0,1 as 1' do
|
47
|
+
1.binomial_by_product_of_divisions(0).should eq(1)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'calculates the binomial C1,2 as 2' do
|
51
|
+
2.binomial_by_product_of_divisions(1).should eq(2)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'calculates the binomial C2,3 as 3' do
|
55
|
+
3.binomial_by_product_of_divisions(2).should eq(3)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'calculates the binomial C2,4 as 6' do
|
59
|
+
4.binomial_by_product_of_divisions(2).should eq(6)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'calculates the binomial C1030,515 as 285964137299780816375713003417892828985047376491079895916065830932808184515508623336290716027603487838840658378528491902852969839049878057252288352997125670272624219209393564328660099438209732411401014699912025370547761703640450600215234966704145601511388368027664919136794617311260529388699426829733229057400' do
|
63
|
+
1030.binomial_by_product_of_divisions(515).should eq(285964137299780816375713003417892828985047376491079895916065830932808184515508623336290716027603487838840658378528491902852969839049878057252288352997125670272624219209393564328660099438209732411401014699912025370547761703640450600215234966704145601511388368027664919136794617311260529388699426829733229057400)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe Integer, '#binomial_by_division_of_products' do
|
68
|
+
it 'calculates the binomial C0,1 as 1' do
|
69
|
+
1.binomial_by_division_of_products(0).should eq(1)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'calculates the binomial C1,2 as 2' do
|
73
|
+
2.binomial_by_division_of_products(1).should eq(2)
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'calculates the binomial C2,3 as 3' do
|
77
|
+
3.binomial_by_division_of_products(2).should eq(3)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'calculates the binomial C2,4 as 6' do
|
81
|
+
4.binomial_by_division_of_products(2).should eq(6)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'calculates the binomial C1030,515 as 285964137299780816375713003417892828985047376491079895916065830932808184515508623336290716027603487838840658378528491902852969839049878057252288352997125670272624219209393564328660099438209732411401014699912025370547761703640450600215234966704145601511388368027664919136794617311260529388699426829733229057400' do
|
85
|
+
1030.binomial_by_division_of_products(515).should eq(285964137299780816375713003417892828985047376491079895916065830932808184515508623336290716027603487838840658378528491902852969839049878057252288352997125670272624219209393564328660099438209732411401014699912025370547761703640450600215234966704145601511388368027664919136794617311260529388699426829733229057400)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe Integer, '#binomial_by_division_of_parallel_products' do
|
90
|
+
it 'calculates the binomial C0,1 as 1' do
|
91
|
+
1.binomial_by_division_of_parallel_products(0).should eq(1)
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'calculates the binomial C1,2 as 2' do
|
95
|
+
2.binomial_by_division_of_parallel_products(1).should eq(2)
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'calculates the binomial C2,3 as 3' do
|
99
|
+
3.binomial_by_division_of_parallel_products(2).should eq(3)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'calculates the binomial C2,4 as 6' do
|
103
|
+
4.binomial_by_division_of_parallel_products(2).should eq(6)
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'calculates the binomial C1030,515 as 285964137299780816375713003417892828985047376491079895916065830932808184515508623336290716027603487838840658378528491902852969839049878057252288352997125670272624219209393564328660099438209732411401014699912025370547761703640450600215234966704145601511388368027664919136794617311260529388699426829733229057400' do
|
107
|
+
1030.binomial_by_division_of_parallel_products(515).should eq(285964137299780816375713003417892828985047376491079895916065830932808184515508623336290716027603487838840658378528491902852969839049878057252288352997125670272624219209393564328660099438209732411401014699912025370547761703640450600215234966704145601511388368027664919136794617311260529388699426829733229057400)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe Integer, '#binomial_by_pascals_triangle' do
|
112
|
+
it 'calculates the binomial C0,1 as 1' do
|
113
|
+
1.binomial_by_pascals_triangle(0).should eq(1)
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'calculates the binomial C1,2 as 2' do
|
117
|
+
2.binomial_by_pascals_triangle(1).should eq(2)
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'calculates the binomial C2,3 as 3' do
|
121
|
+
3.binomial_by_pascals_triangle(2).should eq(3)
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'calculates the binomial C2,4 as 6' do
|
125
|
+
4.binomial_by_pascals_triangle(2).should eq(6)
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'calculates the binomial C1030,515 as 285964137299780816375713003417892828985047376491079895916065830932808184515508623336290716027603487838840658378528491902852969839049878057252288352997125670272624219209393564328660099438209732411401014699912025370547761703640450600215234966704145601511388368027664919136794617311260529388699426829733229057400' do
|
129
|
+
1030.binomial_by_pascals_triangle(515).should eq(285964137299780816375713003417892828985047376491079895916065830932808184515508623336290716027603487838840658378528491902852969839049878057252288352997125670272624219209393564328660099438209732411401014699912025370547761703640450600215234966704145601511388368027664919136794617311260529388699426829733229057400)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe Integer, '#breaking_binomial_by_product_of_divisions' do
|
134
|
+
it 'calculates the binomial C0,1 as 1' do
|
135
|
+
1.breaking_binomial_by_product_of_divisions(0).should eq(1)
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'calculates the binomial C1,2 as 2' do
|
139
|
+
2.breaking_binomial_by_product_of_divisions(1).should eq(2)
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'calculates the binomial C2,3 as 3' do
|
143
|
+
3.breaking_binomial_by_product_of_divisions(2).should eq(3)
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'calculates the binomial C2,4 as 6' do
|
147
|
+
4.breaking_binomial_by_product_of_divisions(2).should eq(6)
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'raises an error when calculating the binomial C1030,515' do
|
151
|
+
1030.breaking_binomial_by_product_of_divisions(515).should be_nil
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe Integer, '#float_binomial_by_product_of_divisions' do
|
156
|
+
it 'calculates the binomial C0,1 as 1.0' do
|
157
|
+
1.float_binomial_by_product_of_divisions(0).should eq(1.to_f)
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'calculates the binomial C1,2 as 2.0' do
|
161
|
+
2.float_binomial_by_product_of_divisions(1).should eq(2.to_f)
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'calculates the binomial C2,3 as 3.0' do
|
165
|
+
3.float_binomial_by_product_of_divisions(2).should eq(3.to_f)
|
166
|
+
end
|
167
|
+
|
168
|
+
it 'calculates the binomial C2,4 as 6.0' do
|
169
|
+
4.float_binomial_by_product_of_divisions(2).should eq(6.to_f)
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'calculates the binomial C1030,515 as Float::INFINITY' do
|
173
|
+
1030.float_binomial_by_product_of_divisions(515).should eq(Float::INFINITY)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
describe Integer, '#float_binomial_by_division_of_products' do
|
178
|
+
it 'calculates the binomial C0,1 as 1.0' do
|
179
|
+
1.float_binomial_by_division_of_products(0).should eq(1.to_f)
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'calculates the binomial C1,2 as 2.0' do
|
183
|
+
2.float_binomial_by_division_of_products(1).should eq(2.to_f)
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'calculates the binomial C2,3 as 3.0' do
|
187
|
+
3.float_binomial_by_division_of_products(2).should eq(3.to_f)
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'calculates the binomial C2,4 as 6.0' do
|
191
|
+
4.float_binomial_by_division_of_products(2).should eq(6.to_f)
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'calculates the binomial C270,135 as Float::INFINITY' do
|
195
|
+
270.float_binomial_by_division_of_products(135).should eq(Float::INFINITY)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe Integer, '#large_float_binomial_by_product_of_divisions' do
|
200
|
+
it 'calculates the binomial C0,1 as 1×10⁰' do
|
201
|
+
1.large_float_binomial_by_product_of_divisions(0).should eq(1.to_lf)
|
202
|
+
end
|
203
|
+
|
204
|
+
it 'calculates the binomial C1,2 as 2×10⁰' do
|
205
|
+
2.large_float_binomial_by_product_of_divisions(1).should eq(2.to_lf)
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'calculates the binomial C2,3 as 3×10⁰' do
|
209
|
+
3.large_float_binomial_by_product_of_divisions(2).should eq(3.to_lf)
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'calculates the binomial C2,4 as 6×10⁰' do
|
213
|
+
4.large_float_binomial_by_product_of_divisions(2).should eq(6.to_lf)
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'calculates the binomial C1030,515 as 2.859641372997808×10³⁰⁸' do
|
217
|
+
1030.large_float_binomial_by_product_of_divisions(515).should eq(LargeBinomials::LargeFloat.new(2.859641372997808, 308))
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
describe Integer, '#large_float_binomial_by_division_of_products' do
|
222
|
+
it 'calculates the binomial C0,1 as 1×10⁰' do
|
223
|
+
1.large_float_binomial_by_division_of_products(0).should eq(1.to_lf)
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'calculates the binomial C1,2 as 2×10⁰' do
|
227
|
+
2.large_float_binomial_by_division_of_products(1).should eq(2.to_lf)
|
228
|
+
end
|
229
|
+
|
230
|
+
it 'calculates the binomial C2,3 as 3×10⁰' do
|
231
|
+
3.large_float_binomial_by_division_of_products(2).should eq(3.to_lf)
|
232
|
+
end
|
233
|
+
|
234
|
+
it 'calculates the binomial C2,4 as 6×10⁰' do
|
235
|
+
4.large_float_binomial_by_division_of_products(2).should eq(6.to_lf)
|
236
|
+
end
|
237
|
+
|
238
|
+
it 'calculates the binomial C1030,515 as 2.859641372997804×10³⁰⁸' do
|
239
|
+
1030.large_float_binomial_by_division_of_products(515).should eq(LargeBinomials::LargeFloat.new(2.859641372997804, 308))
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
describe Integer, '#to_lf' do
|
244
|
+
it 'returns a LargeFloat with the Integer as mantissa, and exponent 0' do
|
245
|
+
1.to_lf.should eq(LargeBinomials::LargeFloat.new(1))
|
246
|
+
end
|
247
|
+
end
|
@@ -0,0 +1,281 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Ruby Library to Calculate Large Binomials
|
4
|
+
# Copyright © 2013 Filip van Laenen <f.a.vanlaenen@ieee.org>
|
5
|
+
#
|
6
|
+
# This file is part of the Ruby Library to Calculate Large Binomials.
|
7
|
+
#
|
8
|
+
# The Ruby Library to Calculate Large Binomials is free software: you can
|
9
|
+
# redistribute it and/or modify it under the terms of the GNU General Public
|
10
|
+
# License as published by the Free Software Foundation, either version 3 of the
|
11
|
+
# License, or (at your option) any later version.
|
12
|
+
#
|
13
|
+
# The Ruby Library to Calculate Large Binomials is distributed in the hope that
|
14
|
+
# it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
15
|
+
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
16
|
+
# Public License for more details.
|
17
|
+
#
|
18
|
+
# You can find a copy of the GNU General Public License in /LICENSE
|
19
|
+
#
|
20
|
+
|
21
|
+
require 'spec_helper'
|
22
|
+
|
23
|
+
describe LargeBinomials::LargeFloat, '#initialize' do
|
24
|
+
it "sets the mantissa correctly if there's only one argument" do
|
25
|
+
lf = LargeBinomials::LargeFloat.new(1.to_f)
|
26
|
+
lf.mantissa.should eq(1.to_f)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "sets the exponent to zero if there's only one argument" do
|
30
|
+
lf = LargeBinomials::LargeFloat.new(1.to_f)
|
31
|
+
lf.exponent.should eq(0)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'sets the mantissa correctly if there are two arguments' do
|
35
|
+
lf = LargeBinomials::LargeFloat.new(1.to_f, 0)
|
36
|
+
lf.mantissa.should eq(1.to_f)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'converts the mantissa to a float' do
|
40
|
+
lf = LargeBinomials::LargeFloat.new(1)
|
41
|
+
lf.mantissa.should be_a_kind_of(Float)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe LargeBinomials::LargeFloat, '#multiply_by_numeric' do
|
46
|
+
it 'creates a new LargeFloat with the mantissa multiplied with the argument' do
|
47
|
+
(1.to_lf * 2).mantissa.should eq(2.to_f)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "doesn't change the mantissa of the original LargeFloat" do
|
51
|
+
one = 1.to_lf
|
52
|
+
one * 2
|
53
|
+
one.mantissa.should eq(1.to_f)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "doesn't normalize without overflow (mantissa part)" do
|
57
|
+
big_number = LargeBinomials::LargeFloat.new(1E+100)
|
58
|
+
big_number_squared = big_number * 1E+100
|
59
|
+
big_number_squared.mantissa.should eq(1E+200)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "doesn't normalize without overflow (exponent part)" do
|
63
|
+
big_number = LargeBinomials::LargeFloat.new(1E+100)
|
64
|
+
big_number_squared = big_number * 1E+100
|
65
|
+
big_number_squared.exponent.should eq(0)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'normalizes before overflow (mantissa part)' do
|
69
|
+
big_number = LargeBinomials::LargeFloat.new(2E+300, 3)
|
70
|
+
big_number_squared = big_number * 1E+300
|
71
|
+
big_number_squared.mantissa.should eq(2E+300)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'normalizes before overflow (exponent part)' do
|
75
|
+
big_number = LargeBinomials::LargeFloat.new(2E+300, 3)
|
76
|
+
big_number_squared = big_number * 1E+300
|
77
|
+
big_number_squared.exponent.should eq(303)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "doesn't change the exponent of the original LargeFloat in case of overflow" do
|
81
|
+
big_number = LargeBinomials::LargeFloat.new(1E+300, 3)
|
82
|
+
big_number_squared = big_number * 1E+300
|
83
|
+
big_number.exponent.should eq(3)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe LargeBinomials::LargeFloat, '#multiply_by_large_float' do
|
88
|
+
def create_thirty
|
89
|
+
LargeBinomials::LargeFloat.new(3, 1)
|
90
|
+
end
|
91
|
+
|
92
|
+
def multiply_by_two_hundred(lf)
|
93
|
+
lf * LargeBinomials::LargeFloat.new(2, 2)
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'creates a new LargeFloat with the mantissas multiplied together' do
|
97
|
+
thirty = create_thirty
|
98
|
+
six_thousand = multiply_by_two_hundred(thirty)
|
99
|
+
six_thousand.mantissa.should eq(6.to_f)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'creates a new LargeFloat with the exponents added together' do
|
103
|
+
thirty = create_thirty
|
104
|
+
six_thousand = multiply_by_two_hundred(thirty)
|
105
|
+
six_thousand.exponent.should eq(3)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "doesn't change the mantissa of the original LargeFloat" do
|
109
|
+
thirty = create_thirty
|
110
|
+
six_thousand = multiply_by_two_hundred(thirty)
|
111
|
+
thirty.mantissa.should eq(3.to_f)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "doesn't normalize without overflow (mantissa part)" do
|
115
|
+
big_number = LargeBinomials::LargeFloat.new(1E+100, 10)
|
116
|
+
big_number_squared = big_number * big_number
|
117
|
+
big_number_squared.mantissa.should eq(1E+200)
|
118
|
+
end
|
119
|
+
|
120
|
+
it "doesn't normalize without overflow (exponent part)" do
|
121
|
+
big_number = LargeBinomials::LargeFloat.new(1E+100, 10)
|
122
|
+
big_number_squared = big_number * big_number
|
123
|
+
big_number_squared.exponent.should eq(20)
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'normalizes before overflow (mantissa part)' do
|
127
|
+
big_number = LargeBinomials::LargeFloat.new(2E+300, 3)
|
128
|
+
big_number_squared = big_number * big_number
|
129
|
+
big_number_squared.mantissa.should eq(4E+300)
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'normalizes before overflow (exponent part)' do
|
133
|
+
big_number = LargeBinomials::LargeFloat.new(2E+300, 3)
|
134
|
+
big_number_squared = big_number * big_number
|
135
|
+
big_number_squared.exponent.should eq(306)
|
136
|
+
end
|
137
|
+
|
138
|
+
it "doesn't change the exponent of the original LargeFloat in case of overflow" do
|
139
|
+
big_number = LargeBinomials::LargeFloat.new(1E+300, 3)
|
140
|
+
big_number_squared = big_number * big_number
|
141
|
+
big_number.exponent.should eq(3)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe LargeBinomials::LargeFloat, '#divide_by_numeric' do
|
146
|
+
it 'creates a new LargeFloat with the mantissa divided with the argument' do
|
147
|
+
(1.to_lf / 2).mantissa.should eq(0.5)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "doesn't change the mantissa of the original LargeFloat" do
|
151
|
+
one = 1.to_lf
|
152
|
+
one / 2
|
153
|
+
one.mantissa.should eq(1.to_f)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe LargeBinomials::LargeFloat, '#divide_by_large_float' do
|
158
|
+
def divide_by_large_float_two_hundred(lf)
|
159
|
+
lf / LargeBinomials::LargeFloat.new(2, 2)
|
160
|
+
end
|
161
|
+
|
162
|
+
it "creates a new LargeFloat with the mantissa divided with the argument's mantissa" do
|
163
|
+
quotient = divide_by_large_float_two_hundred(1.to_lf)
|
164
|
+
quotient.mantissa.should eq(0.5)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "creates a new LargeFloat with the exponent decremented with the argument's exponent" do
|
168
|
+
quotient = divide_by_large_float_two_hundred(1.to_lf)
|
169
|
+
quotient.exponent.should eq(-2)
|
170
|
+
end
|
171
|
+
|
172
|
+
it "doesn't change the mantissa of the original LargeFloat" do
|
173
|
+
one = 1.to_lf
|
174
|
+
divide_by_large_float_two_hundred(one)
|
175
|
+
one.mantissa.should eq(1.to_f)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe LargeBinomials::LargeFloat, '#to_s' do
|
180
|
+
it 'converts 1 to a string as 1.0×10⁰' do
|
181
|
+
1.to_lf.to_s.should eq('1.0×10⁰')
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'converts 10 to a string as 1.0×10¹' do
|
185
|
+
10.to_lf.to_s.should eq('1.0×10¹')
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'converts 1¹²³⁴⁵⁶⁷⁸⁹⁰ to a string as 1.0×10¹²³⁴⁵⁶⁷⁸⁹⁰' do
|
189
|
+
LargeBinomials::LargeFloat.new(1, 1234567890).to_s.should eq('1.0×10¹²³⁴⁵⁶⁷⁸⁹⁰')
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
describe LargeBinomials::LargeFloat, '#+' do
|
194
|
+
it 'creates a new LargeFloat with the same value as the first one when 0 is added' do
|
195
|
+
(1.to_lf + 0.to_lf).should eq(1.to_lf)
|
196
|
+
end
|
197
|
+
|
198
|
+
it 'creates a new LargeFloat with the same value as the second one when added to 0' do
|
199
|
+
(0.to_lf + 1.to_lf).should eq(1.to_lf)
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'creates a new LargeFloat with the exponent of the largest LargeFloat if the second exponent is greater' do
|
203
|
+
(1.to_lf + LargeBinomials::LargeFloat.new(1, 1)).exponent.should eq(1)
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'creates a new LargeFloat with the exponent of the largest LargeFloat if the first exponent is greater' do
|
207
|
+
(LargeBinomials::LargeFloat.new(1, 1) + 1.to_lf).exponent.should eq(1)
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'creates a new LargeFloat with the exponent of the largest LargeFloat normalized if the second exponent is greater and in case of overflow' do
|
211
|
+
max_float = LargeBinomials::LargeFloat.new(Float::MAX)
|
212
|
+
max_float_times_ten = LargeBinomials::LargeFloat.new(Float::MAX, 1)
|
213
|
+
max_float_times_eleven = max_float + max_float_times_ten
|
214
|
+
max_float_times_eleven.exponent.should eq(Math.log10(Float::MAX).floor + 1)
|
215
|
+
end
|
216
|
+
|
217
|
+
it 'creates a new LargeFloat with the exponent of the largest LargeFloat normalized if the first exponent is greater and in case of overflow' do
|
218
|
+
max_float = LargeBinomials::LargeFloat.new(Float::MAX)
|
219
|
+
max_float_times_ten = LargeBinomials::LargeFloat.new(Float::MAX, 1)
|
220
|
+
max_float_times_eleven = max_float_times_ten + max_float
|
221
|
+
max_float_times_eleven.exponent.should eq(Math.log10(Float::MAX).floor + 1)
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'creates a new LargeFloat with the mantissa the sum of the mantissas relative to the exponents if the second exponent is greater' do
|
225
|
+
(1.to_lf + LargeBinomials::LargeFloat.new(1, 1)).mantissa.should eq(1.1)
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'creates a new LargeFloat with the mantissa the sum of the mantissas relative to the exponents if the first exponent is greater' do
|
229
|
+
(LargeBinomials::LargeFloat.new(1, 1) + 1.to_lf).mantissa.should eq(1.1)
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'creates a new LargeFloat with the mantissa the sum of the mantissas relative to the exponents normalized if the second exponent is greater and in case of overflow' do
|
233
|
+
max_float = LargeBinomials::LargeFloat.new(Float::MAX)
|
234
|
+
max_float_times_ten = LargeBinomials::LargeFloat.new(Float::MAX, 1)
|
235
|
+
max_float_times_eleven = max_float + max_float_times_ten
|
236
|
+
max_mantissa = Float::MAX / (10 ** Math.log10(Float::MAX).floor)
|
237
|
+
max_float_times_eleven.mantissa.should eq(max_mantissa + max_mantissa / 10)
|
238
|
+
end
|
239
|
+
|
240
|
+
it 'creates a new LargeFloat with the mantissa the sum of the mantissas relative to the exponents normalized if the first exponent is greater and in case of overflow' do
|
241
|
+
max_float = LargeBinomials::LargeFloat.new(Float::MAX)
|
242
|
+
max_float_times_ten = LargeBinomials::LargeFloat.new(Float::MAX, 1)
|
243
|
+
max_float_times_eleven = max_float_times_ten + max_float
|
244
|
+
max_mantissa = Float::MAX / (10 ** Math.log10(Float::MAX).floor)
|
245
|
+
max_float_times_eleven.mantissa.should eq(max_mantissa + max_mantissa / 10)
|
246
|
+
end
|
247
|
+
|
248
|
+
it 'creates a new LargeFloat equal to the first LargeFloat if the second is too small to be added' do
|
249
|
+
below_accuracy = LargeBinomials::LargeFloat.new(1, -Math.log10(Float::MAX).floor - 1)
|
250
|
+
(1.to_lf + below_accuracy).should eq 1.to_lf
|
251
|
+
end
|
252
|
+
|
253
|
+
it 'creates a new LargeFloat equal to the first LargeFloat if the first is too small to be added' do
|
254
|
+
below_accuracy = LargeBinomials::LargeFloat.new(1, -Math.log10(Float::MAX).floor - 1)
|
255
|
+
(below_accuracy + 1.to_lf).should eq 1.to_lf
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
describe LargeBinomials::LargeFloat, '#<=>' do
|
260
|
+
it 'returns 0 (equal) when mantissa and exponent are equal' do
|
261
|
+
(1.to_lf == 1.to_lf).should be_true
|
262
|
+
end
|
263
|
+
|
264
|
+
it 'returns 0 (equal) when mantissa and exponent are equal after normalization' do
|
265
|
+
(LargeBinomials::LargeFloat.new(0.1, 2) == 10.to_lf).should be_true
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'returns -1 (less than) when the first mantissa is smaller than the second, exponents being equal' do
|
269
|
+
(1.to_lf < 2.to_lf).should be_true
|
270
|
+
end
|
271
|
+
|
272
|
+
it 'returns -1 (less than) when the first exponent is smaller than the second' do
|
273
|
+
(1.to_lf < LargeBinomials::LargeFloat.new(1, 1)).should be_true
|
274
|
+
end
|
275
|
+
|
276
|
+
it 'returns -1 (less than) when the first exponent is smaller than the second after normalization' do
|
277
|
+
one = LargeBinomials::LargeFloat.new(0.01, 2)
|
278
|
+
ten = LargeBinomials::LargeFloat.new(1, 1)
|
279
|
+
(one < ten).should be_true
|
280
|
+
end
|
281
|
+
end
|