bigdecimal 2.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.
- checksums.yaml +7 -0
- data/bigdecimal.gemspec +39 -0
- data/ext/bigdecimal/bigdecimal.c +6617 -0
- data/ext/bigdecimal/bigdecimal.h +396 -0
- data/ext/bigdecimal/extconf.rb +55 -0
- data/lib/bigdecimal.rb +1 -0
- data/lib/bigdecimal/jacobian.rb +90 -0
- data/lib/bigdecimal/ludcmp.rb +89 -0
- data/lib/bigdecimal/math.rb +232 -0
- data/lib/bigdecimal/newton.rb +80 -0
- data/lib/bigdecimal/util.rb +181 -0
- data/sample/linear.rb +74 -0
- data/sample/nlsolve.rb +40 -0
- data/sample/pi.rb +21 -0
- metadata +116 -0
@@ -0,0 +1,181 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# bigdecimal/util extends various native classes to provide the #to_d method,
|
5
|
+
# and provides BigDecimal#to_d and BigDecimal#to_digits.
|
6
|
+
#++
|
7
|
+
|
8
|
+
require 'bigdecimal'
|
9
|
+
|
10
|
+
class Integer < Numeric
|
11
|
+
# call-seq:
|
12
|
+
# int.to_d -> bigdecimal
|
13
|
+
#
|
14
|
+
# Returns the value of +int+ as a BigDecimal.
|
15
|
+
#
|
16
|
+
# require 'bigdecimal'
|
17
|
+
# require 'bigdecimal/util'
|
18
|
+
#
|
19
|
+
# 42.to_d # => 0.42e2
|
20
|
+
#
|
21
|
+
# See also BigDecimal::new.
|
22
|
+
#
|
23
|
+
def to_d
|
24
|
+
BigDecimal(self)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
class Float < Numeric
|
30
|
+
# call-seq:
|
31
|
+
# float.to_d -> bigdecimal
|
32
|
+
# float.to_d(precision) -> bigdecimal
|
33
|
+
#
|
34
|
+
# Returns the value of +float+ as a BigDecimal.
|
35
|
+
# The +precision+ parameter is used to determine the number of
|
36
|
+
# significant digits for the result (the default is Float::DIG).
|
37
|
+
#
|
38
|
+
# require 'bigdecimal'
|
39
|
+
# require 'bigdecimal/util'
|
40
|
+
#
|
41
|
+
# 0.5.to_d # => 0.5e0
|
42
|
+
# 1.234.to_d(2) # => 0.12e1
|
43
|
+
#
|
44
|
+
# See also BigDecimal::new.
|
45
|
+
#
|
46
|
+
def to_d(precision=Float::DIG)
|
47
|
+
BigDecimal(self, precision)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
class String
|
53
|
+
# call-seq:
|
54
|
+
# str.to_d -> bigdecimal
|
55
|
+
#
|
56
|
+
# Returns the result of interpreting leading characters in +str+
|
57
|
+
# as a BigDecimal.
|
58
|
+
#
|
59
|
+
# require 'bigdecimal'
|
60
|
+
# require 'bigdecimal/util'
|
61
|
+
#
|
62
|
+
# "0.5".to_d # => 0.5e0
|
63
|
+
# "123.45e1".to_d # => 0.12345e4
|
64
|
+
# "45.67 degrees".to_d # => 0.4567e2
|
65
|
+
#
|
66
|
+
# See also BigDecimal::new.
|
67
|
+
#
|
68
|
+
def to_d
|
69
|
+
BigDecimal.interpret_loosely(self)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
class BigDecimal < Numeric
|
75
|
+
# call-seq:
|
76
|
+
# a.to_digits -> string
|
77
|
+
#
|
78
|
+
# Converts a BigDecimal to a String of the form "nnnnnn.mmm".
|
79
|
+
# This method is deprecated; use BigDecimal#to_s("F") instead.
|
80
|
+
#
|
81
|
+
# require 'bigdecimal/util'
|
82
|
+
#
|
83
|
+
# d = BigDecimal("3.14")
|
84
|
+
# d.to_digits # => "3.14"
|
85
|
+
#
|
86
|
+
def to_digits
|
87
|
+
if self.nan? || self.infinite? || self.zero?
|
88
|
+
self.to_s
|
89
|
+
else
|
90
|
+
i = self.to_i.to_s
|
91
|
+
_,f,_,z = self.frac.split
|
92
|
+
i + "." + ("0"*(-z)) + f
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# call-seq:
|
97
|
+
# a.to_d -> bigdecimal
|
98
|
+
#
|
99
|
+
# Returns self.
|
100
|
+
#
|
101
|
+
# require 'bigdecimal/util'
|
102
|
+
#
|
103
|
+
# d = BigDecimal("3.14")
|
104
|
+
# d.to_d # => 0.314e1
|
105
|
+
#
|
106
|
+
def to_d
|
107
|
+
self
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
class Rational < Numeric
|
113
|
+
# call-seq:
|
114
|
+
# rat.to_d(precision) -> bigdecimal
|
115
|
+
#
|
116
|
+
# Returns the value as a BigDecimal.
|
117
|
+
#
|
118
|
+
# The required +precision+ parameter is used to determine the number of
|
119
|
+
# significant digits for the result.
|
120
|
+
#
|
121
|
+
# require 'bigdecimal'
|
122
|
+
# require 'bigdecimal/util'
|
123
|
+
#
|
124
|
+
# Rational(22, 7).to_d(3) # => 0.314e1
|
125
|
+
#
|
126
|
+
# See also BigDecimal::new.
|
127
|
+
#
|
128
|
+
def to_d(precision)
|
129
|
+
BigDecimal(self, precision)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
class Complex < Numeric
|
135
|
+
# call-seq:
|
136
|
+
# cmp.to_d -> bigdecimal
|
137
|
+
# cmp.to_d(precision) -> bigdecimal
|
138
|
+
#
|
139
|
+
# Returns the value as a BigDecimal.
|
140
|
+
#
|
141
|
+
# The +precision+ parameter is required for a rational complex number.
|
142
|
+
# This parameter is used to determine the number of significant digits
|
143
|
+
# for the result.
|
144
|
+
#
|
145
|
+
# require 'bigdecimal'
|
146
|
+
# require 'bigdecimal/util'
|
147
|
+
#
|
148
|
+
# Complex(0.1234567, 0).to_d(4) # => 0.1235e0
|
149
|
+
# Complex(Rational(22, 7), 0).to_d(3) # => 0.314e1
|
150
|
+
#
|
151
|
+
# See also BigDecimal::new.
|
152
|
+
#
|
153
|
+
def to_d(*args)
|
154
|
+
BigDecimal(self) unless self.imag.zero? # to raise eerror
|
155
|
+
|
156
|
+
if args.length == 0
|
157
|
+
case self.real
|
158
|
+
when Rational
|
159
|
+
BigDecimal(self.real) # to raise error
|
160
|
+
end
|
161
|
+
end
|
162
|
+
self.real.to_d(*args)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
class NilClass
|
168
|
+
# call-seq:
|
169
|
+
# nil.to_d -> bigdecimal
|
170
|
+
#
|
171
|
+
# Returns nil represented as a BigDecimal.
|
172
|
+
#
|
173
|
+
# require 'bigdecimal'
|
174
|
+
# require 'bigdecimal/util'
|
175
|
+
#
|
176
|
+
# nil.to_d # => 0.0
|
177
|
+
#
|
178
|
+
def to_d
|
179
|
+
BigDecimal(0)
|
180
|
+
end
|
181
|
+
end
|
data/sample/linear.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
# frozen_string_literal: false
|
3
|
+
|
4
|
+
#
|
5
|
+
# linear.rb
|
6
|
+
#
|
7
|
+
# Solves linear equation system(A*x = b) by LU decomposition method.
|
8
|
+
# where A is a coefficient matrix,x is an answer vector,b is a constant vector.
|
9
|
+
#
|
10
|
+
# USAGE:
|
11
|
+
# ruby linear.rb [input file solved]
|
12
|
+
#
|
13
|
+
|
14
|
+
# :stopdoc:
|
15
|
+
require "bigdecimal"
|
16
|
+
require "bigdecimal/ludcmp"
|
17
|
+
|
18
|
+
#
|
19
|
+
# NOTE:
|
20
|
+
# Change following BigDecimal.limit() if needed.
|
21
|
+
BigDecimal.limit(100)
|
22
|
+
#
|
23
|
+
|
24
|
+
include LUSolve
|
25
|
+
def rd_order(na)
|
26
|
+
printf("Number of equations ?") if(na <= 0)
|
27
|
+
n = ARGF.gets().to_i
|
28
|
+
end
|
29
|
+
|
30
|
+
na = ARGV.size
|
31
|
+
zero = BigDecimal("0.0")
|
32
|
+
one = BigDecimal("1.0")
|
33
|
+
|
34
|
+
while (n=rd_order(na))>0
|
35
|
+
a = []
|
36
|
+
as= []
|
37
|
+
b = []
|
38
|
+
if na <= 0
|
39
|
+
# Read data from console.
|
40
|
+
printf("\nEnter coefficient matrix element A[i,j]\n")
|
41
|
+
for i in 0...n do
|
42
|
+
for j in 0...n do
|
43
|
+
printf("A[%d,%d]? ",i,j); s = ARGF.gets
|
44
|
+
a << BigDecimal(s)
|
45
|
+
as << BigDecimal(s)
|
46
|
+
end
|
47
|
+
printf("Contatant vector element b[%d] ? ",i)
|
48
|
+
b << BigDecimal(ARGF.gets)
|
49
|
+
end
|
50
|
+
else
|
51
|
+
# Read data from specified file.
|
52
|
+
printf("Coefficient matrix and constant vector.\n")
|
53
|
+
for i in 0...n do
|
54
|
+
s = ARGF.gets
|
55
|
+
printf("%d) %s",i,s)
|
56
|
+
s = s.split
|
57
|
+
for j in 0...n do
|
58
|
+
a << BigDecimal(s[j])
|
59
|
+
as << BigDecimal(s[j])
|
60
|
+
end
|
61
|
+
b << BigDecimal(s[n])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
x = lusolve(a,b,ludecomp(a,n,zero,one),zero)
|
65
|
+
printf("Answer(x[i] & (A*x-b)[i]) follows\n")
|
66
|
+
for i in 0...n do
|
67
|
+
printf("x[%d]=%s ",i,x[i].to_s)
|
68
|
+
s = zero
|
69
|
+
for j in 0...n do
|
70
|
+
s = s + as[i*n+j]*x[j]
|
71
|
+
end
|
72
|
+
printf(" & %s\n",(s-b[i]).to_s)
|
73
|
+
end
|
74
|
+
end
|
data/sample/nlsolve.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
# frozen_string_literal: false
|
3
|
+
|
4
|
+
#
|
5
|
+
# nlsolve.rb
|
6
|
+
# An example for solving nonlinear algebraic equation system.
|
7
|
+
#
|
8
|
+
|
9
|
+
require "bigdecimal"
|
10
|
+
require "bigdecimal/newton"
|
11
|
+
include Newton
|
12
|
+
|
13
|
+
class Function # :nodoc: all
|
14
|
+
def initialize()
|
15
|
+
@zero = BigDecimal("0.0")
|
16
|
+
@one = BigDecimal("1.0")
|
17
|
+
@two = BigDecimal("2.0")
|
18
|
+
@ten = BigDecimal("10.0")
|
19
|
+
@eps = BigDecimal("1.0e-16")
|
20
|
+
end
|
21
|
+
def zero;@zero;end
|
22
|
+
def one ;@one ;end
|
23
|
+
def two ;@two ;end
|
24
|
+
def ten ;@ten ;end
|
25
|
+
def eps ;@eps ;end
|
26
|
+
def values(x) # <= defines functions solved
|
27
|
+
f = []
|
28
|
+
f1 = x[0]*x[0] + x[1]*x[1] - @two # f1 = x**2 + y**2 - 2 => 0
|
29
|
+
f2 = x[0] - x[1] # f2 = x - y => 0
|
30
|
+
f <<= f1
|
31
|
+
f <<= f2
|
32
|
+
f
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
f = BigDecimal.limit(100)
|
37
|
+
f = Function.new
|
38
|
+
x = [f.zero,f.zero] # Initial values
|
39
|
+
n = nlsolve(f,x)
|
40
|
+
p x
|
data/sample/pi.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
# frozen_string_literal: false
|
3
|
+
|
4
|
+
#
|
5
|
+
# pi.rb
|
6
|
+
#
|
7
|
+
# Calculates 3.1415.... (the number of times that a circle's diameter
|
8
|
+
# will fit around the circle) using J. Machin's formula.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "bigdecimal"
|
12
|
+
require "bigdecimal/math.rb"
|
13
|
+
|
14
|
+
include BigMath
|
15
|
+
|
16
|
+
if ARGV.size == 1
|
17
|
+
print "PI("+ARGV[0]+"):\n"
|
18
|
+
p PI(ARGV[0].to_i)
|
19
|
+
else
|
20
|
+
print "TRY: ruby pi.rb 1000 \n"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bigdecimal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kenta Murata
|
8
|
+
- Zachary Scott
|
9
|
+
- Shigeo Kobayashi
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2019-12-26 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rake
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '10.0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '10.0'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rake-compiler
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0.9'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0.9'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: minitest
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "<"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 5.0.0
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - "<"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 5.0.0
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: pry
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
description: This library provides arbitrary-precision decimal floating-point number
|
72
|
+
class.
|
73
|
+
email:
|
74
|
+
- mrkn@mrkn.jp
|
75
|
+
executables: []
|
76
|
+
extensions:
|
77
|
+
- ext/bigdecimal/extconf.rb
|
78
|
+
extra_rdoc_files: []
|
79
|
+
files:
|
80
|
+
- bigdecimal.gemspec
|
81
|
+
- ext/bigdecimal/bigdecimal.c
|
82
|
+
- ext/bigdecimal/bigdecimal.h
|
83
|
+
- ext/bigdecimal/extconf.rb
|
84
|
+
- lib/bigdecimal.rb
|
85
|
+
- lib/bigdecimal/jacobian.rb
|
86
|
+
- lib/bigdecimal/ludcmp.rb
|
87
|
+
- lib/bigdecimal/math.rb
|
88
|
+
- lib/bigdecimal/newton.rb
|
89
|
+
- lib/bigdecimal/util.rb
|
90
|
+
- sample/linear.rb
|
91
|
+
- sample/nlsolve.rb
|
92
|
+
- sample/pi.rb
|
93
|
+
homepage: https://github.com/ruby/bigdecimal
|
94
|
+
licenses:
|
95
|
+
- ruby
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: 2.4.0
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubygems_version: 3.0.3
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Arbitrary-precision decimal floating-point number library.
|
116
|
+
test_files: []
|