ruby-mpfr 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -2
- data/Manifest.txt +33 -7
- data/README.rdoc +10 -8
- data/Rakefile +4 -3
- data/ext/{extconf.rb → mpfr/extconf.rb} +4 -0
- data/ext/{ruby_mpfr.c → mpfr/ruby_mpfr.c} +466 -199
- data/ext/{ruby_mpfr.h → mpfr/ruby_mpfr.h} +2 -0
- data/ext/mpfr_matrix/mpfr/extconf.rb +7 -0
- data/ext/mpfr_matrix/mpfr/func_mpfr_matrix.c +524 -0
- data/ext/mpfr_matrix/mpfr/func_mpfr_matrix.h +72 -0
- data/ext/mpfr_matrix/mpfr/ruby_mpfr.h +40 -0
- data/ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c +1056 -0
- data/ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.h +13 -0
- data/lib/mpfr/matrix.rb +145 -0
- data/lib/mpfr/version.rb +3 -0
- data/ruby-mpfr.gemspec +36 -0
- data/spec/mpfr/allocate_spec.rb +60 -0
- data/spec/mpfr/arithmetic_spec.rb +64 -0
- data/spec/mpfr/comparison_spec.rb +21 -0
- data/spec/mpfr/constant_spec.rb +23 -0
- data/spec/mpfr/conversion_spec.rb +14 -0
- data/spec/mpfr/exception_spec.rb +60 -0
- data/spec/mpfr/functions_spec.rb +25 -0
- data/spec/mpfr/generate_number_modulue.rb +44 -0
- data/spec/mpfr/precision_roundmode_spec.rb +65 -0
- data/spec/mpfr/rounding_spec.rb +51 -0
- data/spec/mpfr/set_value_spec.rb +77 -0
- data/spec/mpfr/spec_helper.rb +13 -0
- data/spec/mpfr/string_spec.rb +58 -0
- data/spec/mpfr_matrix/generate_matrix_arguments.rb +55 -0
- data/spec/mpfr_matrix/mpfr_matrix_alloc_spec.rb +126 -0
- data/spec/mpfr_matrix/mpfr_matrix_arithmetic_spec.rb +93 -0
- data/spec/mpfr_matrix/mpfr_matrix_set_element_spec.rb +55 -0
- data/spec/mpfr_matrix/mpfr_matrix_string_spec.rb +31 -0
- data/spec/mpfr_matrix/mpfr_square_matrix_spec.rb +75 -0
- data/spec/mpfr_matrix/spec_helper.rb +16 -0
- data/tasks/extconf.rake +36 -0
- metadata +48 -16
- data/lib/ruby-mpfr.rb +0 -6
- data/spec/ruby-mpfr_spec.rb +0 -11
- data/spec/spec_helper.rb +0 -10
- data/tasks/rspec.rake +0 -21
@@ -0,0 +1,13 @@
|
|
1
|
+
#ifndef RUBY_GMP_MATRIX
|
2
|
+
#define RUBY_GMP_MATRIX
|
3
|
+
|
4
|
+
#include "func_mpfr_matrix.h"
|
5
|
+
|
6
|
+
VALUE r_mpfr_matrix, r_mpfr_square_matrix, r_mpfr_col_vector, r_mpfr_row_vector, r_mpfr_vector_module;
|
7
|
+
|
8
|
+
void r_mpfr_matrix_free(void *ptr);
|
9
|
+
|
10
|
+
void r_mpfr_matrix_suitable_matrix_init (VALUE *other, MPFRMatrix **ptr_other, int row, int column);
|
11
|
+
VALUE r_mpfr_matrix_robj(MPFRMatrix *x);
|
12
|
+
|
13
|
+
#endif
|
data/lib/mpfr/matrix.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'mpfr/matrix.so'
|
2
|
+
|
3
|
+
class MPFR
|
4
|
+
class Matrix
|
5
|
+
def str_ary2_2(format)
|
6
|
+
ary = str_ary(format)
|
7
|
+
tmp = []
|
8
|
+
(0...row_size).each { tmp << [] }
|
9
|
+
i = 0
|
10
|
+
while i < size
|
11
|
+
for j in 0...row_size
|
12
|
+
tmp[j] << ary[i + j]
|
13
|
+
end
|
14
|
+
i += row_size
|
15
|
+
end
|
16
|
+
tmp
|
17
|
+
end
|
18
|
+
|
19
|
+
def str_ary3(format)
|
20
|
+
ary = str_ary(format)
|
21
|
+
tmp = []
|
22
|
+
i = 0
|
23
|
+
while i < size
|
24
|
+
tmp << ary[i...(i + row_size)]
|
25
|
+
i += row_size
|
26
|
+
end
|
27
|
+
tmp
|
28
|
+
end
|
29
|
+
|
30
|
+
def inspect
|
31
|
+
tmp = str_ary2("%.Re")
|
32
|
+
tmp.map!{ |a| "['" + a.join("', '") + "']" }
|
33
|
+
sprintf("#<%s:%x, [%s]>", self.class, __id__, tmp.join(', '))
|
34
|
+
end
|
35
|
+
|
36
|
+
def pretty_print(pp)
|
37
|
+
ary = str_ary2("%.Re")
|
38
|
+
pp.object_group(self) do
|
39
|
+
pp.text(sprintf(':%x, ', __id__))
|
40
|
+
pp.breakable
|
41
|
+
pp.text("[")
|
42
|
+
pp.nest(1) do
|
43
|
+
for j in 0...row_size
|
44
|
+
pp.breakable if j > 0
|
45
|
+
pp.text(%|["#{ary[j][0]}"|)
|
46
|
+
pp.nest(1) do
|
47
|
+
for i in 1...column_size
|
48
|
+
pp.comma_breakable
|
49
|
+
pp.text(%|"#{ary[j][i]}"|)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
pp.text("]")
|
53
|
+
pp.comma_breakable if j < row_size - 1
|
54
|
+
end
|
55
|
+
pp.text("]")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_s(f = "%.Ff", delimiter = ' ')
|
61
|
+
str_ary(f).join(delimiter)
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.create(a)
|
65
|
+
case a
|
66
|
+
when MPFR::Vector
|
67
|
+
if self === a
|
68
|
+
a.dup
|
69
|
+
else
|
70
|
+
self.new(a.to_a)
|
71
|
+
end
|
72
|
+
when MPFR::Matrix
|
73
|
+
if a.column_size == a.row_size
|
74
|
+
if MPFR::SquareMatrix === a
|
75
|
+
a.dup
|
76
|
+
else
|
77
|
+
MPFR::SquareMatrix.new(a.to_a)
|
78
|
+
end
|
79
|
+
else
|
80
|
+
a.dup
|
81
|
+
end
|
82
|
+
when Array
|
83
|
+
if Array == a[0] && a.size == a[0].size
|
84
|
+
MPFR::SquareMatrix.new(a)
|
85
|
+
else
|
86
|
+
self.new(a)
|
87
|
+
end
|
88
|
+
else
|
89
|
+
self.new(a)
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
# ary is two-dimensional Array.
|
95
|
+
def self.suitable_matrix_from_ary(ary)
|
96
|
+
if ary.size == 1
|
97
|
+
RowVector.new(ary[0])
|
98
|
+
elsif ary[0].size == 1
|
99
|
+
ColumnVector.new(ary.inject([]){ |res, val| res << val[0] })
|
100
|
+
elsif ary[0].size == ary.size
|
101
|
+
SquareMatrix.new(ary)
|
102
|
+
else
|
103
|
+
Matrix.new(ary)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
module Vector
|
110
|
+
def inspect
|
111
|
+
sprintf("#<%s:%x, ['%s']>", self.class, __id__, str_ary("%.Re").join("', '"))
|
112
|
+
end
|
113
|
+
|
114
|
+
def pretty_print(pp)
|
115
|
+
ary = str_ary("%.Re")
|
116
|
+
pp.object_group(self) do
|
117
|
+
pp.text(sprintf(':%x, ', __id__))
|
118
|
+
pp.breakable
|
119
|
+
pp.text(%|["#{ary[0]}"|)
|
120
|
+
pp.nest(1) do
|
121
|
+
for i in 1...dim
|
122
|
+
pp.comma_breakable
|
123
|
+
pp.text(%|"#{ary[i]}"|)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
pp.text("]")
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def to_s(format = "%.Re", delimiter = ' ')
|
131
|
+
str_ary(format).join(delimiter)
|
132
|
+
end
|
133
|
+
|
134
|
+
def self.inner_product(a, b)
|
135
|
+
a.inner_product(b)
|
136
|
+
end
|
137
|
+
|
138
|
+
def self.distance(a, b)
|
139
|
+
a.distance_from(b)
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
data/lib/mpfr/version.rb
ADDED
data/ruby-mpfr.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{ruby-mpfr}
|
5
|
+
s.version = "0.0.4"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Takayuki YAMAGUCHI"]
|
9
|
+
s.date = %q{2009-12-17}
|
10
|
+
s.description = %q{ruby-mpfr is library to use MPFR[http://www.mpfr.org/] which is a C library for
|
11
|
+
multiple-precision floating-point computations.}
|
12
|
+
s.email = ["d@ytak.info"]
|
13
|
+
s.extensions = ["ext/mpfr/extconf.rb", "ext/mpfr_matrix/mpfr/extconf.rb"]
|
14
|
+
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc"]
|
15
|
+
s.files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "ext/mpfr/extconf.rb", "ext/mpfr/ruby_mpfr.c", "ext/mpfr/ruby_mpfr.h", "ext/mpfr_matrix/mpfr/extconf.rb", "ext/mpfr_matrix/mpfr/func_mpfr_matrix.c", "ext/mpfr_matrix/mpfr/func_mpfr_matrix.h", "ext/mpfr_matrix/mpfr/ruby_mpfr.h", "ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.c", "ext/mpfr_matrix/mpfr/ruby_mpfr_matrix.h", "lib/mpfr/matrix.rb", "lib/mpfr/version.rb", "ruby-mpfr.gemspec", "script/console", "script/destroy", "script/generate", "spec/mpfr/allocate_spec.rb", "spec/mpfr/arithmetic_spec.rb", "spec/mpfr/comparison_spec.rb", "spec/mpfr/constant_spec.rb", "spec/mpfr/conversion_spec.rb", "spec/mpfr/exception_spec.rb", "spec/mpfr/functions_spec.rb", "spec/mpfr/generate_number_modulue.rb", "spec/mpfr/precision_roundmode_spec.rb", "spec/mpfr/rounding_spec.rb", "spec/mpfr/set_value_spec.rb", "spec/mpfr/spec_helper.rb", "spec/mpfr/string_spec.rb", "spec/mpfr_matrix/generate_matrix_arguments.rb", "spec/mpfr_matrix/mpfr_matrix_alloc_spec.rb", "spec/mpfr_matrix/mpfr_matrix_arithmetic_spec.rb", "spec/mpfr_matrix/mpfr_matrix_set_element_spec.rb", "spec/mpfr_matrix/mpfr_matrix_string_spec.rb", "spec/mpfr_matrix/mpfr_square_matrix_spec.rb", "spec/mpfr_matrix/spec_helper.rb", "spec/spec.opts", "tasks/extconf.rake"]
|
16
|
+
s.homepage = %q{http://rubyforge.org/projects/ruby-mpfr/}
|
17
|
+
s.post_install_message = %q{PostInstall.txt}
|
18
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
19
|
+
s.require_paths = ["lib", "ext"]
|
20
|
+
s.rubyforge_project = %q{ruby-mpfr}
|
21
|
+
s.rubygems_version = %q{1.3.5}
|
22
|
+
s.summary = %q{ruby-mpfr is library to use MPFR[http://www.mpfr.org/] which is a C library for multiple-precision floating-point computations.}
|
23
|
+
|
24
|
+
if s.respond_to? :specification_version then
|
25
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
26
|
+
s.specification_version = 3
|
27
|
+
|
28
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
29
|
+
s.add_development_dependency(%q<hoe>, [">= 2.4.0"])
|
30
|
+
else
|
31
|
+
s.add_dependency(%q<hoe>, [">= 2.4.0"])
|
32
|
+
end
|
33
|
+
else
|
34
|
+
s.add_dependency(%q<hoe>, [">= 2.4.0"])
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe MPFR, 'when allocating objects from integer' do
|
4
|
+
it "should equal bignum" do
|
5
|
+
MPFR.set_default_prec(100)
|
6
|
+
num = 1
|
7
|
+
for i in 0..300
|
8
|
+
num *= 10
|
9
|
+
MPFR.new(num).should == num
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should allocate from string" do
|
14
|
+
MPFR.set_default_prec(53)
|
15
|
+
for i in 0..1000
|
16
|
+
MPFR.new(i.to_s).should == i
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should equal float" do
|
21
|
+
MPFR.set_default_prec(256)
|
22
|
+
error = MPFR.new('1.0e-10')
|
23
|
+
GenerateNumber.float(100) do |f|
|
24
|
+
MPFR.new(f).should == f
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should duplicate" do
|
29
|
+
MPFR.set_default_prec(141)
|
30
|
+
for i in 0..1000
|
31
|
+
a = rand(10**10)
|
32
|
+
b = rand(10**50)
|
33
|
+
str = "#{a}.#{b}"
|
34
|
+
c = MPFR.new(str)
|
35
|
+
d = c.dup
|
36
|
+
c.should == d
|
37
|
+
c.object_id.should_not == d.object_id
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should coerce" do
|
42
|
+
MPFR.set_default_prec(89)
|
43
|
+
a = MPFR.new('0.253242')
|
44
|
+
for i in 0..1000
|
45
|
+
b = MPFR.new(i)
|
46
|
+
c = a.coerce(i)[0]
|
47
|
+
b.should == c
|
48
|
+
end
|
49
|
+
|
50
|
+
a = MPFR.new('-13572834.923')
|
51
|
+
for i in 0..1000
|
52
|
+
str = (i + rand).to_s
|
53
|
+
b = MPFR.new(str)
|
54
|
+
c = a.coerce(str)[0]
|
55
|
+
b.should == c
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
MPFR.set_default_prec(256)
|
4
|
+
|
5
|
+
def check_arithmetic_op(arg_ary, error, &block)
|
6
|
+
ruby_res = yield(*arg_ary)
|
7
|
+
mpfr_res = yield(*arg_ary.map{ |a| MPFR.new(a) })
|
8
|
+
mpfr_res = MPFR.new(mpfr_res) unless MPFR === mpfr_res
|
9
|
+
(ruby_res - mpfr_res).abs.should < error
|
10
|
+
end
|
11
|
+
|
12
|
+
describe MPFR, 'when calculating MPFR' do
|
13
|
+
|
14
|
+
it "should equal the result of built-in Numeric instance" do
|
15
|
+
error = 1.0e-10
|
16
|
+
arg = GenerateNumber.float_arguments(1, 1000)
|
17
|
+
arg << [0]
|
18
|
+
arg.each do |ary|
|
19
|
+
check_arithmetic_op(ary, error){ |a| a + 4 }
|
20
|
+
check_arithmetic_op(ary, error){ |a| 4 + a }
|
21
|
+
check_arithmetic_op(ary, error){ |a| a - 3 }
|
22
|
+
check_arithmetic_op(ary, error){ |a| 3 - a }
|
23
|
+
check_arithmetic_op(ary, error){ |a| a * 7 }
|
24
|
+
check_arithmetic_op(ary, error){ |a| 7 * a }
|
25
|
+
check_arithmetic_op(ary, error){ |a| a / 2 }
|
26
|
+
check_arithmetic_op(ary, error){ |a| (a.zero? ? 0 : 7 / a) }
|
27
|
+
|
28
|
+
check_arithmetic_op(ary, error){ |a| a + 4.0 }
|
29
|
+
check_arithmetic_op(ary, error){ |a| 4.0 + a }
|
30
|
+
check_arithmetic_op(ary, error){ |a| a - 3.0 }
|
31
|
+
check_arithmetic_op(ary, error){ |a| 3.0 - a }
|
32
|
+
check_arithmetic_op(ary, error){ |a| a * 7.0 }
|
33
|
+
check_arithmetic_op(ary, error){ |a| 7.0 * a }
|
34
|
+
check_arithmetic_op(ary, error){ |a| a / 2.0 }
|
35
|
+
check_arithmetic_op(ary, error){ |a| (a.zero? ? 0 : 7.0 / a) }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should equal the result of built-in Numeric instance" do
|
40
|
+
error = 1.0e-10
|
41
|
+
args = GenerateNumber.float_arguments(2, 1000)
|
42
|
+
args += [[0.0, 0.0], [0, -2.2], [0.0, 2.9], [1.1, 1000], [-23.1, 8.7]]
|
43
|
+
|
44
|
+
args.each do |ary|
|
45
|
+
check_arithmetic_op(ary, error){ |a, b| a + b }
|
46
|
+
check_arithmetic_op(ary, error){ |a, b| a - b }
|
47
|
+
check_arithmetic_op(ary, error){ |a, b| a * b }
|
48
|
+
check_arithmetic_op(ary, error){ |a, b| (b.zero? ? 0 : a / b) }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should equal the result of built-in Numeric instance" do
|
53
|
+
error = 1.0e-10
|
54
|
+
args = GenerateNumber.float_arguments(3, 1000)
|
55
|
+
args += [[0.0, 0.0, 0.0], [0, 0.0, -2.2], [0.0, 2.9, 0.0], [1.1, 1000, 0.0], [-23.1, 8.7, 0.0]]
|
56
|
+
|
57
|
+
args.each do |ary|
|
58
|
+
check_arithmetic_op(ary, error){ |a, b, c| a * a - b * b + c * c }
|
59
|
+
check_arithmetic_op(ary, error){ |a, b, c| (c.zero? ? (a + b * (-0.3) - c) / 3 : (a + b * (-0.3) - c) / (3 * c)) }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
MPFR.set_default_prec(256)
|
4
|
+
|
5
|
+
def check_comparison(a, b)
|
6
|
+
((a < b) == (MPFR.new(a) < MPFR.new(b))).should be_true
|
7
|
+
((a > b) == (MPFR.new(a) > MPFR.new(b))).should be_true
|
8
|
+
((a == b) == (MPFR.new(a) == MPFR.new(b))).should be_true
|
9
|
+
end
|
10
|
+
|
11
|
+
describe MPFR, "when comparing two numbers" do
|
12
|
+
it "should return the same result to built-in Numeric comparison" do
|
13
|
+
args = GenerateNumber.float_arguments(2, 1000)
|
14
|
+
args += [[0.0, 0.0], [-2.89, -2.89], [5.2, 2.9], [1000, 1000], [8.7, 8.7]]
|
15
|
+
|
16
|
+
args.each do |ary|
|
17
|
+
check_comparison(*ary)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe MPFR, "when calling constant of MPFR" do
|
4
|
+
it "should be defined" do
|
5
|
+
MPFR::MPFR_VERSION.should be_true
|
6
|
+
MPFR::MPFR_PATCHES.should be_true
|
7
|
+
MPFR::MPFR_VERSION2.should be_true
|
8
|
+
MPFR::MPFR_VERSION_MAJOR.should be_true
|
9
|
+
MPFR::MPFR_VERSION_MINOR.should be_true
|
10
|
+
MPFR::MPFR_VERSION_PATCHLEVEL.should be_true
|
11
|
+
MPFR::MPFR_VERSION_STRING.should be_true
|
12
|
+
MPFR::PREC_MAX.should be_true
|
13
|
+
MPFR::PREC_MIN.should be_true
|
14
|
+
MPFR::EMAX_DEFAULT.should be_true
|
15
|
+
MPFR::EMIN_DEFAULT.should be_true
|
16
|
+
MPFR::RNDN.should be_true
|
17
|
+
MPFR::RNDZ.should be_true
|
18
|
+
MPFR::RNDU.should be_true
|
19
|
+
MPFR::RNDD.should be_true
|
20
|
+
# MPFR::VERSION
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
MPFR.set_default_prec(90)
|
4
|
+
|
5
|
+
describe MPFR, 'when converting instance of other class' do
|
6
|
+
it "should be transformed to float" do
|
7
|
+
num = GenerateNumber.float(1000, 100) + [0.0]
|
8
|
+
num.each do |a|
|
9
|
+
b = MPFR.new(a).get_d(MPFR::RNDZ)
|
10
|
+
a.should == b
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe MPFR, "when executing methods which are exception related functions" do
|
4
|
+
it "should get and set emin and emax" do
|
5
|
+
(emin = MPFR.get_emin).should be_a_kind_of Integer
|
6
|
+
(emax = MPFR.get_emax).should be_a_kind_of Integer
|
7
|
+
test_num = 1000
|
8
|
+
MPFR.set_emin(-test_num)
|
9
|
+
MPFR.get_emin.should == -test_num
|
10
|
+
MPFR.set_emax(test_num)
|
11
|
+
MPFR.get_emax.should == test_num
|
12
|
+
MPFR.set_emin(emin)
|
13
|
+
MPFR.set_emax(emax)
|
14
|
+
MPFR.get_emin_min.should be_a_kind_of Integer
|
15
|
+
MPFR.get_emin_max.should be_a_kind_of Integer
|
16
|
+
MPFR.get_emax_min.should be_a_kind_of Integer
|
17
|
+
MPFR.get_emax_max.should be_a_kind_of Integer
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe MPFR, "when executing exception of MPFR" do
|
22
|
+
it "should set exception" do
|
23
|
+
MPFR.set_underflow
|
24
|
+
MPFR.set_overflow
|
25
|
+
MPFR.set_nanflag
|
26
|
+
MPFR.set_inexflag
|
27
|
+
MPFR.set_erangeflag
|
28
|
+
MPFR.underflow_p.should be_true
|
29
|
+
MPFR.overflow_p.should be_true
|
30
|
+
MPFR.nanflag_p.should be_true
|
31
|
+
MPFR.inexflag_p.should be_true
|
32
|
+
MPFR.erangeflag_p.should be_true
|
33
|
+
MPFR.clear_underflow
|
34
|
+
MPFR.clear_overflow
|
35
|
+
MPFR.clear_nanflag
|
36
|
+
MPFR.clear_inexflag
|
37
|
+
MPFR.clear_erangeflag
|
38
|
+
MPFR.underflow_p.should be_false
|
39
|
+
MPFR.overflow_p.should be_false
|
40
|
+
MPFR.nanflag_p.should be_false
|
41
|
+
MPFR.inexflag_p.should be_false
|
42
|
+
MPFR.erangeflag_p.should be_false
|
43
|
+
|
44
|
+
MPFR.set_underflow
|
45
|
+
MPFR.set_overflow
|
46
|
+
MPFR.set_nanflag
|
47
|
+
MPFR.set_inexflag
|
48
|
+
MPFR.set_erangeflag
|
49
|
+
MPFR.clear_flags
|
50
|
+
MPFR.underflow_p.should be_false
|
51
|
+
MPFR.overflow_p.should be_false
|
52
|
+
MPFR.nanflag_p.should be_false
|
53
|
+
MPFR.inexflag_p.should be_false
|
54
|
+
MPFR.erangeflag_p.should be_false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Need to spec for the following methods.
|
59
|
+
# check_range
|
60
|
+
# subnormalize
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
MPFR.set_default_prec(256)
|
4
|
+
|
5
|
+
describe MPFR, 'when executing mathematical functions' do
|
6
|
+
it "should calculate basic functions" do
|
7
|
+
error = MPFR.new('1.0e-10')
|
8
|
+
a = MPFR::Math.const_pi
|
9
|
+
(MPFR.new(Math::PI) - a).abs.should < error
|
10
|
+
(MPFR::Math.sin(a) - MPFR.new('0')).abs.should < error
|
11
|
+
(MPFR::Math.cos(a) - MPFR.new('-1')).abs.should < error
|
12
|
+
a = MPFR.new(Math::PI/4.0)
|
13
|
+
(MPFR::Math.sin(a) - MPFR.new(Math.sqrt(2.0)/2.0)).abs.should < error
|
14
|
+
(MPFR::Math.cos(a) - MPFR.new(Math.sqrt(2.0)/2.0)).abs.should < error
|
15
|
+
(MPFR::Math.tan(a) - MPFR.new(1)).abs.should < error
|
16
|
+
|
17
|
+
(MPFR::Math.atan(MPFR.new(1) / MPFR::Math.sqrt('3')) - MPFR::Math.const_pi / MPFR.new(6)).abs.should < error
|
18
|
+
|
19
|
+
a = MPFR::Math.exp(1)
|
20
|
+
(Math::E - a).abs.should < error
|
21
|
+
(MPFR.new(1) - MPFR::Math.log(a)).abs.should < error
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module GenerateNumber
|
2
|
+
|
3
|
+
def self.float_arguments(size, number, max = 100)
|
4
|
+
ret = Array.new(number){ |j| Array.new(size){ |i| rand(max) - rand } }
|
5
|
+
for i in 0...(ret.size)
|
6
|
+
case i % 4
|
7
|
+
when 1
|
8
|
+
ret[i].map!{ |a| -a }
|
9
|
+
when 2, 3
|
10
|
+
ret[i].map!{ |a| (rand > 0.5 ? -a : a) }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
ret
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.float(num, max = 100, &block)
|
17
|
+
if block_given?
|
18
|
+
for i in 0...num
|
19
|
+
a = rand(max) - rand
|
20
|
+
a = -a if rand > 0.5
|
21
|
+
yield(a)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
ary = Array.new(num){ |i| rand(max) - rand }
|
25
|
+
ary.map!{ |a| (rand > 0.5 ? -a : a) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.string(number)
|
30
|
+
prec = MPFR.get_default_prec
|
31
|
+
max = 2 ** prec
|
32
|
+
Array.new(number) do |i|
|
33
|
+
sign = ((rand > 0.5 ? '-' : ''))
|
34
|
+
"#{sign}#{rand(max)}.#{rand(max)}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.mpfr_args(number)
|
39
|
+
ret = self.string(number)
|
40
|
+
ret.map!{ |a| MPFR.new(a) }
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
def check_rnd(a)
|
4
|
+
MPFR.new(a).should == MPFR.new(a, MPFR.get_default_rounding_mode)
|
5
|
+
n = MPFR.new(a, MPFR::RNDN)
|
6
|
+
z = MPFR.new(a, MPFR::RNDZ)
|
7
|
+
u = MPFR.new(a, MPFR::RNDU)
|
8
|
+
d = MPFR.new(a, MPFR::RNDD)
|
9
|
+
(n > 0 ? d : u).should == z
|
10
|
+
[(a - z).abs, (a - u).abs, (a - d).abs].min.should >= (a - n).abs
|
11
|
+
(d <= u && (n == z || n == u || n == d)).should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
def check_set_rnd(rnd1, rnd2)
|
15
|
+
MPFR.set_default_rounding_mode(rnd1)
|
16
|
+
MPFR.get_default_rounding_mode.should == rnd1
|
17
|
+
MPFR.set_default_rounding_mode(rnd2)
|
18
|
+
MPFR.get_default_rounding_mode.should_not == rnd1
|
19
|
+
MPFR.get_default_rounding_mode.should == rnd2
|
20
|
+
end
|
21
|
+
|
22
|
+
describe MPFR, 'when getting rounding mode' do
|
23
|
+
it "should return rounding mode" do
|
24
|
+
args = GenerateNumber.float(100)
|
25
|
+
args += [Math::PI, -Math::PI, Math::E, -Math::E, 0, 10, 27, -9, -293578]
|
26
|
+
args.each{ |a| check_rnd(a) }
|
27
|
+
[MPFR::RNDN, MPFR::RNDZ, MPFR::RNDU, MPFR::RNDD].each{ |rnd| args.each{ |a| check_rnd(a) } }
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should be set rounding mode" do
|
31
|
+
rnds = [MPFR::RNDN, MPFR::RNDZ, MPFR::RNDU, MPFR::RNDD]
|
32
|
+
for i in 0...rnds.size
|
33
|
+
for j in (i+1)...rnds.size
|
34
|
+
check_set_rnd(rnds[i], rnds[j])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def check_set_prec(prec)
|
41
|
+
MPFR.set_default_prec(prec)
|
42
|
+
MPFR.get_default_prec.should == prec
|
43
|
+
a = MPFR.new(rand(10000) - rand)
|
44
|
+
MPFR.get_default_prec.should == a.get_prec
|
45
|
+
prec2 = prec / 2
|
46
|
+
a.set_prec(prec2)
|
47
|
+
a.nan?.should be_true
|
48
|
+
MPFR.get_default_prec.should_not == a.get_prec
|
49
|
+
prec2.should == a.get_prec
|
50
|
+
b = MPFR.new(rand(10000) - rand)
|
51
|
+
prec3 = prec * 3
|
52
|
+
MPFR.set_default_prec(prec3)
|
53
|
+
c = MPFR.new(rand(10000) - rand)
|
54
|
+
prec3.should == c.get_prec
|
55
|
+
c.get_prec.should_not == b.get_prec
|
56
|
+
end
|
57
|
+
|
58
|
+
describe MPFR, 'when setting precision' do
|
59
|
+
it "should be set precision" do
|
60
|
+
for i in 1...1000
|
61
|
+
check_set_prec(20 + rand(i))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe MPFR, "when rounding number by prec_round" do
|
4
|
+
before(:all) do
|
5
|
+
MPFR.set_default_prec(200)
|
6
|
+
@prec = 100
|
7
|
+
@args = GenerateNumber.float(1000).map{ |a| MPFR.new(a.to_s) }
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be rounded up" do
|
11
|
+
@args.each do |a|
|
12
|
+
a.prec_round(MPFR::RNDU, @prec).should >= a
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should be rounded down" do
|
17
|
+
@args.each do |a|
|
18
|
+
a.prec_round(MPFR::RNDD, @prec).should <= a
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be rounded to zero" do
|
23
|
+
@args.each do |a|
|
24
|
+
if a > 0
|
25
|
+
a.prec_round(MPFR::RNDZ, @prec).should <= a
|
26
|
+
else
|
27
|
+
a.prec_round(MPFR::RNDZ, @prec).should >= a
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should be rounded to near number" do
|
33
|
+
@args.each do |a|
|
34
|
+
min = [(a - a.prec_round(MPFR::RNDU, @prec)).abs, (a - a.prec_round(MPFR::RNDD, @prec)).abs].min
|
35
|
+
(a - a.prec_round(MPFR::RNDN, @prec)).abs.should eql min
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should be rounded by prec_round!" do
|
40
|
+
@args.each do |a|
|
41
|
+
[MPFR::RNDU, MPFR::RNDD, MPFR::RNDN, MPFR::RNDZ].each do |rnd|
|
42
|
+
b = a.dup
|
43
|
+
old_object_id = b.object_id
|
44
|
+
b.prec_round!(rnd, @prec)
|
45
|
+
b.should eql a.prec_round(rnd, @prec)
|
46
|
+
b.object_id.should eql old_object_id
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|