bigdecimal 1.2.7 → 1.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/bigdecimal.gemspec +32 -18
- data/{bigdecimal.c → ext/bigdecimal/bigdecimal.c} +748 -482
- data/ext/bigdecimal/bigdecimal.def +3 -0
- data/{bigdecimal.h → ext/bigdecimal/bigdecimal.h} +87 -12
- data/{depend → ext/bigdecimal/depend} +4 -1
- data/ext/bigdecimal/extconf.rb +40 -0
- data/ext/bigdecimal/util/extconf.rb +24 -0
- data/ext/bigdecimal/util/util.c +9 -0
- data/lib/bigdecimal/jacobian.rb +4 -0
- data/lib/bigdecimal/ludcmp.rb +1 -0
- data/lib/bigdecimal/math.rb +10 -9
- data/lib/bigdecimal/newton.rb +1 -0
- data/lib/bigdecimal/util.rb +74 -55
- data/lib/bigdecimal.rb +22 -0
- data/sample/linear.rb +12 -10
- data/sample/nlsolve.rb +6 -5
- data/sample/pi.rb +1 -0
- metadata +91 -16
- data/README +0 -60
- data/extconf.rb +0 -10
@@ -4,18 +4,13 @@
|
|
4
4
|
*
|
5
5
|
* Copyright(C) 2002 by Shigeo Kobayashi(shigeo@tinyforest.gr.jp)
|
6
6
|
*
|
7
|
-
* You may distribute under the terms of either the GNU General Public
|
8
|
-
* License or the Artistic License, as specified in the README file
|
9
|
-
* of this BigDecimal distribution.
|
10
|
-
*
|
11
|
-
* NOTES:
|
12
|
-
* 2003-03-28 V1.0 checked in.
|
13
|
-
*
|
14
7
|
*/
|
15
8
|
|
16
9
|
#ifndef RUBY_BIG_DECIMAL_H
|
17
10
|
#define RUBY_BIG_DECIMAL_H 1
|
18
11
|
|
12
|
+
#define RUBY_NO_OLD_COMPATIBILITY
|
13
|
+
|
19
14
|
#include "ruby/ruby.h"
|
20
15
|
#include <float.h>
|
21
16
|
|
@@ -43,13 +38,35 @@
|
|
43
38
|
# define BDIGIT_DBL uint64_t
|
44
39
|
# define BDIGIT_DBL_SIGNED int64_t
|
45
40
|
# define SIZEOF_BDIGITS 4
|
41
|
+
# define PRI_BDIGIT_PREFIX ""
|
42
|
+
# ifdef PRI_LL_PREFIX
|
43
|
+
# define PRI_BDIGIT_DBL_PREFIX PRI_LL_PREFIX
|
44
|
+
# else
|
45
|
+
# define PRI_BDIGIT_DBL_PREFIX "l"
|
46
|
+
# endif
|
46
47
|
#else
|
47
48
|
# define BDIGIT uint16_t
|
48
49
|
# define BDIGIT_DBL uint32_t
|
49
50
|
# define BDIGIT_DBL_SIGNED int32_t
|
50
51
|
# define SIZEOF_BDIGITS 2
|
52
|
+
# define PRI_BDIGIT_PREFIX "h"
|
53
|
+
# define PRI_BDIGIT_DBL_PREFIX ""
|
51
54
|
#endif
|
52
55
|
|
56
|
+
#define PRIdBDIGIT PRI_BDIGIT_PREFIX"d"
|
57
|
+
#define PRIiBDIGIT PRI_BDIGIT_PREFIX"i"
|
58
|
+
#define PRIoBDIGIT PRI_BDIGIT_PREFIX"o"
|
59
|
+
#define PRIuBDIGIT PRI_BDIGIT_PREFIX"u"
|
60
|
+
#define PRIxBDIGIT PRI_BDIGIT_PREFIX"x"
|
61
|
+
#define PRIXBDIGIT PRI_BDIGIT_PREFIX"X"
|
62
|
+
|
63
|
+
#define PRIdBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"d"
|
64
|
+
#define PRIiBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"i"
|
65
|
+
#define PRIoBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"o"
|
66
|
+
#define PRIuBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"u"
|
67
|
+
#define PRIxBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"x"
|
68
|
+
#define PRIXBDIGIT_DBL PRI_BDIGIT_DBL_PREFIX"X"
|
69
|
+
|
53
70
|
#if defined(__cplusplus)
|
54
71
|
extern "C" {
|
55
72
|
#if 0
|
@@ -75,6 +92,62 @@ llabs(LONG_LONG const x)
|
|
75
92
|
}
|
76
93
|
#endif
|
77
94
|
|
95
|
+
#ifndef HAVE_FINITE
|
96
|
+
static int
|
97
|
+
finite(double)
|
98
|
+
{
|
99
|
+
return !isnan(n) && !isinf(n);
|
100
|
+
}
|
101
|
+
#endif
|
102
|
+
|
103
|
+
#ifndef isfinite
|
104
|
+
# ifndef HAVE_ISFINITE
|
105
|
+
# define HAVE_ISFINITE 1
|
106
|
+
# define isfinite(x) finite(x)
|
107
|
+
# endif
|
108
|
+
#endif
|
109
|
+
|
110
|
+
#ifndef FIX_CONST_VALUE_PTR
|
111
|
+
# if defined(__fcc__) || defined(__fcc_version) || \
|
112
|
+
defined(__FCC__) || defined(__FCC_VERSION)
|
113
|
+
/* workaround for old version of Fujitsu C Compiler (fcc) */
|
114
|
+
# define FIX_CONST_VALUE_PTR(x) ((const VALUE *)(x))
|
115
|
+
# else
|
116
|
+
# define FIX_CONST_VALUE_PTR(x) (x)
|
117
|
+
# endif
|
118
|
+
#endif
|
119
|
+
|
120
|
+
#ifndef HAVE_RB_ARRAY_CONST_PTR
|
121
|
+
static inline const VALUE *
|
122
|
+
rb_array_const_ptr(VALUE a)
|
123
|
+
{
|
124
|
+
return FIX_CONST_VALUE_PTR((RBASIC(a)->flags & RARRAY_EMBED_FLAG) ?
|
125
|
+
RARRAY(a)->as.ary : RARRAY(a)->as.heap.ptr);
|
126
|
+
}
|
127
|
+
#endif
|
128
|
+
|
129
|
+
#ifndef RARRAY_CONST_PTR
|
130
|
+
# define RARRAY_CONST_PTR(a) rb_array_const_ptr(a)
|
131
|
+
#endif
|
132
|
+
|
133
|
+
#ifndef RARRAY_AREF
|
134
|
+
# define RARRAY_AREF(a, i) (RARRAY_CONST_PTR(a)[i])
|
135
|
+
#endif
|
136
|
+
|
137
|
+
#ifndef HAVE_RB_SYM2STR
|
138
|
+
static inline VALUE
|
139
|
+
rb_sym2str(VALUE sym)
|
140
|
+
{
|
141
|
+
return rb_id2str(SYM2ID(sym));
|
142
|
+
}
|
143
|
+
#endif
|
144
|
+
|
145
|
+
#ifndef ST2FIX
|
146
|
+
# undef RB_ST2FIX
|
147
|
+
# define RB_ST2FIX(h) LONG2FIX((long)(h))
|
148
|
+
# define ST2FIX(h) RB_ST2FIX(h)
|
149
|
+
#endif
|
150
|
+
|
78
151
|
#ifdef vabs
|
79
152
|
# undef vabs
|
80
153
|
#endif
|
@@ -154,7 +227,9 @@ extern VALUE rb_cBigDecimal;
|
|
154
227
|
#define VP_SIGN_POSITIVE_INFINITE 3 /* Positive infinite number */
|
155
228
|
#define VP_SIGN_NEGATIVE_INFINITE -3 /* Negative infinite number */
|
156
229
|
|
157
|
-
#
|
230
|
+
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
|
231
|
+
#define FLEXIBLE_ARRAY_SIZE /* */
|
232
|
+
#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
158
233
|
#define FLEXIBLE_ARRAY_SIZE 0
|
159
234
|
#else
|
160
235
|
#define FLEXIBLE_ARRAY_SIZE 1
|
@@ -167,11 +242,11 @@ extern VALUE rb_cBigDecimal;
|
|
167
242
|
typedef struct {
|
168
243
|
VALUE obj; /* Back pointer(VALUE) for Ruby object. */
|
169
244
|
size_t MaxPrec; /* Maximum precision size */
|
170
|
-
/* This is the actual size of
|
245
|
+
/* This is the actual size of frac[] */
|
171
246
|
/*(frac[0] to frac[MaxPrec] are available). */
|
172
247
|
size_t Prec; /* Current precision size. */
|
173
|
-
/* This indicates how much the
|
174
|
-
/*
|
248
|
+
/* This indicates how much the */
|
249
|
+
/* array frac[] is actually used. */
|
175
250
|
SIGNED_VALUE exponent; /* Exponent part. */
|
176
251
|
short sign; /* Attributes of the value. */
|
177
252
|
/*
|
@@ -233,7 +308,7 @@ VP_EXPORT size_t VpInit(BDIGIT BaseVal);
|
|
233
308
|
VP_EXPORT void *VpMemAlloc(size_t mb);
|
234
309
|
VP_EXPORT void *VpMemRealloc(void *ptr, size_t mb);
|
235
310
|
VP_EXPORT void VpFree(Real *pv);
|
236
|
-
VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal);
|
311
|
+
VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal, int strict_p, int exc);
|
237
312
|
VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
|
238
313
|
VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
|
239
314
|
VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
|
@@ -1,13 +1,16 @@
|
|
1
|
+
extconf.h: $(srcdir)/$(GEMSPEC)
|
2
|
+
Makefile: $(BIGDECIMAL_RB)
|
3
|
+
|
1
4
|
# AUTOGENERATED DEPENDENCIES START
|
2
5
|
bigdecimal.o: $(RUBY_EXTCONF_H)
|
3
6
|
bigdecimal.o: $(arch_hdrdir)/ruby/config.h
|
4
7
|
bigdecimal.o: $(hdrdir)/ruby/defines.h
|
5
8
|
bigdecimal.o: $(hdrdir)/ruby/intern.h
|
6
9
|
bigdecimal.o: $(hdrdir)/ruby/missing.h
|
10
|
+
bigdecimal.o: $(hdrdir)/ruby/ruby.h
|
7
11
|
bigdecimal.o: $(hdrdir)/ruby/st.h
|
8
12
|
bigdecimal.o: $(hdrdir)/ruby/subst.h
|
9
13
|
bigdecimal.o: $(hdrdir)/ruby/util.h
|
10
|
-
bigdecimal.o: $(hdrdir)/ruby/ruby.h
|
11
14
|
bigdecimal.o: bigdecimal.c
|
12
15
|
bigdecimal.o: bigdecimal.h
|
13
16
|
# AUTOGENERATED DEPENDENCIES END
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'mkmf'
|
3
|
+
|
4
|
+
gemspec_name = gemspec_path = nil
|
5
|
+
unless ['', '../../'].any? {|dir|
|
6
|
+
gemspec_name = "#{dir}bigdecimal.gemspec"
|
7
|
+
gemspec_path = File.expand_path("../#{gemspec_name}", __FILE__)
|
8
|
+
File.file?(gemspec_path)
|
9
|
+
}
|
10
|
+
$stderr.puts "Unable to find bigdecimal.gemspec"
|
11
|
+
abort
|
12
|
+
end
|
13
|
+
|
14
|
+
bigdecimal_version =
|
15
|
+
IO.readlines(gemspec_path)
|
16
|
+
.grep(/\Abigdecimal_version\s+=\s+/)[0][/\'([\d\.]+)\'/, 1]
|
17
|
+
|
18
|
+
$defs << %Q[-DRUBY_BIGDECIMAL_VERSION=\\"#{bigdecimal_version}\\"]
|
19
|
+
|
20
|
+
have_func("labs", "stdlib.h")
|
21
|
+
have_func("llabs", "stdlib.h")
|
22
|
+
have_func("finite", "math.h")
|
23
|
+
have_func("isfinite", "math.h")
|
24
|
+
|
25
|
+
have_type("struct RRational", "ruby.h")
|
26
|
+
have_func("rb_rational_num", "ruby.h")
|
27
|
+
have_func("rb_rational_den", "ruby.h")
|
28
|
+
have_func("rb_array_const_ptr", "ruby.h")
|
29
|
+
have_func("rb_sym2str", "ruby.h")
|
30
|
+
|
31
|
+
if File.file?(File.expand_path('../lib/bigdecimal.rb', __FILE__))
|
32
|
+
bigdecimal_rb = "$(srcdir)/lib/bigdecimal.rb"
|
33
|
+
else
|
34
|
+
bigdecimal_rb = "$(srcdir)/../../lib/bigdecimal.rb"
|
35
|
+
end
|
36
|
+
|
37
|
+
create_makefile('bigdecimal') {|mf|
|
38
|
+
mf << "GEMSPEC = #{gemspec_name}\n"
|
39
|
+
mf << "BIGDECIMAL_RB = #{bigdecimal_rb}\n"
|
40
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'mkmf'
|
3
|
+
|
4
|
+
checking_for(checking_message("Windows")) do
|
5
|
+
case RUBY_PLATFORM
|
6
|
+
when /cygwin|mingw/
|
7
|
+
if ARGV.include?('-rdevkit') # check `rake -rdevkit compile` case
|
8
|
+
base_dir = File.expand_path('../../../..', __FILE__)
|
9
|
+
build_dir = File.join(base_dir, "tmp", RUBY_PLATFORM, "bigdecimal", RUBY_VERSION, "")
|
10
|
+
else
|
11
|
+
build_dir = "$(TARGET_SO_DIR)../"
|
12
|
+
end
|
13
|
+
$libs << " #{build_dir}bigdecimal.so"
|
14
|
+
true
|
15
|
+
when /mswin/
|
16
|
+
$DLDFLAGS << " -libpath:.."
|
17
|
+
$libs << " bigdecimal-$(arch).lib"
|
18
|
+
true
|
19
|
+
else
|
20
|
+
false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
create_makefile('bigdecimal/util')
|
data/lib/bigdecimal/jacobian.rb
CHANGED
data/lib/bigdecimal/ludcmp.rb
CHANGED
data/lib/bigdecimal/math.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: false
|
1
2
|
require 'bigdecimal'
|
2
3
|
|
3
4
|
#
|
@@ -25,7 +26,7 @@ require 'bigdecimal'
|
|
25
26
|
# include BigMath
|
26
27
|
#
|
27
28
|
# a = BigDecimal((PI(100)/2).to_s)
|
28
|
-
# puts sin(a,100) # => 0.
|
29
|
+
# puts sin(a,100) # => 0.99999999999999999999......e0
|
29
30
|
#
|
30
31
|
module BigMath
|
31
32
|
module_function
|
@@ -36,8 +37,8 @@ module BigMath
|
|
36
37
|
# Computes the square root of +decimal+ to the specified number of digits of
|
37
38
|
# precision, +numeric+.
|
38
39
|
#
|
39
|
-
# BigMath.sqrt(BigDecimal
|
40
|
-
# #=> "0.
|
40
|
+
# BigMath.sqrt(BigDecimal('2'), 16).to_s
|
41
|
+
# #=> "0.1414213562373095048801688724e1"
|
41
42
|
#
|
42
43
|
def sqrt(x, prec)
|
43
44
|
x.sqrt(prec)
|
@@ -52,7 +53,7 @@ module BigMath
|
|
52
53
|
# If +decimal+ is Infinity or NaN, returns NaN.
|
53
54
|
#
|
54
55
|
# BigMath.sin(BigMath.PI(5)/4, 5).to_s
|
55
|
-
# #=> "0.
|
56
|
+
# #=> "0.70710678118654752440082036563292800375e0"
|
56
57
|
#
|
57
58
|
def sin(x, prec)
|
58
59
|
raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
|
@@ -96,7 +97,7 @@ module BigMath
|
|
96
97
|
# If +decimal+ is Infinity or NaN, returns NaN.
|
97
98
|
#
|
98
99
|
# BigMath.cos(BigMath.PI(4), 16).to_s
|
99
|
-
# #=> "-0.
|
100
|
+
# #=> "-0.999999999999999999999999999999856613163740061349e0"
|
100
101
|
#
|
101
102
|
def cos(x, prec)
|
102
103
|
raise ArgumentError, "Zero or negative precision for cos" if prec <= 0
|
@@ -139,8 +140,8 @@ module BigMath
|
|
139
140
|
#
|
140
141
|
# If +decimal+ is NaN, returns NaN.
|
141
142
|
#
|
142
|
-
# BigMath.atan(BigDecimal
|
143
|
-
# #=> "-0.
|
143
|
+
# BigMath.atan(BigDecimal('-1'), 16).to_s
|
144
|
+
# #=> "-0.785398163397448309615660845819878471907514682065e0"
|
144
145
|
#
|
145
146
|
def atan(x, prec)
|
146
147
|
raise ArgumentError, "Zero or negative precision for atan" if prec <= 0
|
@@ -177,7 +178,7 @@ module BigMath
|
|
177
178
|
# +numeric+.
|
178
179
|
#
|
179
180
|
# BigMath.PI(10).to_s
|
180
|
-
# #=> "0.
|
181
|
+
# #=> "0.3141592653589793238462643388813853786957412e1"
|
181
182
|
#
|
182
183
|
def PI(prec)
|
183
184
|
raise ArgumentError, "Zero or negative precision for PI" if prec <= 0
|
@@ -222,7 +223,7 @@ module BigMath
|
|
222
223
|
# digits of precision, +numeric+.
|
223
224
|
#
|
224
225
|
# BigMath.E(10).to_s
|
225
|
-
# #=> "0.
|
226
|
+
# #=> "0.271828182845904523536028752390026306410273e1"
|
226
227
|
#
|
227
228
|
def E(prec)
|
228
229
|
raise ArgumentError, "Zero or negative precision for E" if prec <= 0
|
data/lib/bigdecimal/newton.rb
CHANGED
data/lib/bigdecimal/util.rb
CHANGED
@@ -1,71 +1,74 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: false
|
2
2
|
#
|
3
|
-
|
4
|
-
#
|
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
|
+
require 'bigdecimal/util.so'
|
10
|
+
|
5
11
|
class Integer < Numeric
|
6
12
|
# call-seq:
|
7
13
|
# int.to_d -> bigdecimal
|
8
14
|
#
|
9
|
-
#
|
15
|
+
# Returns the value of +int+ as a BigDecimal.
|
10
16
|
#
|
11
17
|
# require 'bigdecimal'
|
12
18
|
# require 'bigdecimal/util'
|
13
19
|
#
|
14
|
-
# 42.to_d
|
15
|
-
#
|
20
|
+
# 42.to_d # => 0.42e2
|
21
|
+
#
|
22
|
+
# See also BigDecimal::new.
|
16
23
|
#
|
17
24
|
def to_d
|
18
25
|
BigDecimal(self)
|
19
26
|
end
|
20
27
|
end
|
21
28
|
|
22
|
-
|
23
|
-
#
|
24
|
-
# When you require BigDecimal in your application, this method will be
|
25
|
-
# available on Float objects.
|
29
|
+
|
26
30
|
class Float < Numeric
|
27
31
|
# call-seq:
|
28
|
-
#
|
32
|
+
# float.to_d -> bigdecimal
|
33
|
+
# float.to_d(precision) -> bigdecimal
|
29
34
|
#
|
30
|
-
#
|
35
|
+
# Returns the value of +float+ as a BigDecimal.
|
36
|
+
# The +precision+ parameter is used to determine the number of
|
37
|
+
# significant digits for the result (the default is Float::DIG).
|
31
38
|
#
|
32
39
|
# require 'bigdecimal'
|
33
40
|
# require 'bigdecimal/util'
|
34
41
|
#
|
35
|
-
# 0.5.to_d
|
36
|
-
# # =>
|
42
|
+
# 0.5.to_d # => 0.5e0
|
43
|
+
# 1.234.to_d(2) # => 0.12e1
|
37
44
|
#
|
38
|
-
|
39
|
-
|
45
|
+
# See also BigDecimal::new.
|
46
|
+
#
|
47
|
+
def to_d(precision=Float::DIG)
|
48
|
+
BigDecimal(self, precision)
|
40
49
|
end
|
41
50
|
end
|
42
51
|
|
43
|
-
|
44
|
-
#
|
45
|
-
# When you require BigDecimal in your application, this method will be
|
46
|
-
# available on String objects.
|
52
|
+
|
47
53
|
class String
|
48
54
|
# call-seq:
|
49
|
-
#
|
55
|
+
# str.to_d -> bigdecimal
|
50
56
|
#
|
51
|
-
#
|
57
|
+
# Returns the result of interpreting leading characters in +str+
|
58
|
+
# as a BigDecimal.
|
52
59
|
#
|
53
60
|
# require 'bigdecimal'
|
54
61
|
# require 'bigdecimal/util'
|
55
62
|
#
|
56
|
-
# "0.5".to_d
|
57
|
-
# # =>
|
63
|
+
# "0.5".to_d # => 0.5e0
|
64
|
+
# "123.45e1".to_d # => 0.12345e4
|
65
|
+
# "45.67 degrees".to_d # => 0.4567e2
|
66
|
+
#
|
67
|
+
# See also BigDecimal::new.
|
58
68
|
#
|
59
|
-
def to_d
|
60
|
-
BigDecimal(self)
|
61
|
-
end
|
62
69
|
end
|
63
70
|
|
64
|
-
|
65
|
-
# #to_d methods.
|
66
|
-
#
|
67
|
-
# When you require BigDecimal in your application, this method will be
|
68
|
-
# available on BigDecimal objects.
|
71
|
+
|
69
72
|
class BigDecimal < Numeric
|
70
73
|
# call-seq:
|
71
74
|
# a.to_digits -> string
|
@@ -73,12 +76,11 @@ class BigDecimal < Numeric
|
|
73
76
|
# Converts a BigDecimal to a String of the form "nnnnnn.mmm".
|
74
77
|
# This method is deprecated; use BigDecimal#to_s("F") instead.
|
75
78
|
#
|
76
|
-
# require 'bigdecimal'
|
77
79
|
# require 'bigdecimal/util'
|
78
80
|
#
|
79
|
-
# d = BigDecimal
|
80
|
-
# d.to_digits
|
81
|
-
#
|
81
|
+
# d = BigDecimal("3.14")
|
82
|
+
# d.to_digits # => "3.14"
|
83
|
+
#
|
82
84
|
def to_digits
|
83
85
|
if self.nan? || self.infinite? || self.zero?
|
84
86
|
self.to_s
|
@@ -93,35 +95,52 @@ class BigDecimal < Numeric
|
|
93
95
|
# a.to_d -> bigdecimal
|
94
96
|
#
|
95
97
|
# Returns self.
|
98
|
+
#
|
99
|
+
# require 'bigdecimal/util'
|
100
|
+
#
|
101
|
+
# d = BigDecimal("3.14")
|
102
|
+
# d.to_d # => 0.314e1
|
103
|
+
#
|
96
104
|
def to_d
|
97
105
|
self
|
98
106
|
end
|
99
107
|
end
|
100
108
|
|
101
|
-
|
102
|
-
#
|
103
|
-
# When you require BigDecimal in your application, this method will be
|
104
|
-
# available on Rational objects.
|
109
|
+
|
105
110
|
class Rational < Numeric
|
106
111
|
# call-seq:
|
107
|
-
#
|
112
|
+
# rat.to_d(precision) -> bigdecimal
|
113
|
+
#
|
114
|
+
# Returns the value as a BigDecimal.
|
115
|
+
#
|
116
|
+
# The required +precision+ parameter is used to determine the number of
|
117
|
+
# significant digits for the result.
|
118
|
+
#
|
119
|
+
# require 'bigdecimal'
|
120
|
+
# require 'bigdecimal/util'
|
108
121
|
#
|
109
|
-
#
|
122
|
+
# Rational(22, 7).to_d(3) # => 0.314e1
|
110
123
|
#
|
111
|
-
#
|
112
|
-
# significant digits for the result. See BigDecimal#div for more information,
|
113
|
-
# as it is used along with the #denominator and the +precision+ for
|
114
|
-
# parameters.
|
124
|
+
# See also BigDecimal::new.
|
115
125
|
#
|
116
|
-
# r = (22/7.0).to_r
|
117
|
-
# # => (7077085128725065/2251799813685248)
|
118
|
-
# r.to_d(3)
|
119
|
-
# # => #<BigDecimal:1a44d08,'0.314E1',18(36)>
|
120
126
|
def to_d(precision)
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
127
|
+
BigDecimal(self, precision)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
class NilClass
|
133
|
+
# call-seq:
|
134
|
+
# nil.to_d -> bigdecimal
|
135
|
+
#
|
136
|
+
# Returns nil represented as a BigDecimal.
|
137
|
+
#
|
138
|
+
# require 'bigdecimal'
|
139
|
+
# require 'bigdecimal/util'
|
140
|
+
#
|
141
|
+
# nil.to_d # => 0.0
|
142
|
+
#
|
143
|
+
def to_d
|
144
|
+
BigDecimal(0)
|
126
145
|
end
|
127
146
|
end
|
data/lib/bigdecimal.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
begin
|
2
|
+
require "#{RUBY_VERSION[/\d+\.\d+/]}/bigdecimal.so"
|
3
|
+
rescue LoadError
|
4
|
+
require 'bigdecimal.so'
|
5
|
+
end
|
6
|
+
|
7
|
+
class BigDecimal
|
8
|
+
module Deprecation
|
9
|
+
def new(*args, **kwargs)
|
10
|
+
warn "BigDecimal.new is deprecated; use BigDecimal() method instead.", uplevel: 1
|
11
|
+
super
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
prepend Deprecation
|
17
|
+
|
18
|
+
def inherited(subclass)
|
19
|
+
warn "subclassing BigDecimal will be disallowed after bigdecimal version 2.0", uplevel: 1
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/sample/linear.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/local/bin/ruby
|
2
|
+
# frozen_string_literal: false
|
2
3
|
|
3
4
|
#
|
4
5
|
# linear.rb
|
@@ -27,8 +28,8 @@ def rd_order(na)
|
|
27
28
|
end
|
28
29
|
|
29
30
|
na = ARGV.size
|
30
|
-
zero = BigDecimal
|
31
|
-
one = BigDecimal
|
31
|
+
zero = BigDecimal("0.0")
|
32
|
+
one = BigDecimal("1.0")
|
32
33
|
|
33
34
|
while (n=rd_order(na))>0
|
34
35
|
a = []
|
@@ -36,27 +37,28 @@ while (n=rd_order(na))>0
|
|
36
37
|
b = []
|
37
38
|
if na <= 0
|
38
39
|
# Read data from console.
|
39
|
-
printf("\nEnter coefficient matrix element A[i,j]\n")
|
40
|
+
printf("\nEnter coefficient matrix element A[i,j]\n")
|
40
41
|
for i in 0...n do
|
41
42
|
for j in 0...n do
|
42
43
|
printf("A[%d,%d]? ",i,j); s = ARGF.gets
|
43
|
-
a << BigDecimal
|
44
|
-
as << BigDecimal
|
44
|
+
a << BigDecimal(s)
|
45
|
+
as << BigDecimal(s)
|
45
46
|
end
|
46
|
-
printf("Contatant vector element b[%d] ? ",i)
|
47
|
+
printf("Contatant vector element b[%d] ? ",i)
|
48
|
+
b << BigDecimal(ARGF.gets)
|
47
49
|
end
|
48
50
|
else
|
49
51
|
# Read data from specified file.
|
50
|
-
printf("Coefficient matrix and constant vector.\n")
|
52
|
+
printf("Coefficient matrix and constant vector.\n")
|
51
53
|
for i in 0...n do
|
52
54
|
s = ARGF.gets
|
53
55
|
printf("%d) %s",i,s)
|
54
56
|
s = s.split
|
55
57
|
for j in 0...n do
|
56
|
-
a << BigDecimal
|
57
|
-
as << BigDecimal
|
58
|
+
a << BigDecimal(s[j])
|
59
|
+
as << BigDecimal(s[j])
|
58
60
|
end
|
59
|
-
b << BigDecimal
|
61
|
+
b << BigDecimal(s[n])
|
60
62
|
end
|
61
63
|
end
|
62
64
|
x = lusolve(a,b,ludecomp(a,n,zero,one),zero)
|
data/sample/nlsolve.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/local/bin/ruby
|
2
|
+
# frozen_string_literal: false
|
2
3
|
|
3
4
|
#
|
4
5
|
# nlsolve.rb
|
@@ -11,11 +12,11 @@ include Newton
|
|
11
12
|
|
12
13
|
class Function # :nodoc: all
|
13
14
|
def initialize()
|
14
|
-
@zero = BigDecimal
|
15
|
-
@one = BigDecimal
|
16
|
-
@two = BigDecimal
|
17
|
-
@ten = BigDecimal
|
18
|
-
@eps = BigDecimal
|
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")
|
19
20
|
end
|
20
21
|
def zero;@zero;end
|
21
22
|
def one ;@one ;end
|