ruby-mpfr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2009-07-11
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,16 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/ruby-mpfr.rb
7
+ ext/ruby_mpfr.c
8
+ ext/ruby_mpfr.h
9
+ ext/extconf.rb
10
+ script/console
11
+ script/destroy
12
+ script/generate
13
+ spec/ruby-mpfr_spec.rb
14
+ spec/spec.opts
15
+ spec/spec_helper.rb
16
+ tasks/rspec.rake
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on ruby-mpfr, see http://ruby-mpfr.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,33 @@
1
+ = ruby-mpfr
2
+
3
+ * http://github.com/#{github_username}/#{project_name}
4
+
5
+ == DESCRIPTION:
6
+
7
+ ruby-mpfr is library to use MPFR which is a C library for
8
+ multiple-precision floating-point computations.
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * Many methods have not been tested.
13
+
14
+ == SYNOPSIS:
15
+
16
+ require "mpfr"
17
+ MPFR.get_default_prec(100)
18
+ a = MPFR.new(3)
19
+ b = MPFR.new('2.2')
20
+ puts (a * b).to_strf("%.30Re")
21
+
22
+ == REQUIREMENTS:
23
+
24
+ * GMP
25
+ * MPFR
26
+
27
+ == INSTALL:
28
+
29
+ * sudo gem install ruby-mpfr
30
+
31
+ == LICENSE:
32
+
33
+ LGPL v3
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/ruby-mpfr'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'ruby-mpfr' do
14
+ self.developer 'Takayuki YAMAGUCHI', 'd@ytak.info'
15
+ self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ # self.extra_deps = [['activesupport','>= 2.0.2']]
18
+ self.spec_extras[:extensions] = "ext/extconf.rb"
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
data/ext/extconf.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+
3
+ if have_header('mpfr.h') && have_library('mpfr') && have_header('gmp.h') && have_library('gmp')
4
+ create_makefile("mpfr")
5
+ end
data/ext/ruby_mpfr.c ADDED
@@ -0,0 +1,2542 @@
1
+ #include "ruby_mpfr.h"
2
+
3
+ static ID eqq, to_s, new, class, method_defined, object_id;
4
+ static VALUE __mpfr_class__, __sym_to_s__, __sym_to_str__;
5
+
6
+ /* ------------------------------ Precision and Rounding Mode Start ------------------------------ */
7
+ #define VALID_RND(rnd) (rnd >= 0 && rnd <= 3)
8
+ #define SPECIAL_FUNC_STATE "@@special_func_state"
9
+
10
+ /* Convert VALUE rnd (rounding mode number) to C integer and */
11
+ /* return it if it is valid as rounding mode number. */
12
+ mp_rnd_t r_mpfr_rnd_from_value(VALUE rnd){
13
+ mp_rnd_t r = (mp_rnd_t)NUM2INT(rnd);
14
+ if(!VALID_RND(r)){
15
+ rb_raise(rb_eArgError, "Argument must be Rounding Mode.");
16
+ }
17
+ return r;
18
+ }
19
+
20
+ /* If argc equals max, convert last argument to rounding mode number. */
21
+ /* Otherwise, return defoult rounding mode number. */
22
+ mp_rnd_t r_mpfr_rnd_from_optional_argument(int min, int max, int argc, VALUE *argv){
23
+ mp_rnd_t rnd;
24
+ if(argc >= min && argc <= max){
25
+ if(argc == max){
26
+ rnd = r_mpfr_rnd_from_value(argv[max-1]);
27
+ }else{
28
+ rnd = mpfr_get_default_rounding_mode();
29
+ }
30
+ }else{
31
+ rb_raise(rb_eArgError, "Invalid number of arguments.");
32
+ }
33
+ return rnd;
34
+ }
35
+
36
+ /* If argc equals max, convert last argument to precision number. */
37
+ /* Otherwise, return defoult precision number. */
38
+ mp_rnd_t r_mpfr_prec_from_optional_argument(int min, int max, int argc, VALUE *argv){
39
+ mp_prec_t prec;
40
+ if(argc >= min && argc <= max){
41
+ if(argc == max){
42
+ prec = NUM2INT(argv[max - 1]);
43
+ }else{
44
+ prec = mpfr_get_default_prec();
45
+ }
46
+ }else{
47
+ rb_raise(rb_eArgError, "Invalid number of arguments.");
48
+ }
49
+ return prec;
50
+ }
51
+
52
+ /* min is a minimum number of arguments. */
53
+ /* max is a maximum number of arguments. */
54
+ void r_mpfr_get_rnd_prec_from_optional_arguments(mp_rnd_t *rnd, mp_prec_t *prec, int min, int max,
55
+ int argc, VALUE *argv){
56
+ if(argc >= min && argc <= max){
57
+ if(argc >= max - 1){
58
+ *rnd = r_mpfr_rnd_from_value(argv[max - 2]);
59
+ }else{
60
+ *rnd = mpfr_get_default_rounding_mode();
61
+ }
62
+ if(argc == max){
63
+ *prec = NUM2INT(argv[max - 1]);
64
+ }else{
65
+ *prec = mpfr_get_default_prec();
66
+ }
67
+ }else{
68
+ rb_raise(rb_eArgError, "Invalid number of arguments.");
69
+ }
70
+ }
71
+
72
+
73
+ /* Set the default MPFR precision in bits. */
74
+ static VALUE r_mpfr_set_default_prec(VALUE self, VALUE prec){
75
+ int set_prec = NUM2INT(prec);
76
+ if(set_prec <= 0){
77
+ rb_raise(rb_eRangeError, "Argument must be positive.");
78
+ }
79
+ mpfr_set_default_prec(set_prec);
80
+ return INT2FIX(mpf_get_default_prec());
81
+ }
82
+
83
+ /* Return the default MPFR precision in bits. */
84
+ static VALUE r_mpfr_get_default_prec(VALUE self){ return INT2NUM((int)mpfr_get_default_prec()); }
85
+
86
+ /* Set the default rounding mode. The default rounding mode is MPFR::RNDN. */
87
+ static VALUE r_mpfr_set_default_rounding_mode(VALUE self, VALUE rnd){
88
+ mp_rnd_t a = NUM2INT(rnd);
89
+ if(VALID_RND(a)){
90
+ mpfr_set_default_rounding_mode(a);
91
+ }else{
92
+ rb_raise(rb_eArgError, "Argument must be Rounding Mode.");
93
+ }
94
+ return INT2FIX(mpfr_get_default_rounding_mode());
95
+ }
96
+
97
+ /* Get the default rounding mode. */
98
+ static VALUE r_mpfr_get_default_rounding_mode(VALUE self){ return INT2NUM(mpfr_get_default_rounding_mode()); }
99
+
100
+ /* ------------------------------ Precision and Rounding Mode End ------------------------------ */
101
+
102
+ /* ------------------------------ Exception Related Functions Start ------------------------------ */
103
+
104
+ /* Return integer which is mpfr_get_emin(). */
105
+ static VALUE r_mpfr_get_emin(VALUE self){ return INT2NUM(mpfr_get_emin()); }
106
+
107
+ /* Return integer which is mpfr_get_emax(). */
108
+ static VALUE r_mpfr_get_emax(VALUE self){ return INT2NUM(mpfr_get_emax()); }
109
+
110
+ /* Return integer which is mpfr_set_emin( p1 ). */
111
+ static VALUE r_mpfr_set_emin(VALUE self, VALUE exp){ return INT2NUM(mpfr_set_emin(NUM2INT(exp))); }
112
+
113
+ /* Return integer which is mpfr_set_emax( p1 ). */
114
+ static VALUE r_mpfr_set_emax(VALUE self, VALUE exp){ return INT2NUM(mpfr_set_emax(NUM2INT(exp))); }
115
+
116
+ /* Return integer which is mpfr_get_emin_min(). */
117
+ static VALUE r_mpfr_get_emin_min(VALUE self){ return INT2NUM(mpfr_get_emin_min()); }
118
+
119
+ /* Return integer which is mpfr_get_emin_max(). */
120
+ static VALUE r_mpfr_get_emin_max(VALUE self){ return INT2NUM(mpfr_get_emin_max()); }
121
+
122
+ /* Return integer which is mpfr_get_emax_min(). */
123
+ static VALUE r_mpfr_get_emax_min(VALUE self){ return INT2NUM(mpfr_get_emax_min()); }
124
+
125
+ /* Return integer which is mpfr_get_emax_max(). */
126
+ static VALUE r_mpfr_get_emax_max(VALUE self){ return INT2NUM(mpfr_get_emax_max()); }
127
+
128
+ /* Execute mpfr_clear_underflow() and return nil. */
129
+ static VALUE r_mpfr_clear_underflow(VALUE self){ mpfr_clear_underflow(); return Qnil; }
130
+
131
+ /* Execute mpfr_clear_overflow() and return nil. */
132
+ static VALUE r_mpfr_clear_overflow(VALUE self){ mpfr_clear_overflow(); return Qnil; }
133
+
134
+ /* Execute mpfr_clear_nanflag() and return nil. */
135
+ static VALUE r_mpfr_clear_nanflag(VALUE self){ mpfr_clear_nanflag(); return Qnil; }
136
+
137
+ /* Execute mpfr_clear_inexflag() and return nil. */
138
+ static VALUE r_mpfr_clear_inexflag(VALUE self){ mpfr_clear_inexflag(); return Qnil; }
139
+
140
+ /* Execute mpfr_clear_erangeflag() and return nil. */
141
+ static VALUE r_mpfr_clear_erangeflag(VALUE self){ mpfr_clear_erangeflag(); return Qnil; }
142
+
143
+ /* Execute mpfr_set_underflow() and return nil. */
144
+ static VALUE r_mpfr_set_underflow(VALUE self){ mpfr_set_underflow(); return Qnil; }
145
+
146
+ /* Execute mpfr_set_overflow() and return nil. */
147
+ static VALUE r_mpfr_set_overflow(VALUE self){ mpfr_set_overflow(); return Qnil; }
148
+
149
+ /* Execute mpfr_set_nanflag() and return nil. */
150
+ static VALUE r_mpfr_set_nanflag(VALUE self){ mpfr_set_nanflag(); return Qnil; }
151
+
152
+ /* Execute mpfr_set_inexflag() and return nil. */
153
+ static VALUE r_mpfr_set_inexflag(VALUE self){ mpfr_set_inexflag(); return Qnil; }
154
+
155
+ /* Execute mpfr_set_erangeflag() and return nil. */
156
+ static VALUE r_mpfr_set_erangeflag(VALUE self){ mpfr_set_erangeflag(); return Qnil; }
157
+
158
+ /* Execute mpfr_clear_flags() and return nil. */
159
+ static VALUE r_mpfr_clear_flags(VALUE self){ mpfr_clear_flags(); return Qnil; }
160
+
161
+ /* If underflow flag is set, this method returns true. Otherwise nil. */
162
+ static VALUE r_mpfr_underflow_p(VALUE self){ return (mpfr_underflow_p() != 0 ? Qtrue : Qnil); }
163
+
164
+ /* If owerflow flag is set, this method returns true. Otherwise nil. */
165
+ static VALUE r_mpfr_overflow_p(VALUE self){ return (mpfr_overflow_p() != 0 ? Qtrue : Qnil); }
166
+
167
+ /* If invalid flag is set, this method returns true. Otherwise nil. */
168
+ static VALUE r_mpfr_nanflag_p(VALUE self){ return (mpfr_nanflag_p() != 0 ? Qtrue : Qnil); }
169
+
170
+ /* If inexact flag is set, this method returns true. Otherwise nil. */
171
+ static VALUE r_mpfr_inexflag_p(VALUE self){ return (mpfr_inexflag_p() != 0 ? Qtrue : Qnil); }
172
+
173
+ /* If erange flag is set, this method returns true. Otherwise nil. */
174
+ static VALUE r_mpfr_erangeflag_p(VALUE self){ return (mpfr_erangeflag_p() != 0 ? Qtrue : Qnil); }
175
+
176
+ /* Execute mpfr_check_range( self, p1, rnd ) and return self. */
177
+ static VALUE r_mpfr_check_range(int argc, VALUE *argv, VALUE self){
178
+ mp_rnd_t rnd = r_mpfr_rnd_from_optional_argument(1, 2, argc, argv);
179
+ MPFR *ptr_self;
180
+ r_mpfr_get_struct(ptr_self, self);
181
+ mpfr_check_range(ptr_self, NUM2INT(argv[0]), rnd);
182
+ return self;
183
+ }
184
+
185
+ /* Execute mpfr_subnormalize( self, p1, rnd ) and return self. */
186
+ static VALUE r_mpfr_subnormalize(int argc, VALUE *argv, VALUE self){
187
+ mp_rnd_t rnd = r_mpfr_rnd_from_optional_argument(1, 2, argc, argv);
188
+ MPFR *ptr_self;
189
+ r_mpfr_get_struct(ptr_self, self);
190
+ mpfr_subnormalize(ptr_self, NUM2INT(argv[0]), rnd);
191
+ return self;
192
+ }
193
+
194
+ /* ------------------------------ Exception Related Functions End ------------------------------ */
195
+
196
+ /* ------------------------------ MPFR allocation Start ------------------------------ */
197
+
198
+ void r_mpfr_free(void *ptr){
199
+ mpfr_clear(ptr);
200
+ free(ptr);
201
+ }
202
+
203
+ static void r_mpfr_convert_to_str_set(MPFR *ptr, VALUE obj, mp_rnd_t rnd){
204
+ if(RTEST(rb_funcall(rb_funcall(obj, class, 0), method_defined, 1, __sym_to_str__))){
205
+ char *str = StringValuePtr(obj);
206
+ mpfr_set_str(ptr, str, 10, rnd);
207
+ }else if(RTEST(rb_funcall(rb_funcall(obj, class, 0), method_defined, 1, __sym_to_s__))){
208
+ volatile VALUE tmp = rb_funcall(obj, to_s, 0);
209
+ char *str = StringValuePtr(tmp);
210
+ mpfr_set_str(ptr, str, 10, rnd);
211
+ }
212
+ }
213
+
214
+ void r_mpfr_set_robj(MPFR *ptr, VALUE obj, mp_rnd_t rnd){
215
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, obj))){
216
+ MPFR *ptr_obj;
217
+ r_mpfr_get_struct(ptr_obj, obj);
218
+ mpfr_set(ptr, ptr_obj, rnd);
219
+ }else{
220
+ switch(TYPE(obj)){
221
+ case T_STRING:
222
+ mpfr_set_str(ptr, StringValuePtr(obj), 10, rnd);
223
+ break;
224
+ case T_FLOAT:
225
+ mpfr_set_d(ptr, NUM2DBL(obj), rnd);
226
+ break;
227
+ case T_FIXNUM:
228
+ mpfr_set_si(ptr, FIX2LONG(obj), rnd);
229
+ break;
230
+ case T_BIGNUM:
231
+ r_mpfr_convert_to_str_set(ptr, obj, rnd);
232
+ break;
233
+ default:
234
+ rb_raise(rb_eArgError, "Invalid class %s for making MPFR.", rb_class2name(obj));
235
+ break;
236
+ }
237
+ }
238
+ }
239
+
240
+ /* If obj is MPFR instance, then this method returns obj. */
241
+ /* Otherwise it returns MPFR.new(obj). */
242
+ VALUE r_mpfr_new_fr_obj(VALUE obj){
243
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, obj))){
244
+ return obj;
245
+ }else{
246
+ return rb_funcall(__mpfr_class__, new, 1, obj);
247
+ }
248
+ }
249
+
250
+ static VALUE r_mpfr_alloc(VALUE self){
251
+ MPFR *ptr;
252
+ r_mpfr_make_struct(self, ptr);
253
+ return self;
254
+ }
255
+
256
+ /*
257
+ This method returns MPFR instance.
258
+ If there is not an argument, it is set NaN.
259
+ Possible arguments are value, rounding mode, and precesion.
260
+ All arguments are optional.
261
+ */
262
+ static VALUE r_mpfr_initialize(int argc, VALUE *argv, VALUE self){
263
+ MPFR *ptr;
264
+ r_mpfr_get_struct(ptr, self);
265
+ switch(argc){
266
+ case 0:
267
+ mpfr_init(ptr);
268
+ mpfr_set_nan(ptr);
269
+ break;
270
+ case 1:
271
+ mpfr_init(ptr);
272
+ r_mpfr_set_robj(ptr, argv[0], mpfr_get_default_rounding_mode());
273
+ break;
274
+ case 2:
275
+ case 3:
276
+ if(argc == 2){
277
+ mpfr_init(ptr);
278
+ }else{
279
+ mpfr_init2(ptr, NUM2INT(argv[2]));
280
+ }
281
+ r_mpfr_set_robj(ptr, argv[0], r_mpfr_rnd_from_value(argv[1]));
282
+ break;
283
+ default:
284
+ rb_raise(rb_eArgError, "Invalid number of arguments.");
285
+ break;
286
+ }
287
+ return Qtrue;
288
+ }
289
+
290
+ static VALUE r_mpfr_initialize_copy(VALUE self, VALUE other){
291
+ MPFR *ptr_self, *ptr_other;
292
+ r_mpfr_get_struct(ptr_self, self);
293
+ r_mpfr_get_struct(ptr_other, other);
294
+ mpfr_init2(ptr_self, mpfr_get_prec(ptr_other));
295
+ mpfr_set(ptr_self, ptr_other, mpfr_get_default_rounding_mode());
296
+ return Qtrue;
297
+ }
298
+
299
+ /* Return array which have MPFR instance converted to from p1 and self. */
300
+ static VALUE r_mpfr_coerce(VALUE self, VALUE other){
301
+ VALUE val_other;
302
+ MPFR *ptr_self, *ptr_other;
303
+ r_mpfr_get_struct(ptr_self, self);
304
+ r_mpfr_make_struct_init2(val_other, ptr_other, mpfr_get_prec(ptr_self));
305
+ r_mpfr_set_robj(ptr_other, other, mpfr_get_default_rounding_mode());
306
+ return rb_ary_new3(2, val_other, self);
307
+ }
308
+
309
+ /* Return NaN. This method takes one optional argument meaning precision. */
310
+ static VALUE r_mpfr_nan(int argc, VALUE *argv, VALUE self){
311
+ mp_prec_t prec = r_mpfr_prec_from_optional_argument(0, 1, argc, argv);
312
+ MPFR *ptr_return;
313
+ VALUE val_ret;
314
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
315
+ mpfr_set_nan(ptr_return);
316
+ return val_ret;
317
+ }
318
+
319
+ /* Return plus infinity. This method takes one optional argument meaning precision. */
320
+ static VALUE r_mpfr_pinf(int argc, VALUE *argv, VALUE self){
321
+ mp_prec_t prec = r_mpfr_prec_from_optional_argument(0, 1, argc, argv);
322
+ MPFR *ptr_return;
323
+ VALUE val_ret;
324
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
325
+ mpfr_set_inf(ptr_return, 1);
326
+ return val_ret;
327
+ }
328
+
329
+ /* Return minus infinity. This method takes one optional argument meaning precision. */
330
+ static VALUE r_mpfr_minf(int argc, VALUE *argv, VALUE self){
331
+ mp_prec_t prec = r_mpfr_prec_from_optional_argument(0, 1, argc, argv);
332
+ MPFR *ptr_return;
333
+ VALUE val_ret;
334
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
335
+ mpfr_set_inf(ptr_return, -1);
336
+ return val_ret;
337
+ }
338
+
339
+ /* ------------------------------ MPFR allocation End ------------------------------ */
340
+
341
+ /* ------------------------------ Assignment Functions Start ------------------------------ */
342
+
343
+ /* Reset the precision of self to be exactly p1 bits and set its value to NaN. */
344
+ static VALUE r_mpfr_set_prec(VALUE self, VALUE prec){
345
+ MPFR *ptr_self;
346
+ r_mpfr_get_struct(ptr_self, self);
347
+ mpfr_set_prec(ptr_self, NUM2INT(prec));
348
+ return INT2NUM((int)mpfr_get_prec(ptr_self));
349
+ }
350
+
351
+ /* Return precision actually used for assignments of self. */
352
+ static VALUE r_mpfr_get_prec(VALUE self){
353
+ MPFR *ptr_self;
354
+ r_mpfr_get_struct(ptr_self, self);
355
+ return INT2NUM((int)mpfr_get_prec(ptr_self));
356
+ }
357
+
358
+ /* Arguments are val, rnd, and prec. Set the value of self from val. rnd and prec are optional.*/
359
+ static VALUE r_mpfr_set(int argc, VALUE *argv, VALUE self){
360
+ MPFR *ptr_self;
361
+ r_mpfr_get_struct(ptr_self, self);
362
+ mp_rnd_t rnd = r_mpfr_rnd_from_optional_argument(1, 2, argc, argv);
363
+ r_mpfr_set_robj(ptr_self, argv[0], rnd);
364
+ return self;
365
+ }
366
+
367
+ /*
368
+ Arguments are num, exp, rnd, and prec. Set the value of self from num multiplied by two to the power exp.
369
+ Set the value of self from val. rnd and prec are optional.
370
+ */
371
+ static VALUE r_mpfr_set_fixnum_2exp(int argc, VALUE *argv, VALUE self){
372
+ MPFR *ptr_self;
373
+ r_mpfr_get_struct(ptr_self, self);
374
+ mp_rnd_t rnd = r_mpfr_rnd_from_optional_argument(2, 3, argc, argv);
375
+ mpfr_set_si_2exp(ptr_self, NUM2INT(argv[0]), NUM2INT(argv[1]), rnd);
376
+ return self;
377
+ }
378
+
379
+ /*
380
+ If p1 is nonnegative Fixnum, set the value of self to plus Inf.
381
+ Otherwise, set the value of self to minus Inf.
382
+ */
383
+ static VALUE r_mpfr_set_inf(VALUE self, VALUE sign){
384
+ MPFR *ptr_self;
385
+ r_mpfr_get_struct(ptr_self, self);
386
+ mpfr_set_inf(ptr_self, NUM2INT(sign));
387
+ return self;
388
+ }
389
+
390
+ /* Set the value of self to NaN. */
391
+ static VALUE r_mpfr_set_nan(VALUE self){
392
+ MPFR *ptr_self;
393
+ r_mpfr_get_struct(ptr_self, self);
394
+ mpfr_set_nan(ptr_self);
395
+ return self;
396
+ }
397
+
398
+ /* Swap the values self and p1 efficiently. p1 must be MPFR object. */
399
+ static VALUE r_mpfr_swap(VALUE self, VALUE other){
400
+ MPFR *ptr_self, *ptr_other;
401
+ r_mpfr_get_struct(ptr_self, self);
402
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, other))){
403
+ r_mpfr_get_struct(ptr_other, other);
404
+ }else{
405
+ rb_raise(rb_eArgError, "Argument must be MPFR object.");
406
+ }
407
+ mpfr_swap(ptr_self, ptr_other);
408
+ return self;
409
+ }
410
+
411
+ /* ------------------------------ Assignment Functions End ------------------------------ */
412
+
413
+ /* ------------------------------ Methods related to string Start ------------------------------ */
414
+
415
+ /* Output self by mpfr_asprintf( some_val, p1, self ). For example, p1 are "%.Re", "%.Rf" or "%.30Re" etc. */
416
+ static VALUE r_mpfr_to_strf(VALUE self, VALUE format_str){
417
+ MPFR *ptr_self;
418
+ r_mpfr_get_struct(ptr_self, self);
419
+ char *format = StringValuePtr(format_str);
420
+ char *ret_str;
421
+ mpfr_asprintf(&ret_str, format, ptr_self);
422
+ VALUE ret_val = rb_str_new2(ret_str);
423
+ mpfr_free_str(ret_str);
424
+ return ret_val;
425
+ }
426
+
427
+ /* Convert to string. */
428
+ static VALUE r_mpfr_to_s(VALUE self){
429
+ MPFR *ptr_self;
430
+ r_mpfr_get_struct(ptr_self, self);
431
+ char *ret_str;
432
+ mpfr_asprintf(&ret_str, "%.Re", ptr_self);
433
+ VALUE ret_val = rb_str_new2(ret_str);
434
+ mpfr_free_str(ret_str);
435
+ return ret_val;
436
+ }
437
+
438
+ /* Output for debugging. */
439
+ static VALUE r_mpfr_inspect(VALUE self){
440
+ MPFR *ptr_s;
441
+ r_mpfr_get_struct(ptr_s, self);
442
+ char *ret_str;
443
+ mpfr_asprintf(&ret_str, "#<MPFR:%lx,'%0.Re',%d>", NUM2LONG(rb_funcall(self, object_id, 0)), ptr_s, mpfr_get_prec(ptr_s));
444
+ VALUE ret_val = rb_str_new2(ret_str);
445
+ mpfr_free_str(ret_str);
446
+ return ret_val;
447
+ }
448
+
449
+ /* ------------------------------ Methods related to string End ------------------------------ */
450
+
451
+ /* ------------------------------ Conversion functions Start ------------------------------ */
452
+
453
+ /* Return Float object by converting self. Optional argument is rnd meaning rounding mode. See MPFR reference for detail. */
454
+ static VALUE r_mpfr_get_d(int argc, VALUE *argv, VALUE self){
455
+ MPFR *ptr_self;
456
+ r_mpfr_get_struct(ptr_self, self);
457
+ return rb_float_new(mpfr_get_d(ptr_self, r_mpfr_rnd_from_optional_argument(0, 1, argc, argv)));
458
+ }
459
+
460
+ /* Return array having Float object d and Fixnum object i such that 0.5 <= abs(d) < 1.0 and */
461
+ /* d times 2 raised to exp equals self rounded to double precision. See MPFR reference for detail. */
462
+ static VALUE r_mpfr_get_d_2exp(int argc, VALUE *argv, VALUE self){
463
+ MPFR *ptr_self;
464
+ r_mpfr_get_struct(ptr_self, self);
465
+ long int ret_val2;
466
+ double ret_val1 = mpfr_get_d_2exp(&ret_val2, ptr_self, r_mpfr_rnd_from_optional_argument(0, 1, argc, argv));
467
+ return rb_ary_new3(2, rb_float_new(ret_val1), INT2NUM(ret_val2));
468
+ }
469
+
470
+ /* Return Fixnum object converted after rounding self with respect to rnd which is optional argument. */
471
+ static VALUE r_mpfr_get_si(int argc, VALUE *argv, VALUE self){
472
+ MPFR *ptr_self;
473
+ r_mpfr_get_struct(ptr_self, self);
474
+ return INT2NUM(mpfr_get_si(ptr_self, r_mpfr_rnd_from_optional_argument(0, 1, argc, argv)));
475
+ }
476
+
477
+ /* Return Fixnum object which is nearest integer to self. */
478
+ static VALUE r_mpfr_round_to_i(VALUE self){
479
+ MPFR *ptr_self;
480
+ r_mpfr_get_struct(ptr_self, self);
481
+ return INT2NUM(mpfr_get_si(ptr_self, GMP_RNDN));
482
+ }
483
+
484
+ /* Return Fixnum object which is the minimum integer over self. */
485
+ static VALUE r_mpfr_ceil_to_i(VALUE self){
486
+ MPFR *ptr_self;
487
+ r_mpfr_get_struct(ptr_self, self);
488
+ return INT2NUM(mpfr_get_si(ptr_self, GMP_RNDU));
489
+ }
490
+
491
+ /* Return Fixnum object which is the maximum integer not over self. */
492
+ static VALUE r_mpfr_floor_to_i(VALUE self){
493
+ MPFR *ptr_self;
494
+ r_mpfr_get_struct(ptr_self, self);
495
+ return INT2NUM(mpfr_get_si(ptr_self, GMP_RNDD));
496
+ }
497
+
498
+ /* Return Fixnum object by truncating self. */
499
+ static VALUE r_mpfr_truncate_to_i(VALUE self){
500
+ MPFR *ptr_self;
501
+ r_mpfr_get_struct(ptr_self, self);
502
+ return INT2NUM(mpfr_get_si(ptr_self, GMP_RNDZ));
503
+ }
504
+
505
+ /* Return array having String object meaning mantissa and Fixnum object meaning exponent. See MPFR reference for detail. */
506
+ static VALUE r_mpfr_get_str(VALUE self){
507
+ MPFR *ptr_self;
508
+ r_mpfr_get_struct(ptr_self, self);
509
+ mp_exp_t e;
510
+ char *str = mpfr_get_str(NULL, &e, 10, 0, ptr_self, GMP_RNDN);
511
+ VALUE ret_str = rb_str_new2(str);
512
+ mpfr_free_str(str);
513
+ return rb_ary_new3(2, ret_str, INT2FIX((int)e));
514
+ }
515
+
516
+ /* ------------------------------ Conversion functions End ------------------------------ */
517
+
518
+ /* ------------------------------ Basic Arithmetic Functions Start ------------------------------ */
519
+
520
+ /* Return self + p1. */
521
+ static VALUE r_mpfr_add(VALUE self, VALUE other){
522
+ MPFR *ptr_self, *ptr_other, *ptr_return;
523
+ VALUE val_ret;
524
+ r_mpfr_get_struct(ptr_self, self);
525
+ r_mpfr_make_struct_init(val_ret, ptr_return);
526
+
527
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, other))){
528
+ r_mpfr_get_struct(ptr_other, other);
529
+ mpfr_add(ptr_return, ptr_self, ptr_other, mpfr_get_default_rounding_mode());
530
+ }else if(TYPE(other) == T_FIXNUM){
531
+ mpfr_add_si(ptr_return, ptr_self, FIX2LONG(other), mpfr_get_default_rounding_mode());
532
+ }else if(TYPE(other) == T_FLOAT){
533
+ mpfr_add_d(ptr_return, ptr_self, NUM2DBL(other), mpfr_get_default_rounding_mode());
534
+ }else if(TYPE(other) == T_BIGNUM){
535
+ volatile VALUE tmp = rb_funcall(__mpfr_class__, new, 1, other);
536
+ r_mpfr_get_struct(ptr_other, tmp);
537
+ mpfr_add(ptr_return, ptr_self, ptr_other, mpfr_get_default_rounding_mode());
538
+ }else{
539
+ rb_raise(rb_eArgError, "Argument must be MPFR, Fixnum, Float, or Bignum.");
540
+ }
541
+ return val_ret;
542
+ }
543
+
544
+ /* Return self - p1. */
545
+ static VALUE r_mpfr_sub(VALUE self, VALUE other){
546
+ MPFR *ptr_self, *ptr_other, *ptr_return;
547
+ VALUE val_ret;
548
+ r_mpfr_get_struct(ptr_self, self);
549
+ r_mpfr_make_struct_init(val_ret, ptr_return);
550
+
551
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, other))){
552
+ r_mpfr_get_struct(ptr_other, other);
553
+ mpfr_sub(ptr_return, ptr_self, ptr_other, mpfr_get_default_rounding_mode());
554
+ }else if(TYPE(other) == T_FIXNUM){
555
+ mpfr_sub_si(ptr_return, ptr_self, FIX2LONG(other), mpfr_get_default_rounding_mode());
556
+ }else if(TYPE(other) == T_FLOAT){
557
+ mpfr_sub_d(ptr_return, ptr_self, NUM2DBL(other), mpfr_get_default_rounding_mode());
558
+ }else if(TYPE(other) == T_BIGNUM){
559
+ volatile VALUE tmp = rb_funcall(__mpfr_class__, new, 1, other);
560
+ r_mpfr_get_struct(ptr_other, tmp);
561
+ mpfr_sub(ptr_return, ptr_self, ptr_other, mpfr_get_default_rounding_mode());
562
+ }else{
563
+ rb_raise(rb_eArgError, "Argument must be MPFR, Fixnum, Float, or Bignum.");
564
+ }
565
+ return val_ret;
566
+ }
567
+
568
+ /* Return self * p1. */
569
+ static VALUE r_mpfr_mul(VALUE self, VALUE other){
570
+ MPFR *ptr_self, *ptr_other, *ptr_return;
571
+ VALUE val_ret;
572
+ r_mpfr_get_struct(ptr_self, self);
573
+ r_mpfr_make_struct_init(val_ret, ptr_return);
574
+
575
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, other))){
576
+ r_mpfr_get_struct(ptr_other, other);
577
+ mpfr_mul(ptr_return, ptr_self, ptr_other, mpfr_get_default_rounding_mode());
578
+ }else if(TYPE(other) == T_FIXNUM){
579
+ mpfr_mul_si(ptr_return, ptr_self, FIX2LONG(other), mpfr_get_default_rounding_mode());
580
+ }else if(TYPE(other) == T_FLOAT){
581
+ mpfr_mul_d(ptr_return, ptr_self, NUM2DBL(other), mpfr_get_default_rounding_mode());
582
+ }else if(TYPE(other) == T_BIGNUM){
583
+ volatile VALUE tmp = rb_funcall(__mpfr_class__, new, 1, other);
584
+ r_mpfr_get_struct(ptr_other, tmp);
585
+ mpfr_mul(ptr_return, ptr_self, ptr_other, mpfr_get_default_rounding_mode());
586
+ }else{
587
+ rb_raise(rb_eArgError, "Argument must be MPFR, Fixnum, Float, or Bignum.");
588
+ }
589
+ return val_ret;
590
+ }
591
+
592
+ /* Return self / p1. */
593
+ static VALUE r_mpfr_div(VALUE self, VALUE other){
594
+ MPFR *ptr_self, *ptr_other, *ptr_return;
595
+ VALUE val_ret;
596
+ r_mpfr_get_struct(ptr_self, self);
597
+ r_mpfr_make_struct_init(val_ret, ptr_return);
598
+
599
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, other))){
600
+ r_mpfr_get_struct(ptr_other, other);
601
+ mpfr_div(ptr_return, ptr_self, ptr_other, mpfr_get_default_rounding_mode());
602
+ }else if(TYPE(other) == T_FIXNUM){
603
+ mpfr_div_si(ptr_return, ptr_self, FIX2LONG(other), mpfr_get_default_rounding_mode());
604
+ }else if(TYPE(other) == T_FLOAT){
605
+ mpfr_div_d(ptr_return, ptr_self, NUM2DBL(other), mpfr_get_default_rounding_mode());
606
+ }else if(TYPE(other) == T_BIGNUM){
607
+ volatile VALUE tmp = rb_funcall(__mpfr_class__, new, 1, other);
608
+ r_mpfr_get_struct(ptr_other, tmp);
609
+ mpfr_div(ptr_return, ptr_self, ptr_other, mpfr_get_default_rounding_mode());
610
+ }else{
611
+ rb_raise(rb_eArgError, "Argument must be MPFR, Fixnum, Float, or Bignum.");
612
+ }
613
+ return val_ret;
614
+ }
615
+
616
+ /* Return p1-th power of self. */
617
+ static VALUE r_mpfr_pow(VALUE self, VALUE other){
618
+ MPFR *ptr_self, *ptr_other, *ptr_return;
619
+ VALUE val_ret;
620
+ r_mpfr_get_struct(ptr_self, self);
621
+ r_mpfr_make_struct_init(val_ret, ptr_return);
622
+ if(TYPE(other) == T_FIXNUM){
623
+ mpfr_pow_si(ptr_return, ptr_self, FIX2LONG(other), mpfr_get_default_rounding_mode());
624
+ }else{
625
+ volatile VALUE tmp_other = r_mpfr_new_fr_obj(other);
626
+ r_mpfr_get_struct(ptr_other, tmp_other);
627
+ mpfr_pow(ptr_return, ptr_self, ptr_other, mpfr_get_default_rounding_mode());
628
+ }
629
+ return val_ret;
630
+ }
631
+
632
+ /* Return negative value of self. */
633
+ static VALUE r_mpfr_neg(int argc, VALUE *argv, VALUE self){
634
+ mp_rnd_t rnd;
635
+ mp_prec_t prec;
636
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
637
+ MPFR *ptr_self, *ptr_return;
638
+ VALUE val_ret;
639
+ r_mpfr_get_struct(ptr_self, self);
640
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
641
+ mpfr_neg(ptr_return, ptr_self, rnd);
642
+ return val_ret;
643
+ }
644
+
645
+ /* Return absolute value of self. */
646
+ static VALUE r_mpfr_abs(int argc, VALUE *argv, VALUE self){
647
+ mp_rnd_t rnd;
648
+ mp_prec_t prec;
649
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
650
+ MPFR *ptr_self, *ptr_return;
651
+ VALUE val_ret;
652
+ r_mpfr_get_struct(ptr_self, self);
653
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
654
+ mpfr_abs(ptr_return, ptr_self, rnd);
655
+ return val_ret;
656
+ }
657
+
658
+ /* ------------------------------ Basic Arithmetic Functions End ------------------------------ */
659
+
660
+ /* ------------------------------ Math Basic Arithmetic Functions Start ------------------------------ */
661
+
662
+ /* This method needs two required arguments and returns p1 + p2. */
663
+ static VALUE r_mpfr_math_add(int argc, VALUE *argv, VALUE self){
664
+ mp_rnd_t rnd;
665
+ mp_prec_t prec;
666
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
667
+ MPFR *ptr_arg1, *ptr_arg2, *ptr_return;
668
+ VALUE val_ret;
669
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
670
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
671
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
672
+
673
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, argv[1]))){
674
+ r_mpfr_get_struct(ptr_arg2, argv[1]);
675
+ mpfr_add(ptr_return, ptr_arg1, ptr_arg2, rnd);
676
+ }else if(TYPE(argv[1]) == T_FIXNUM){
677
+ mpfr_add_si(ptr_return, ptr_arg1, FIX2LONG(argv[1]), rnd);
678
+ }else{
679
+ r_mpfr_get_struct(ptr_arg2, rb_funcall(__mpfr_class__, new, 1, argv[1]));
680
+ mpfr_add(ptr_return, ptr_arg1, ptr_arg2, rnd);
681
+ }
682
+ return val_ret;
683
+ }
684
+
685
+ /* This method needs two required arguments and returns p1 - p2. */
686
+ static VALUE r_mpfr_math_sub(int argc, VALUE *argv, VALUE self){
687
+ mp_rnd_t rnd;
688
+ mp_prec_t prec;
689
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
690
+ MPFR *ptr_arg1, *ptr_arg2, *ptr_return;
691
+ VALUE val_ret;
692
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
693
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
694
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
695
+
696
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, argv[1]))){
697
+ r_mpfr_get_struct(ptr_arg2, argv[1]);
698
+ mpfr_sub(ptr_return, ptr_arg1, ptr_arg2, rnd);
699
+ }else if(TYPE(argv[1]) == T_FIXNUM){
700
+ mpfr_sub_si(ptr_return, ptr_arg1, FIX2LONG(argv[1]), rnd);
701
+ }else{
702
+ r_mpfr_get_struct(ptr_arg2, rb_funcall(__mpfr_class__, new, 1, argv[1]));
703
+ mpfr_sub(ptr_return, ptr_arg1, ptr_arg2, rnd);
704
+ }
705
+ return val_ret;
706
+ }
707
+
708
+ /* This method needs two required arguments and returns p1 - p2. */
709
+ static VALUE r_mpfr_math_mul(int argc, VALUE *argv, VALUE self){
710
+ mp_rnd_t rnd;
711
+ mp_prec_t prec;
712
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
713
+ MPFR *ptr_arg1, *ptr_arg2, *ptr_return;
714
+ VALUE val_ret;
715
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
716
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
717
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
718
+
719
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, argv[1]))){
720
+ r_mpfr_get_struct(ptr_arg2, argv[1]);
721
+ mpfr_mul(ptr_return, ptr_arg1, ptr_arg2, rnd);
722
+ }else if(TYPE(argv[1]) == T_FIXNUM){
723
+ mpfr_mul_si(ptr_return, ptr_arg1, FIX2LONG(argv[1]), rnd);
724
+ }else{
725
+ r_mpfr_get_struct(ptr_arg2, rb_funcall(__mpfr_class__, new, 1, argv[1]));
726
+ mpfr_mul(ptr_return, ptr_arg1, ptr_arg2, rnd);
727
+ }
728
+ return val_ret;
729
+ }
730
+
731
+ /* This method needs two required arguments and returns p1 / p2. */
732
+ static VALUE r_mpfr_math_div(int argc, VALUE *argv, VALUE self){
733
+ mp_rnd_t rnd;
734
+ mp_prec_t prec;
735
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
736
+ MPFR *ptr_arg1, *ptr_arg2, *ptr_return;
737
+ VALUE val_ret;
738
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
739
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
740
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
741
+
742
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, argv[1]))){
743
+ r_mpfr_get_struct(ptr_arg2, argv[1]);
744
+ mpfr_div(ptr_return, ptr_arg1, ptr_arg2, rnd);
745
+ }else if(TYPE(argv[1]) == T_FIXNUM){
746
+ mpfr_div_si(ptr_return, ptr_arg1, FIX2LONG(argv[1]), rnd);
747
+ }else{
748
+ r_mpfr_get_struct(ptr_arg2, rb_funcall(__mpfr_class__, new, 1, argv[1]));
749
+ mpfr_div(ptr_return, ptr_arg1, ptr_arg2, rnd);
750
+ }
751
+ return val_ret;
752
+ }
753
+
754
+ /* mpfr_sqr(ret, p1, rnd). */
755
+ static VALUE r_mpfr_math_sqr(int argc, VALUE *argv, VALUE self){
756
+ mp_rnd_t rnd;
757
+ mp_prec_t prec;
758
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
759
+ MPFR *ptr_arg1, *ptr_return;
760
+ VALUE val_ret;
761
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
762
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
763
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
764
+ mpfr_sqr(ptr_return, ptr_arg1, rnd);
765
+ return val_ret;
766
+ }
767
+
768
+ /* mpfr_sqrt(ret, p1, rnd). */
769
+ static VALUE r_mpfr_math_sqrt(int argc, VALUE *argv, VALUE self){
770
+ mp_rnd_t rnd;
771
+ mp_prec_t prec;
772
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
773
+ MPFR *ptr_arg1, *ptr_return;
774
+ VALUE val_ret;
775
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
776
+
777
+ if(TYPE(argv[0]) == T_FIXNUM){
778
+ long num = FIX2LONG(argv[0]);
779
+ if(num >= 0){
780
+ mpfr_sqrt_ui(ptr_return, (unsigned long)num, rnd);
781
+ }else{
782
+ mpfr_set_nan(ptr_return);
783
+ }
784
+ }else{
785
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
786
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
787
+ mpfr_sqrt(ptr_return, ptr_arg1, rnd);
788
+ }
789
+ return val_ret;
790
+ }
791
+
792
+ /* mpfr_rec_sqrt(ret, p1, rnd). */
793
+ static VALUE r_mpfr_math_rec_sqrt(int argc, VALUE *argv, VALUE self){
794
+ mp_rnd_t rnd;
795
+ mp_prec_t prec;
796
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
797
+ MPFR *ptr_arg1, *ptr_return;
798
+ VALUE val_ret;
799
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
800
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
801
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
802
+ mpfr_rec_sqrt(ptr_return, ptr_arg1, rnd);
803
+ return val_ret;
804
+ }
805
+
806
+ /* mpfr_cbrt(ret, p1, rnd). */
807
+ static VALUE r_mpfr_math_cbrt(int argc, VALUE *argv, VALUE self){
808
+ mp_rnd_t rnd;
809
+ mp_prec_t prec;
810
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
811
+ MPFR *ptr_arg1, *ptr_return;
812
+ VALUE val_ret;
813
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
814
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
815
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
816
+ mpfr_cbrt(ptr_return, ptr_arg1, rnd);
817
+ return val_ret;
818
+ }
819
+
820
+ /* mpfr_root(ret, p1, rnd). */
821
+ static VALUE r_mpfr_math_root(int argc, VALUE *argv, VALUE self){
822
+ mp_rnd_t rnd;
823
+ mp_prec_t prec;
824
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
825
+ MPFR *ptr_arg1, *ptr_return;
826
+ VALUE val_ret;
827
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
828
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
829
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
830
+ int root = NUM2INT(argv[1]);
831
+ if(root > 0){
832
+ mpfr_root(ptr_return, ptr_arg1, root, rnd);
833
+ }else{
834
+ rb_raise(rb_eArgError, "Second argument must be positive Fixnum.");
835
+ }
836
+ return val_ret;
837
+ }
838
+
839
+ /* mpfr_pow(ret, p1, p2, rnd). */
840
+ static VALUE r_mpfr_math_pow(int argc, VALUE *argv, VALUE self){
841
+ mp_rnd_t rnd;
842
+ mp_prec_t prec;
843
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
844
+ MPFR *ptr_arg0, *ptr_arg1, *ptr_return;
845
+ VALUE val_ret;
846
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
847
+ r_mpfr_get_struct(ptr_arg0, tmp_argv0);
848
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
849
+ if(TYPE(argv[1]) == T_FIXNUM){
850
+ mpfr_pow_si(ptr_return, ptr_arg0, FIX2LONG(argv[1]), rnd);
851
+ }else{
852
+ volatile VALUE tmp_argv1 = r_mpfr_new_fr_obj(argv[1]);
853
+ r_mpfr_get_struct(ptr_arg1, tmp_argv1);
854
+ mpfr_pow(ptr_return, ptr_arg0, ptr_arg1, rnd);
855
+ }
856
+ return val_ret;
857
+ }
858
+
859
+ /* mpfr_dim(ret, p1, p2, rnd). */
860
+ static VALUE r_mpfr_math_dim(int argc, VALUE *argv, VALUE self){
861
+ mp_rnd_t rnd;
862
+ mp_prec_t prec;
863
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
864
+ MPFR *ptr_arg0, *ptr_arg1, *ptr_return;
865
+ VALUE val_ret;
866
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
867
+ r_mpfr_get_struct(ptr_arg0, tmp_argv0);
868
+ volatile VALUE tmp_argv1 = r_mpfr_new_fr_obj(argv[1]);
869
+ r_mpfr_get_struct(ptr_arg1, tmp_argv1);
870
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
871
+ mpfr_dim(ptr_return, ptr_arg0, ptr_arg1, rnd);
872
+ return val_ret;
873
+ }
874
+
875
+ /* mpfr_mul_2si(ret, p1, p2, rnd). p2 must be integer. */
876
+ static VALUE r_mpfr_math_mul_2si(int argc, VALUE *argv, VALUE self){
877
+ mp_rnd_t rnd;
878
+ mp_prec_t prec;
879
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
880
+ MPFR *ptr_arg0, *ptr_return;
881
+ VALUE val_ret;
882
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
883
+ r_mpfr_get_struct(ptr_arg0, tmp_argv0);
884
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
885
+ mpfr_mul_2si(ptr_return, ptr_arg0, NUM2INT(argv[1]), rnd);
886
+ return val_ret;
887
+ }
888
+
889
+ /* mpfr_div_2si(ret, p1, p2, rnd). p2 must be integer. */
890
+ static VALUE r_mpfr_math_div_2si(int argc, VALUE *argv, VALUE self){
891
+ mp_rnd_t rnd;
892
+ mp_prec_t prec;
893
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
894
+ MPFR *ptr_arg0, *ptr_return;
895
+ VALUE val_ret;
896
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
897
+ r_mpfr_get_struct(ptr_arg0, tmp_argv0);
898
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
899
+ mpfr_div_2si(ptr_return, ptr_arg0, NUM2INT(argv[1]), rnd);
900
+ return val_ret;
901
+ }
902
+
903
+ /* ------------------------------ Math Basic Arithmetic Functions End ------------------------------ */
904
+
905
+ /* ------------------------------ Comparison Functions Start ------------------------------ */
906
+
907
+ /* Return negative integer if self < other, negative integer if self > other, or 0 if self == other. */
908
+ static VALUE r_mpfr_cmp(VALUE self, VALUE other){
909
+ MPFR *ptr_self, *ptr_other;
910
+ r_mpfr_get_struct(ptr_self, self);
911
+ int val_ret;
912
+
913
+ if(RTEST(rb_funcall(__mpfr_class__, eqq, 1, other))){
914
+ r_mpfr_get_struct(ptr_other, other);
915
+ val_ret = mpfr_cmp(ptr_self, ptr_other);
916
+ }else if(TYPE(other) == T_FIXNUM){
917
+ val_ret = mpfr_cmp_si(ptr_self, FIX2LONG(other));
918
+ }else if(TYPE(other) == T_FLOAT){
919
+ val_ret = mpfr_cmp_d(ptr_self, NUM2DBL(other));
920
+ }else if(TYPE(other) == T_BIGNUM){
921
+ volatile VALUE tmp = rb_funcall(__mpfr_class__, new, 1, other);
922
+ r_mpfr_get_struct(ptr_other, tmp);
923
+ val_ret = mpfr_cmp(ptr_self, ptr_other);
924
+ }else{
925
+ rb_raise(rb_eArgError, "Argument must be MPFR, Fixnum, Float, or Bignum.");
926
+ }
927
+ return INT2FIX(val_ret);
928
+ }
929
+
930
+ /* mpfr_cmp_ui_2exp(self, p1, p2). */
931
+ static VALUE r_mpfr_cmp_ui_2exp(VALUE self, VALUE other, VALUE exp){
932
+ MPFR *ptr_self;
933
+ r_mpfr_get_struct(ptr_self, self);
934
+ VALUE val_ret;
935
+ int i = NUM2INT(other);
936
+ if(i > 0){
937
+ val_ret = INT2FIX(mpfr_cmp_ui_2exp(ptr_self, i, (mp_exp_t)NUM2INT(exp)));
938
+ }else{
939
+ rb_raise(rb_eArgError, "First argument must be positive Fixnum.");
940
+ }
941
+ return val_ret;
942
+ }
943
+
944
+ /* mpfr_cmp_si_2exp(self, p1, p2). */
945
+ static VALUE r_mpfr_cmp_si_2exp(VALUE self, VALUE other, VALUE exp){
946
+ MPFR *ptr_self;
947
+ r_mpfr_get_struct(ptr_self, self);
948
+ return INT2FIX(mpfr_cmp_si_2exp(ptr_self, NUM2INT(other), NUM2INT(exp)));
949
+ }
950
+
951
+ /* mpfr_cmpabs(self, p1). */
952
+ static VALUE r_mpfr_cmpabs(VALUE self, VALUE other){
953
+ MPFR *ptr_self, *ptr_other;
954
+ r_mpfr_get_struct(ptr_self, self);
955
+ volatile VALUE tmp_other = r_mpfr_new_fr_obj(other);
956
+ r_mpfr_get_struct(ptr_other, tmp_other);
957
+ return INT2FIX(mpfr_cmpabs(ptr_self, ptr_other));
958
+ }
959
+
960
+ /* Return true if self is NaN, nil otherwise. */
961
+ static VALUE r_mpfr_nan_p(VALUE self){
962
+ MPFR *ptr_self;
963
+ r_mpfr_get_struct(ptr_self, self);
964
+ if(mpfr_nan_p(ptr_self) != 0){
965
+ return Qtrue;
966
+ }else{
967
+ return Qnil;
968
+ }
969
+ }
970
+
971
+ /* Return true if self is infinity, nil otherwise. */
972
+ static VALUE r_mpfr_inf_p(VALUE self){
973
+ MPFR *ptr_self;
974
+ r_mpfr_get_struct(ptr_self, self);
975
+ return (mpfr_inf_p(ptr_self) != 0 ? Qtrue : Qnil);
976
+ }
977
+
978
+ /* Return 1 if self is plus infinity, -1 if minus infinity. nil otherwise. */
979
+ static VALUE r_mpfr_inf_p2(VALUE self){
980
+ MPFR *ptr_self;
981
+ r_mpfr_get_struct(ptr_self, self);
982
+ if(mpfr_inf_p(ptr_self) != 0){
983
+ return (mpfr_sgn(ptr_self) > 0 ? INT2NUM(1) : INT2NUM(-1));
984
+ }else{
985
+ return Qnil;
986
+ }
987
+ }
988
+
989
+ /* Return true if self is number, nil otherwise */
990
+ static VALUE r_mpfr_number_p(VALUE self){
991
+ MPFR *ptr_self;
992
+ r_mpfr_get_struct(ptr_self, self);
993
+ return (mpfr_number_p(ptr_self) != 0 ? Qtrue : Qnil);
994
+ }
995
+
996
+ /* Return true if self is 0, nil otherwise. */
997
+ static VALUE r_mpfr_zero_p(VALUE self){
998
+ MPFR *ptr_self;
999
+ r_mpfr_get_struct(ptr_self, self);
1000
+ return (mpfr_zero_p(ptr_self) != 0 ? Qtrue : Qnil);
1001
+ }
1002
+
1003
+ /* Return true if self is non zero, nil otherwise. */
1004
+ static VALUE r_mpfr_nonzero_p(VALUE self){
1005
+ MPFR *ptr_self;
1006
+ r_mpfr_get_struct(ptr_self, self);
1007
+ return (mpfr_zero_p(ptr_self) == 0 ? Qtrue : Qnil);
1008
+ }
1009
+
1010
+ /* mpfr_sgn(self). */
1011
+ static VALUE r_mpfr_sgn(VALUE self){
1012
+ MPFR *ptr_self;
1013
+ r_mpfr_get_struct(ptr_self, self);
1014
+ return INT2FIX(mpfr_sgn(ptr_self));
1015
+ }
1016
+
1017
+ /* Return true if self > other, nil otherwise. */
1018
+ static VALUE r_mpfr_greater_p(VALUE self, VALUE other){
1019
+ MPFR *ptr_self, *ptr_other;
1020
+ r_mpfr_get_struct(ptr_self, self);
1021
+ volatile VALUE tmp_other = r_mpfr_new_fr_obj(other);
1022
+ r_mpfr_get_struct(ptr_other, tmp_other);
1023
+ return (mpfr_greater_p(ptr_self, ptr_other) != 0 ? Qtrue : Qnil);
1024
+ }
1025
+
1026
+ /* Return true if self >= other, nil otherwise. */
1027
+ static VALUE r_mpfr_greaterequal_p(VALUE self, VALUE other){
1028
+ MPFR *ptr_self, *ptr_other;
1029
+ r_mpfr_get_struct(ptr_self, self);
1030
+ volatile VALUE tmp_other = r_mpfr_new_fr_obj(other);
1031
+ r_mpfr_get_struct(ptr_other, tmp_other);
1032
+ return (mpfr_greaterequal_p(ptr_self, ptr_other) != 0 ? Qtrue : Qnil);
1033
+ }
1034
+
1035
+ /* Return true if self < other, nil otherwise. */
1036
+ static VALUE r_mpfr_less_p(VALUE self, VALUE other){
1037
+ MPFR *ptr_self, *ptr_other;
1038
+ r_mpfr_get_struct(ptr_self, self);
1039
+ volatile VALUE tmp_other = r_mpfr_new_fr_obj(other);
1040
+ r_mpfr_get_struct(ptr_other, tmp_other);
1041
+ return (mpfr_less_p(ptr_self, ptr_other) != 0 ? Qtrue : Qnil);
1042
+ }
1043
+
1044
+ /* Return true if self <= other, nil otherwise. */
1045
+ static VALUE r_mpfr_lessequal_p(VALUE self, VALUE other){
1046
+ MPFR *ptr_self, *ptr_other;
1047
+ r_mpfr_get_struct(ptr_self, self);
1048
+ volatile VALUE tmp_other = r_mpfr_new_fr_obj(other);
1049
+ r_mpfr_get_struct(ptr_other, tmp_other);
1050
+ return (mpfr_lessequal_p(ptr_self, ptr_other) != 0 ? Qtrue : Qnil);
1051
+ }
1052
+
1053
+ /* Return true if self < other or self > other, nil otherwise. */
1054
+ static VALUE r_mpfr_lessgreater_p(VALUE self, VALUE other){
1055
+ MPFR *ptr_self, *ptr_other;
1056
+ r_mpfr_get_struct(ptr_self, self);
1057
+ volatile VALUE tmp_other = r_mpfr_new_fr_obj(other);
1058
+ r_mpfr_get_struct(ptr_other, tmp_other);
1059
+ return (mpfr_lessgreater_p(ptr_self, ptr_other) != 0 ? Qtrue : Qnil);
1060
+ }
1061
+
1062
+ /* Return true if self == other, nil otherwise. */
1063
+ static VALUE r_mpfr_equal_p(VALUE self, VALUE other){
1064
+ MPFR *ptr_self, *ptr_other;
1065
+ r_mpfr_get_struct(ptr_self, self);
1066
+ volatile VALUE tmp_other = r_mpfr_new_fr_obj(other);
1067
+ r_mpfr_get_struct(ptr_other, tmp_other);
1068
+ return (mpfr_equal_p(ptr_self, ptr_other) != 0 ? Qtrue : Qnil);
1069
+ }
1070
+
1071
+ /* Return true if self or other is a NaN, nil otherwise */
1072
+ static VALUE r_mpfr_unordered_p(VALUE self, VALUE other){
1073
+ MPFR *ptr_self, *ptr_other;
1074
+ r_mpfr_get_struct(ptr_self, self);
1075
+ volatile VALUE tmp_other = r_mpfr_new_fr_obj(other);
1076
+ r_mpfr_get_struct(ptr_other, tmp_other);
1077
+ return (mpfr_unordered_p(ptr_self, ptr_other) != 0 ? Qtrue : Qnil);
1078
+ }
1079
+
1080
+ /* ------------------------------ Comparison Functions End ------------------------------ */
1081
+
1082
+ /* ------------------------------ Integer Related Functions Start ------------------------------ */
1083
+
1084
+ static VALUE r_mpfr_m_rint(int argc, VALUE *argv, VALUE self){
1085
+ mp_rnd_t rnd;
1086
+ mp_prec_t prec;
1087
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
1088
+ MPFR *ptr_self, *ptr_return;
1089
+ VALUE val_ret;
1090
+ r_mpfr_get_struct(ptr_self, self);
1091
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1092
+ mpfr_rint(ptr_return, ptr_self, rnd);
1093
+ return val_ret;
1094
+ }
1095
+
1096
+ static VALUE r_mpfr_m_ceil(int argc, VALUE *argv, VALUE self){
1097
+ mp_prec_t prec = r_mpfr_prec_from_optional_argument(0, 1, argc, argv);
1098
+ MPFR *ptr_self, *ptr_return;
1099
+ VALUE val_ret;
1100
+ r_mpfr_get_struct(ptr_self, self);
1101
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1102
+ mpfr_ceil(ptr_return, ptr_self);
1103
+ return val_ret;
1104
+ }
1105
+
1106
+ static VALUE r_mpfr_m_floor(int argc, VALUE *argv, VALUE self){
1107
+ mp_prec_t prec = r_mpfr_prec_from_optional_argument(0, 1, argc, argv);
1108
+ MPFR *ptr_self, *ptr_return;
1109
+ VALUE val_ret;
1110
+ r_mpfr_get_struct(ptr_self, self);
1111
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1112
+ mpfr_floor(ptr_return, ptr_self);
1113
+ return val_ret;
1114
+ }
1115
+
1116
+ static VALUE r_mpfr_m_round(int argc, VALUE *argv, VALUE self){
1117
+ mp_prec_t prec = r_mpfr_prec_from_optional_argument(0, 1, argc, argv);
1118
+ MPFR *ptr_self, *ptr_return;
1119
+ VALUE val_ret;
1120
+ r_mpfr_get_struct(ptr_self, self);
1121
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1122
+ mpfr_round(ptr_return, ptr_self);
1123
+ return val_ret;
1124
+ }
1125
+
1126
+ static VALUE r_mpfr_m_trunc(int argc, VALUE *argv, VALUE self){
1127
+ mp_prec_t prec = r_mpfr_prec_from_optional_argument(0, 1, argc, argv);
1128
+ MPFR *ptr_self, *ptr_return;
1129
+ VALUE val_ret;
1130
+ r_mpfr_get_struct(ptr_self, self);
1131
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1132
+ mpfr_trunc(ptr_return, ptr_self);
1133
+ return val_ret;
1134
+ }
1135
+
1136
+ static VALUE r_mpfr_rint_ceil(int argc, VALUE *argv, VALUE self){
1137
+ mp_rnd_t rnd;
1138
+ mp_prec_t prec;
1139
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
1140
+ MPFR *ptr_self, *ptr_return;
1141
+ VALUE val_ret;
1142
+ r_mpfr_get_struct(ptr_self, self);
1143
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1144
+ mpfr_rint_ceil(ptr_return, ptr_self, rnd);
1145
+ return val_ret;
1146
+ }
1147
+
1148
+ static VALUE r_mpfr_rint_floor(int argc, VALUE *argv, VALUE self){
1149
+ mp_rnd_t rnd;
1150
+ mp_prec_t prec;
1151
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
1152
+ MPFR *ptr_self, *ptr_return;
1153
+ VALUE val_ret;
1154
+ r_mpfr_get_struct(ptr_self, self);
1155
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1156
+ mpfr_rint_floor(ptr_return, ptr_self, rnd);
1157
+ return val_ret;
1158
+ }
1159
+
1160
+ static VALUE r_mpfr_rint_round(int argc, VALUE *argv, VALUE self){
1161
+ mp_rnd_t rnd;
1162
+ mp_prec_t prec;
1163
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
1164
+ MPFR *ptr_self, *ptr_return;
1165
+ VALUE val_ret;
1166
+ r_mpfr_get_struct(ptr_self, self);
1167
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1168
+ mpfr_rint_round(ptr_return, ptr_self, rnd);
1169
+ return val_ret;
1170
+ }
1171
+
1172
+ static VALUE r_mpfr_rint_trunc(int argc, VALUE *argv, VALUE self){
1173
+ mp_rnd_t rnd;
1174
+ mp_prec_t prec;
1175
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
1176
+ MPFR *ptr_self, *ptr_return;
1177
+ VALUE val_ret;
1178
+ r_mpfr_get_struct(ptr_self, self);
1179
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1180
+ mpfr_rint_trunc(ptr_return, ptr_self, rnd);
1181
+ return val_ret;
1182
+ }
1183
+
1184
+ static VALUE r_mpfr_frac(int argc, VALUE *argv, VALUE self){
1185
+ mp_rnd_t rnd;
1186
+ mp_prec_t prec;
1187
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
1188
+ MPFR *ptr_self, *ptr_return;
1189
+ VALUE val_ret;
1190
+ r_mpfr_get_struct(ptr_self, self);
1191
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1192
+ mpfr_frac(ptr_return, ptr_self, rnd);
1193
+ return val_ret;
1194
+ }
1195
+
1196
+ /* Return [ret1, ret2] such that mpfr_modf(ret1, ret2, self, rnd). */
1197
+ static VALUE r_mpfr_modf(int argc, VALUE *argv, VALUE self){
1198
+ mp_rnd_t rnd;
1199
+ mp_prec_t prec;
1200
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
1201
+ MPFR *ptr_self, *ptr_return1, *ptr_return2;
1202
+ VALUE val_ret1, val_ret2;
1203
+ r_mpfr_get_struct(ptr_self, self);
1204
+ r_mpfr_make_struct_init2(val_ret1, ptr_return1, prec);
1205
+ r_mpfr_make_struct_init2(val_ret2, ptr_return2, prec);
1206
+ mpfr_modf(ptr_return1, ptr_return2, ptr_self, rnd);
1207
+ return rb_ary_new3(2, val_ret1, val_ret2);
1208
+ }
1209
+
1210
+ static VALUE r_mpfr_fmod(int argc, VALUE *argv, VALUE self){
1211
+ mp_rnd_t rnd;
1212
+ mp_prec_t prec;
1213
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1214
+ MPFR *ptr_self, *ptr_other, *ptr_return;
1215
+ VALUE val_ret;
1216
+ r_mpfr_get_struct(ptr_self, self);
1217
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1218
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1219
+ r_mpfr_get_struct(ptr_other, tmp_argv0);
1220
+ mpfr_fmod(ptr_return, ptr_self, ptr_other, rnd);
1221
+ return val_ret;
1222
+ }
1223
+
1224
+ static VALUE r_mpfr_remainder(int argc, VALUE *argv, VALUE self){
1225
+ mp_rnd_t rnd;
1226
+ mp_prec_t prec;
1227
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1228
+ MPFR *ptr_self, *ptr_other, *ptr_return;
1229
+ VALUE val_ret;
1230
+ r_mpfr_get_struct(ptr_self, self);
1231
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1232
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1233
+ r_mpfr_get_struct(ptr_other, tmp_argv0);
1234
+ mpfr_remainder(ptr_return, ptr_self, ptr_other, rnd);
1235
+ return val_ret;
1236
+ }
1237
+
1238
+ static VALUE r_mpfr_remainder2(VALUE self, VALUE other){
1239
+ MPFR *ptr_self, *ptr_other, *ptr_return;
1240
+ VALUE val_ret;
1241
+ r_mpfr_get_struct(ptr_self, self);
1242
+ r_mpfr_make_struct_init(val_ret, ptr_return);
1243
+ volatile VALUE tmp_other = r_mpfr_new_fr_obj(other);
1244
+ r_mpfr_get_struct(ptr_other, tmp_other);
1245
+ mpfr_remainder(ptr_return, ptr_self, ptr_other, mpfr_get_default_rounding_mode());
1246
+ return val_ret;
1247
+ }
1248
+
1249
+ static VALUE r_mpfr_remquo(int argc, VALUE *argv, VALUE self){
1250
+ mp_rnd_t rnd;
1251
+ mp_prec_t prec;
1252
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1253
+ MPFR *ptr_self, *ptr_other, *ptr_return;
1254
+ VALUE val_ret;
1255
+ long q;
1256
+ r_mpfr_get_struct(ptr_self, self);
1257
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1258
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1259
+ r_mpfr_get_struct(ptr_other, tmp_argv0);
1260
+ mpfr_remquo(ptr_return, &q, ptr_self, ptr_other, rnd);
1261
+ return rb_ary_new3(2, val_ret, LONG2FIX(q));
1262
+ }
1263
+
1264
+ static VALUE r_mpfr_integer_p(VALUE self){
1265
+ MPFR *ptr_self;
1266
+ r_mpfr_get_struct(ptr_self, self);
1267
+ return (mpfr_integer_p(ptr_self) != 0 ? Qtrue : Qnil);
1268
+ }
1269
+
1270
+ /* ------------------------------ Integer Related Functions End ------------------------------ */
1271
+
1272
+ /* ------------------------------ Miscellaneous Functions Start ------------------------------ */
1273
+
1274
+ static VALUE r_mpfr_nexttoward(VALUE self, VALUE other){
1275
+ MPFR *ptr_self, *ptr_other;
1276
+ r_mpfr_get_struct(ptr_self, self);
1277
+ volatile VALUE tmp_other = r_mpfr_new_fr_obj(other);
1278
+ r_mpfr_get_struct(ptr_other, tmp_other);
1279
+ mpfr_nexttoward(ptr_self, ptr_other);
1280
+ return self;
1281
+ }
1282
+
1283
+ static VALUE r_mpfr_nextabove(VALUE self){
1284
+ MPFR *ptr_self;
1285
+ r_mpfr_get_struct(ptr_self, self);
1286
+ mpfr_nextabove(ptr_self);
1287
+ return self;
1288
+ }
1289
+
1290
+ static VALUE r_mpfr_nextbelow(VALUE self){
1291
+ MPFR *ptr_self;
1292
+ r_mpfr_get_struct(ptr_self, self);
1293
+ mpfr_nextbelow(ptr_self);
1294
+ return self;
1295
+ }
1296
+
1297
+ static VALUE r_mpfr_get_exp(VALUE self){
1298
+ MPFR *ptr_self;
1299
+ r_mpfr_get_struct(ptr_self, self);
1300
+ if(mpfr_number_p(ptr_self) != 0){
1301
+ return INT2FIX(mpfr_get_exp(ptr_self));
1302
+ }else{
1303
+ return Qnil;
1304
+ }
1305
+ }
1306
+
1307
+ static VALUE r_mpfr_set_exp(VALUE self, VALUE arg_exp){
1308
+ MPFR *ptr_self;
1309
+ r_mpfr_get_struct(ptr_self, self);
1310
+ mp_exp_t exp = NUM2INT(arg_exp);
1311
+ mpfr_set_exp(ptr_self, exp);
1312
+ return self;
1313
+ }
1314
+
1315
+ static VALUE r_mpfr_signbit(VALUE self){
1316
+ MPFR *ptr_self;
1317
+ r_mpfr_get_struct(ptr_self, self);
1318
+ return INT2FIX(mpfr_signbit(ptr_self));
1319
+ }
1320
+
1321
+ static VALUE r_mpfr_setsign(int argc, VALUE *argv, VALUE self){
1322
+ mp_rnd_t rnd;
1323
+ mp_prec_t prec;
1324
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
1325
+ MPFR *ptr_self, *ptr_return;
1326
+ VALUE val_ret;
1327
+ r_mpfr_get_struct(ptr_self, self);
1328
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1329
+ int s = NUM2INT(argv[0]);
1330
+ mpfr_setsign(ptr_return, ptr_self, s, rnd);
1331
+ return val_ret;
1332
+ }
1333
+
1334
+ static VALUE r_mpfr_copysign(int argc, VALUE *argv, VALUE self){
1335
+ mp_rnd_t rnd;
1336
+ mp_prec_t prec;
1337
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
1338
+ MPFR *ptr_self, *ptr_return, *ptr_arg;
1339
+ VALUE val_ret;
1340
+ r_mpfr_get_struct(ptr_self, self);
1341
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1342
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1343
+ r_mpfr_get_struct(ptr_arg, tmp_argv0);
1344
+ mpfr_copysign(ptr_return, ptr_self, ptr_arg, rnd);
1345
+ return val_ret;
1346
+ }
1347
+
1348
+ /* ------------------------------ Miscellaneous Functions End ------------------------------ */
1349
+
1350
+ /* ------------------------------ Rounding Mode Related Functions Start ------------------------------ */
1351
+
1352
+ static VALUE r_mpfr_prec_round(int argc, VALUE *argv, VALUE self){
1353
+ mp_rnd_t rnd;
1354
+ mp_prec_t prec;
1355
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
1356
+ MPFR *ptr_self, *ptr_return;
1357
+ VALUE val_ret;
1358
+ r_mpfr_get_struct(ptr_self, self);
1359
+ r_mpfr_make_struct_init2(val_ret, ptr_return, mpfr_get_prec(ptr_self));
1360
+ mpfr_set(ptr_return, ptr_self, mpfr_get_default_prec());
1361
+ mpfr_prec_round(ptr_return, prec, rnd);
1362
+ return val_ret;
1363
+ }
1364
+
1365
+ static VALUE r_mpfr_prec_round2(int argc, VALUE *argv, VALUE self){
1366
+ mp_rnd_t rnd;
1367
+ mp_prec_t prec;
1368
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
1369
+ MPFR *ptr_self;
1370
+ r_mpfr_get_struct(ptr_self, self);
1371
+ mpfr_prec_round(ptr_self, prec, rnd);
1372
+ return self;
1373
+ }
1374
+
1375
+ /* ------------------------------ Rounding Mode Related Functions End ------------------------------ */
1376
+
1377
+ /* ------------------------------ Special Functions Start ------------------------------ */
1378
+
1379
+ /*
1380
+ Ruby MPFR saves the returned value of Special Functions (log, sin, cos, etc)
1381
+ in MPFR library to class variable of class MPFR.
1382
+ This method returns that class variable meaning the returned value of Special Functions in MPFR library.
1383
+ See the MPFR reference for the kind of Special Functions and the meaning of the returned value of Special Functions.
1384
+ */
1385
+ static VALUE r_mpfr_get_special_func_state(VALUE self){ return rb_cv_get(r_mpfr_class, SPECIAL_FUNC_STATE); }
1386
+
1387
+ static void r_mpfr_set_special_func_state(int num){ rb_cv_set(r_mpfr_class, SPECIAL_FUNC_STATE, INT2NUM(num)); }
1388
+
1389
+ /* mpfr_log(ret, p1, rnd). */
1390
+ static VALUE r_mpfr_math_log(int argc, VALUE *argv, VALUE self){
1391
+ mp_rnd_t rnd;
1392
+ mp_prec_t prec;
1393
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1394
+ MPFR *ptr_arg1, *ptr_return;
1395
+ VALUE val_ret;
1396
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1397
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1398
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1399
+ r_mpfr_set_special_func_state(mpfr_log(ptr_return, ptr_arg1, rnd));
1400
+ return val_ret;
1401
+ }
1402
+
1403
+ /* mpfr_log2(ret, p1, rnd). */
1404
+ static VALUE r_mpfr_math_log2(int argc, VALUE *argv, VALUE self){
1405
+ mp_rnd_t rnd;
1406
+ mp_prec_t prec;
1407
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1408
+ MPFR *ptr_arg1, *ptr_return;
1409
+ VALUE val_ret;
1410
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1411
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1412
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1413
+ r_mpfr_set_special_func_state(mpfr_log2(ptr_return, ptr_arg1, rnd));
1414
+ return val_ret;
1415
+ }
1416
+
1417
+ /* mpfr_log10(ret, p1, rnd). */
1418
+ static VALUE r_mpfr_math_log10(int argc, VALUE *argv, VALUE self){
1419
+ mp_rnd_t rnd;
1420
+ mp_prec_t prec;
1421
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1422
+ MPFR *ptr_arg1, *ptr_return;
1423
+ VALUE val_ret;
1424
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1425
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1426
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1427
+ r_mpfr_set_special_func_state(mpfr_log10(ptr_return, ptr_arg1, rnd));
1428
+ return val_ret;
1429
+ }
1430
+
1431
+ /* mpfr_exp(ret, p1, rnd). */
1432
+ static VALUE r_mpfr_math_exp(int argc, VALUE *argv, VALUE self){
1433
+ mp_rnd_t rnd;
1434
+ mp_prec_t prec;
1435
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1436
+ MPFR *ptr_arg1, *ptr_return;
1437
+ VALUE val_ret;
1438
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1439
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1440
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1441
+ r_mpfr_set_special_func_state(mpfr_exp(ptr_return, ptr_arg1, rnd));
1442
+ return val_ret;
1443
+ }
1444
+
1445
+ /* mpfr_exp2(ret, p1, rnd). */
1446
+ static VALUE r_mpfr_math_exp2(int argc, VALUE *argv, VALUE self){
1447
+ mp_rnd_t rnd;
1448
+ mp_prec_t prec;
1449
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1450
+ MPFR *ptr_arg1, *ptr_return;
1451
+ VALUE val_ret;
1452
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1453
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1454
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1455
+ r_mpfr_set_special_func_state(mpfr_exp2(ptr_return, ptr_arg1, rnd));
1456
+ return val_ret;
1457
+ }
1458
+
1459
+ /* mpfr_exp10(ret, p1, rnd). */
1460
+ static VALUE r_mpfr_math_exp10(int argc, VALUE *argv, VALUE self){
1461
+ mp_rnd_t rnd;
1462
+ mp_prec_t prec;
1463
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1464
+ MPFR *ptr_arg1, *ptr_return;
1465
+ VALUE val_ret;
1466
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1467
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1468
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1469
+ r_mpfr_set_special_func_state(mpfr_exp10(ptr_return, ptr_arg1, rnd));
1470
+ return val_ret;
1471
+ }
1472
+
1473
+ /* mpfr_cos(ret, p1, rnd). */
1474
+ static VALUE r_mpfr_math_cos(int argc, VALUE *argv, VALUE self){
1475
+ mp_rnd_t rnd;
1476
+ mp_prec_t prec;
1477
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1478
+ MPFR *ptr_arg1, *ptr_return;
1479
+ VALUE val_ret;
1480
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1481
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1482
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1483
+ r_mpfr_set_special_func_state(mpfr_cos(ptr_return, ptr_arg1, rnd));
1484
+ return val_ret;
1485
+ }
1486
+
1487
+ /* mpfr_sin(ret, p1, rnd). */
1488
+ static VALUE r_mpfr_math_sin(int argc, VALUE *argv, VALUE self){
1489
+ mp_rnd_t rnd;
1490
+ mp_prec_t prec;
1491
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1492
+ MPFR *ptr_arg1, *ptr_return;
1493
+ VALUE val_ret;
1494
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1495
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1496
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1497
+ r_mpfr_set_special_func_state(mpfr_sin(ptr_return, ptr_arg1, rnd));
1498
+ return val_ret;
1499
+ }
1500
+
1501
+ /* mpfr_tan(ret, p1, rnd). */
1502
+ static VALUE r_mpfr_math_tan(int argc, VALUE *argv, VALUE self){
1503
+ mp_rnd_t rnd;
1504
+ mp_prec_t prec;
1505
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1506
+ MPFR *ptr_arg1, *ptr_return;
1507
+ VALUE val_ret;
1508
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1509
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1510
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1511
+ r_mpfr_set_special_func_state(mpfr_tan(ptr_return, ptr_arg1, rnd));
1512
+ return val_ret;
1513
+ }
1514
+
1515
+ /* mpfr_sec(ret, p1, rnd). */
1516
+ static VALUE r_mpfr_math_sec(int argc, VALUE *argv, VALUE self){
1517
+ mp_rnd_t rnd;
1518
+ mp_prec_t prec;
1519
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1520
+ MPFR *ptr_arg1, *ptr_return;
1521
+ VALUE val_ret;
1522
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1523
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1524
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1525
+ r_mpfr_set_special_func_state(mpfr_sec(ptr_return, ptr_arg1, rnd));
1526
+ return val_ret;
1527
+ }
1528
+
1529
+ /* mpfr_csc(ret, p1, rnd). */
1530
+ static VALUE r_mpfr_math_csc(int argc, VALUE *argv, VALUE self){
1531
+ mp_rnd_t rnd;
1532
+ mp_prec_t prec;
1533
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1534
+ MPFR *ptr_arg1, *ptr_return;
1535
+ VALUE val_ret;
1536
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1537
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1538
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1539
+ r_mpfr_set_special_func_state(mpfr_csc(ptr_return, ptr_arg1, rnd));
1540
+ return val_ret;
1541
+ }
1542
+
1543
+ /* mpfr_cot(ret, p1, rnd). */
1544
+ static VALUE r_mpfr_math_cot(int argc, VALUE *argv, VALUE self){
1545
+ mp_rnd_t rnd;
1546
+ mp_prec_t prec;
1547
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1548
+ MPFR *ptr_arg1, *ptr_return;
1549
+ VALUE val_ret;
1550
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1551
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1552
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1553
+ r_mpfr_set_special_func_state(mpfr_cot(ptr_return, ptr_arg1, rnd));
1554
+ return val_ret;
1555
+ }
1556
+
1557
+ /* Return [sin, cos] such as mpfr_sin_cos(sin, cos, p1, rnd). */
1558
+ static VALUE r_mpfr_math_sin_cos(int argc, VALUE *argv, VALUE self){
1559
+ mp_rnd_t rnd;
1560
+ mp_prec_t prec;
1561
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
1562
+ MPFR *ptr_arg1, *ptr_return1, *ptr_return2;
1563
+ VALUE val_ret1, val_ret2;
1564
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1565
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1566
+ r_mpfr_make_struct_init2(val_ret1, ptr_return1, prec);
1567
+ r_mpfr_make_struct_init2(val_ret2, ptr_return2, prec);
1568
+ mpfr_sin_cos(ptr_return1, ptr_return2, ptr_arg1, rnd);
1569
+ return rb_ary_new3(2, ptr_return1, ptr_return2);
1570
+ }
1571
+
1572
+ /* mpfr_acos(ret, p1, rnd). */
1573
+ static VALUE r_mpfr_math_acos(int argc, VALUE *argv, VALUE self){
1574
+ mp_rnd_t rnd;
1575
+ mp_prec_t prec;
1576
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1577
+ MPFR *ptr_arg1, *ptr_return;
1578
+ VALUE val_ret;
1579
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1580
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1581
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1582
+ r_mpfr_set_special_func_state(mpfr_acos(ptr_return, ptr_arg1, rnd));
1583
+ return val_ret;
1584
+ }
1585
+
1586
+ /* mpfr_asin(ret, p1, rnd). */
1587
+ static VALUE r_mpfr_math_asin(int argc, VALUE *argv, VALUE self){
1588
+ mp_rnd_t rnd;
1589
+ mp_prec_t prec;
1590
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1591
+ MPFR *ptr_arg1, *ptr_return;
1592
+ VALUE val_ret;
1593
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1594
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1595
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1596
+ r_mpfr_set_special_func_state(mpfr_asin(ptr_return, ptr_arg1, rnd));
1597
+ return val_ret;
1598
+ }
1599
+
1600
+ /* mpfr_atan(ret, p1, rnd). */
1601
+ static VALUE r_mpfr_math_atan(int argc, VALUE *argv, VALUE self){
1602
+ mp_rnd_t rnd;
1603
+ mp_prec_t prec;
1604
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1605
+ MPFR *ptr_arg1, *ptr_return;
1606
+ VALUE val_ret;
1607
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1608
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1609
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1610
+ r_mpfr_set_special_func_state(mpfr_atan(ptr_return, ptr_arg1, rnd));
1611
+ return val_ret;
1612
+ }
1613
+
1614
+ /* mpfr_atan2(ret, p1, rnd). */
1615
+ static VALUE r_mpfr_math_atan2(int argc, VALUE *argv, VALUE self){
1616
+ mp_rnd_t rnd;
1617
+ mp_prec_t prec;
1618
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
1619
+ MPFR *ptr_arg0, *ptr_arg1, *ptr_return;
1620
+ VALUE val_ret;
1621
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1622
+ r_mpfr_get_struct(ptr_arg0, tmp_argv0);
1623
+ volatile VALUE tmp_argv1 = r_mpfr_new_fr_obj(argv[1]);
1624
+ r_mpfr_get_struct(ptr_arg1, tmp_argv1);
1625
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1626
+ r_mpfr_set_special_func_state(mpfr_atan2(ptr_return, ptr_arg0, ptr_arg1, rnd));
1627
+ return val_ret;
1628
+ }
1629
+
1630
+ /* mpfr_cosh(ret, p1, rnd). */
1631
+ static VALUE r_mpfr_math_cosh(int argc, VALUE *argv, VALUE self){
1632
+ mp_rnd_t rnd;
1633
+ mp_prec_t prec;
1634
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1635
+ MPFR *ptr_arg1, *ptr_return;
1636
+ VALUE val_ret;
1637
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1638
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1639
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1640
+ r_mpfr_set_special_func_state(mpfr_cosh(ptr_return, ptr_arg1, rnd));
1641
+ return val_ret;
1642
+ }
1643
+
1644
+ /* mpfr_sinh(ret, p1, rnd). */
1645
+ static VALUE r_mpfr_math_sinh(int argc, VALUE *argv, VALUE self){
1646
+ mp_rnd_t rnd;
1647
+ mp_prec_t prec;
1648
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1649
+ MPFR *ptr_arg1, *ptr_return;
1650
+ VALUE val_ret;
1651
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1652
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1653
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1654
+ r_mpfr_set_special_func_state(mpfr_sinh(ptr_return, ptr_arg1, rnd));
1655
+ return val_ret;
1656
+ }
1657
+
1658
+ /* mpfr_tanh(ret, p1, rnd). */
1659
+ static VALUE r_mpfr_math_tanh(int argc, VALUE *argv, VALUE self){
1660
+ mp_rnd_t rnd;
1661
+ mp_prec_t prec;
1662
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1663
+ MPFR *ptr_arg1, *ptr_return;
1664
+ VALUE val_ret;
1665
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1666
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1667
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1668
+ r_mpfr_set_special_func_state(mpfr_tanh(ptr_return, ptr_arg1, rnd));
1669
+ return val_ret;
1670
+ }
1671
+
1672
+ /* Return array contaning hyperbolic sine and hyperbolic cosine by mpfr_sinh_cosh(ret1, ret2, arg1, rnd). */
1673
+ static VALUE r_mpfr_math_sinh_cosh(int argc, VALUE *argv, VALUE self){
1674
+ mp_rnd_t rnd;
1675
+ mp_prec_t prec;
1676
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1677
+ MPFR *ptr_arg1, *ptr_return1, *ptr_return2;
1678
+ VALUE val_ret1, val_ret2;
1679
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1680
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1681
+ r_mpfr_make_struct_init2(val_ret1, ptr_return1, prec);
1682
+ r_mpfr_make_struct_init2(val_ret2, ptr_return2, prec);
1683
+ r_mpfr_set_special_func_state(mpfr_sinh_cosh(ptr_return1, ptr_return2, ptr_arg1, rnd));
1684
+ return rb_ary_new3(2, val_ret1, val_ret2);
1685
+ }
1686
+
1687
+ /* mpfr_acosh(ret, p1, rnd). */
1688
+ static VALUE r_mpfr_math_acosh(int argc, VALUE *argv, VALUE self){
1689
+ mp_rnd_t rnd;
1690
+ mp_prec_t prec;
1691
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1692
+ MPFR *ptr_arg1, *ptr_return;
1693
+ VALUE val_ret;
1694
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1695
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1696
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1697
+ r_mpfr_set_special_func_state(mpfr_acosh(ptr_return, ptr_arg1, rnd));
1698
+ return val_ret;
1699
+ }
1700
+
1701
+ /* mpfr_asinh(ret, p1, rnd). */
1702
+ static VALUE r_mpfr_math_asinh(int argc, VALUE *argv, VALUE self){
1703
+ mp_rnd_t rnd;
1704
+ mp_prec_t prec;
1705
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1706
+ MPFR *ptr_arg1, *ptr_return;
1707
+ VALUE val_ret;
1708
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1709
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1710
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1711
+ r_mpfr_set_special_func_state(mpfr_asinh(ptr_return, ptr_arg1, rnd));
1712
+ return val_ret;
1713
+ }
1714
+
1715
+ /* mpfr_atanh(ret, p1, rnd). */
1716
+ static VALUE r_mpfr_math_atanh(int argc, VALUE *argv, VALUE self){
1717
+ mp_rnd_t rnd;
1718
+ mp_prec_t prec;
1719
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1720
+ MPFR *ptr_arg1, *ptr_return;
1721
+ VALUE val_ret;
1722
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1723
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1724
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1725
+ r_mpfr_set_special_func_state(mpfr_atanh(ptr_return, ptr_arg1, rnd));
1726
+ return val_ret;
1727
+ }
1728
+
1729
+ /* mpfr_fac_ui(ret, p1, rnd). */
1730
+ static VALUE r_mpfr_math_fac_ui(int argc, VALUE *argv, VALUE self){
1731
+ mp_rnd_t rnd;
1732
+ mp_prec_t prec;
1733
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1734
+ MPFR *ptr_return;
1735
+ VALUE val_ret;
1736
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1737
+ int num = NUM2INT(argv[0]);
1738
+ if(num >= 0){
1739
+ r_mpfr_set_special_func_state(mpfr_fac_ui(ptr_return, num, rnd));
1740
+ }else{
1741
+ rb_raise(rb_eArgError, "First argument must be positive Fixnum.");
1742
+ }
1743
+ return val_ret;
1744
+ }
1745
+
1746
+ /* mpfr_log1p(ret, p1, rnd). */
1747
+ static VALUE r_mpfr_math_log1p(int argc, VALUE *argv, VALUE self){
1748
+ mp_rnd_t rnd;
1749
+ mp_prec_t prec;
1750
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1751
+ MPFR *ptr_arg1, *ptr_return;
1752
+ VALUE val_ret;
1753
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1754
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1755
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1756
+ r_mpfr_set_special_func_state(mpfr_log1p(ptr_return, ptr_arg1, rnd));
1757
+ return val_ret;
1758
+ }
1759
+
1760
+ /* mpfr_expm1(ret, p1, rnd). */
1761
+ static VALUE r_mpfr_math_expm1(int argc, VALUE *argv, VALUE self){
1762
+ mp_rnd_t rnd;
1763
+ mp_prec_t prec;
1764
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1765
+ MPFR *ptr_arg1, *ptr_return;
1766
+ VALUE val_ret;
1767
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1768
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1769
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1770
+ r_mpfr_set_special_func_state(mpfr_expm1(ptr_return, ptr_arg1, rnd));
1771
+ return val_ret;
1772
+ }
1773
+
1774
+ /* mpfr_eint(ret, p1, rnd). */
1775
+ static VALUE r_mpfr_math_eint(int argc, VALUE *argv, VALUE self){
1776
+ mp_rnd_t rnd;
1777
+ mp_prec_t prec;
1778
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1779
+ MPFR *ptr_arg1, *ptr_return;
1780
+ VALUE val_ret;
1781
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1782
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1783
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1784
+ r_mpfr_set_special_func_state(mpfr_eint(ptr_return, ptr_arg1, rnd));
1785
+ return val_ret;
1786
+ }
1787
+
1788
+ /* mpfr_li2(ret, p1, rnd). */
1789
+ static VALUE r_mpfr_math_li2(int argc, VALUE *argv, VALUE self){
1790
+ mp_rnd_t rnd;
1791
+ mp_prec_t prec;
1792
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1793
+ MPFR *ptr_arg1, *ptr_return;
1794
+ VALUE val_ret;
1795
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1796
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1797
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1798
+ r_mpfr_set_special_func_state(mpfr_li2(ptr_return, ptr_arg1, rnd));
1799
+ return val_ret;
1800
+ }
1801
+
1802
+ /* mpfr_gamma(ret, p1, rnd). */
1803
+ static VALUE r_mpfr_math_gamma(int argc, VALUE *argv, VALUE self){
1804
+ mp_rnd_t rnd;
1805
+ mp_prec_t prec;
1806
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1807
+ MPFR *ptr_arg1, *ptr_return;
1808
+ VALUE val_ret;
1809
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1810
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1811
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1812
+ r_mpfr_set_special_func_state(mpfr_gamma(ptr_return, ptr_arg1, rnd));
1813
+ return val_ret;
1814
+ }
1815
+
1816
+ /* mpfr_lngamma(ret, p1, rnd). */
1817
+ static VALUE r_mpfr_math_lngamma(int argc, VALUE *argv, VALUE self){
1818
+ mp_rnd_t rnd;
1819
+ mp_prec_t prec;
1820
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1821
+ MPFR *ptr_arg1, *ptr_return;
1822
+ VALUE val_ret;
1823
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1824
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1825
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1826
+ r_mpfr_set_special_func_state(mpfr_lngamma(ptr_return, ptr_arg1, rnd));
1827
+ return val_ret;
1828
+ }
1829
+
1830
+ static VALUE r_mpfr_math_lgamma(int argc, VALUE *argv, VALUE self){
1831
+ mp_rnd_t rnd;
1832
+ mp_prec_t prec;
1833
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1834
+ MPFR *ptr_arg1, *ptr_return;
1835
+ VALUE val_ret;
1836
+ int singp;
1837
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1838
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1839
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1840
+ r_mpfr_set_special_func_state(mpfr_lgamma(ptr_return, &singp, ptr_arg1, rnd));
1841
+ return rb_ary_new3(2, val_ret, INT2FIX(singp));
1842
+ }
1843
+
1844
+ /* mpfr_zeta(ret, p1, rnd). */
1845
+ static VALUE r_mpfr_math_zeta(int argc, VALUE *argv, VALUE self){
1846
+ mp_rnd_t rnd;
1847
+ mp_prec_t prec;
1848
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1849
+ MPFR *ptr_arg1, *ptr_return;
1850
+ VALUE val_ret;
1851
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1852
+ if(TYPE(argv[0]) == T_FIXNUM){
1853
+ int num = FIX2LONG(argv[0]);
1854
+ if(num >= 0){
1855
+ mpfr_zeta_ui(ptr_return, (unsigned long)num, rnd);
1856
+ return val_ret;
1857
+ }
1858
+ }else{
1859
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1860
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1861
+ mpfr_zeta(ptr_return, ptr_arg1, rnd);
1862
+ }
1863
+ return val_ret;
1864
+ }
1865
+
1866
+ /* mpfr_erf(ret, p1, rnd). */
1867
+ static VALUE r_mpfr_math_erf(int argc, VALUE *argv, VALUE self){
1868
+ mp_rnd_t rnd;
1869
+ mp_prec_t prec;
1870
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1871
+ MPFR *ptr_arg1, *ptr_return;
1872
+ VALUE val_ret;
1873
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1874
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1875
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1876
+ r_mpfr_set_special_func_state(mpfr_erf(ptr_return, ptr_arg1, rnd));
1877
+ return val_ret;
1878
+ }
1879
+
1880
+ /* mpfr_erfc(ret, p1, rnd). */
1881
+ static VALUE r_mpfr_math_erfc(int argc, VALUE *argv, VALUE self){
1882
+ mp_rnd_t rnd;
1883
+ mp_prec_t prec;
1884
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1885
+ MPFR *ptr_arg1, *ptr_return;
1886
+ VALUE val_ret;
1887
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1888
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1889
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1890
+ r_mpfr_set_special_func_state(mpfr_erfc(ptr_return, ptr_arg1, rnd));
1891
+ return val_ret;
1892
+ }
1893
+
1894
+ /* mpfr_j0(ret, p1, rnd). */
1895
+ static VALUE r_mpfr_math_j0(int argc, VALUE *argv, VALUE self){
1896
+ mp_rnd_t rnd;
1897
+ mp_prec_t prec;
1898
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1899
+ MPFR *ptr_arg1, *ptr_return;
1900
+ VALUE val_ret;
1901
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1902
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1903
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1904
+ r_mpfr_set_special_func_state(mpfr_j0(ptr_return, ptr_arg1, rnd));
1905
+ return val_ret;
1906
+ }
1907
+
1908
+ /* mpfr_j1(ret, p1, rnd). */
1909
+ static VALUE r_mpfr_math_j1(int argc, VALUE *argv, VALUE self){
1910
+ mp_rnd_t rnd;
1911
+ mp_prec_t prec;
1912
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1913
+ MPFR *ptr_arg1, *ptr_return;
1914
+ VALUE val_ret;
1915
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1916
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1917
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1918
+ r_mpfr_set_special_func_state(mpfr_j1(ptr_return, ptr_arg1, rnd));
1919
+ return val_ret;
1920
+ }
1921
+
1922
+ static VALUE r_mpfr_math_jn(int argc, VALUE *argv, VALUE self){
1923
+ mp_rnd_t rnd;
1924
+ mp_prec_t prec;
1925
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
1926
+ MPFR *ptr_arg1, *ptr_return;
1927
+ VALUE val_ret;
1928
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1929
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1930
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1931
+ int n = NUM2INT(argv[1]);
1932
+ r_mpfr_set_special_func_state(mpfr_jn(ptr_return, n, ptr_arg1, rnd));
1933
+ return val_ret;
1934
+ }
1935
+
1936
+ /* mpfr_y0(ret, p1, rnd). */
1937
+ static VALUE r_mpfr_math_y0(int argc, VALUE *argv, VALUE self){
1938
+ mp_rnd_t rnd;
1939
+ mp_prec_t prec;
1940
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1941
+ MPFR *ptr_arg1, *ptr_return;
1942
+ VALUE val_ret;
1943
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1944
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1945
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1946
+ r_mpfr_set_special_func_state(mpfr_y0(ptr_return, ptr_arg1, rnd));
1947
+ return val_ret;
1948
+ }
1949
+
1950
+ /* mpfr_y1(ret, p1, rnd). */
1951
+ static VALUE r_mpfr_math_y1(int argc, VALUE *argv, VALUE self){
1952
+ mp_rnd_t rnd;
1953
+ mp_prec_t prec;
1954
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
1955
+ MPFR *ptr_arg1, *ptr_return;
1956
+ VALUE val_ret;
1957
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1958
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1959
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1960
+ r_mpfr_set_special_func_state(mpfr_y1(ptr_return, ptr_arg1, rnd));
1961
+ return val_ret;
1962
+ }
1963
+
1964
+ static VALUE r_mpfr_math_yn(int argc, VALUE *argv, VALUE self){
1965
+ mp_rnd_t rnd;
1966
+ mp_prec_t prec;
1967
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
1968
+ MPFR *ptr_arg1, *ptr_return;
1969
+ VALUE val_ret;
1970
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1971
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1972
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1973
+ int n = NUM2INT(argv[1]);
1974
+ r_mpfr_set_special_func_state(mpfr_yn(ptr_return, n, ptr_arg1, rnd));
1975
+ return val_ret;
1976
+ }
1977
+
1978
+ /* mpfr_fma(ret, p1, p2, p3, rnd). */
1979
+ static VALUE r_mpfr_math_fma(int argc, VALUE *argv, VALUE self){
1980
+ mp_rnd_t rnd;
1981
+ mp_prec_t prec;
1982
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 3, 5, argc, argv);
1983
+ MPFR *ptr_arg1, *ptr_arg2, *ptr_arg3, *ptr_return;
1984
+ VALUE val_ret;
1985
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
1986
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
1987
+ volatile VALUE tmp_argv1 = r_mpfr_new_fr_obj(argv[1]);
1988
+ r_mpfr_get_struct(ptr_arg2, tmp_argv1);
1989
+ volatile VALUE tmp_argv2 = r_mpfr_new_fr_obj(argv[2]);
1990
+ r_mpfr_get_struct(ptr_arg3, tmp_argv2);
1991
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
1992
+ r_mpfr_set_special_func_state(mpfr_fma(ptr_return, ptr_arg1, ptr_arg2, ptr_arg3, rnd));
1993
+ return val_ret;
1994
+ }
1995
+
1996
+ /* mpfr_fms(ret, p1, p2, p3, rnd). */
1997
+ static VALUE r_mpfr_math_fms(int argc, VALUE *argv, VALUE self){
1998
+ mp_rnd_t rnd;
1999
+ mp_prec_t prec;
2000
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 3, 5, argc, argv);
2001
+ MPFR *ptr_arg1, *ptr_arg2, *ptr_arg3, *ptr_return;
2002
+ VALUE val_ret;
2003
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
2004
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
2005
+ volatile VALUE tmp_argv1 = r_mpfr_new_fr_obj(argv[1]);
2006
+ r_mpfr_get_struct(ptr_arg2, tmp_argv1);
2007
+ volatile VALUE tmp_argv2 = r_mpfr_new_fr_obj(argv[2]);
2008
+ r_mpfr_get_struct(ptr_arg3, tmp_argv2);
2009
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
2010
+ r_mpfr_set_special_func_state(mpfr_fms(ptr_return, ptr_arg1, ptr_arg2, ptr_arg3, rnd));
2011
+ return val_ret;
2012
+ }
2013
+
2014
+ /* mpfr_agm(ret, p1, p2, rnd). */
2015
+ static VALUE r_mpfr_math_agm(int argc, VALUE *argv, VALUE self){
2016
+ mp_rnd_t rnd;
2017
+ mp_prec_t prec;
2018
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
2019
+ MPFR *ptr_arg1, *ptr_arg2, *ptr_return;
2020
+ VALUE val_ret;
2021
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
2022
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
2023
+ volatile VALUE tmp_argv1 = r_mpfr_new_fr_obj(argv[1]);
2024
+ r_mpfr_get_struct(ptr_arg2, tmp_argv1);
2025
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
2026
+ r_mpfr_set_special_func_state(mpfr_agm(ptr_return, ptr_arg1, ptr_arg2, rnd));
2027
+ return val_ret;
2028
+ }
2029
+
2030
+ /* mpfr_hypot(ret, p1, p2, rnd). */
2031
+ static VALUE r_mpfr_math_hypot(int argc, VALUE *argv, VALUE self){
2032
+ mp_rnd_t rnd;
2033
+ mp_prec_t prec;
2034
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
2035
+ MPFR *ptr_arg1, *ptr_arg2, *ptr_return;
2036
+ VALUE val_ret;
2037
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
2038
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
2039
+ volatile VALUE tmp_argv1 = r_mpfr_new_fr_obj(argv[1]);
2040
+ r_mpfr_get_struct(ptr_arg2, tmp_argv1);
2041
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
2042
+ r_mpfr_set_special_func_state(mpfr_hypot(ptr_return, ptr_arg1, ptr_arg2, rnd));
2043
+ return val_ret;
2044
+ }
2045
+
2046
+ /* mpfr_const_log2(ret, rnd). */
2047
+ static VALUE r_mpfr_math_const_log2(int argc, VALUE *argv, VALUE self){
2048
+ mp_rnd_t rnd;
2049
+ mp_prec_t prec;
2050
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
2051
+ MPFR *ptr_return;
2052
+ VALUE val_ret;
2053
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
2054
+ r_mpfr_set_special_func_state(mpfr_const_log2(ptr_return, rnd));
2055
+ return val_ret;
2056
+ }
2057
+
2058
+ /* mpfr_const_pi(ret, rnd). */
2059
+ static VALUE r_mpfr_math_const_pi(int argc, VALUE *argv, VALUE self){
2060
+ mp_rnd_t rnd;
2061
+ mp_prec_t prec;
2062
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
2063
+ MPFR *ptr_return;
2064
+ VALUE val_ret;
2065
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
2066
+ r_mpfr_set_special_func_state(mpfr_const_pi(ptr_return, rnd));
2067
+ return val_ret;
2068
+ }
2069
+
2070
+ /* mpfr_const_euler(ret, rnd). */
2071
+ static VALUE r_mpfr_math_const_euler(int argc, VALUE *argv, VALUE self){
2072
+ mp_rnd_t rnd;
2073
+ mp_prec_t prec;
2074
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
2075
+ MPFR *ptr_return;
2076
+ VALUE val_ret;
2077
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
2078
+ r_mpfr_set_special_func_state(mpfr_const_euler(ptr_return, rnd));
2079
+ return val_ret;
2080
+ }
2081
+
2082
+ /* mpfr_const_catalan(ret, rnd). */
2083
+ static VALUE r_mpfr_math_const_catalan(int argc, VALUE *argv, VALUE self){
2084
+ mp_rnd_t rnd;
2085
+ mp_prec_t prec;
2086
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
2087
+ MPFR *ptr_return;
2088
+ VALUE val_ret;
2089
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
2090
+ r_mpfr_set_special_func_state(mpfr_const_catalan(ptr_return, rnd));
2091
+ return val_ret;
2092
+ }
2093
+
2094
+ /* Execute mpfr_free_cache(). */
2095
+ static VALUE r_mpfr_math_free_cache(VALUE self){
2096
+ mpfr_free_cache();
2097
+ return Qnil;
2098
+ }
2099
+
2100
+ /* static VALUE r_mpfr_math_sum(int argc, VALUE *argv, VALUE self){ */
2101
+ /* mp_rnd_t rnd; */
2102
+ /* mp_prec_t prec; */
2103
+ /* r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv); */
2104
+ /* MPFR *ptr_return, *ptr_args[argc]; */
2105
+ /* VALUE val_ret; */
2106
+ /* int i; */
2107
+ /* for(i = 0; i < argc; i++){ */
2108
+ /* volatile VALUE tmp_argvi = r_mpfr_new_fr_obj(argv[i]); */
2109
+ /* r_mpfr_get_struct(ptr_args[i], tmp_argvi); */
2110
+ /* } */
2111
+ /* r_mpfr_make_struct_init2(val_ret, ptr_return, prec); */
2112
+ /* r_mpfr_set_special_func_state(mpfr_sum(ptr_return, ptr_args, argc, rnd)); */
2113
+ /* return val_ret; */
2114
+ /* } */
2115
+
2116
+ /* ------------------------------ Special Functions End ------------------------------ */
2117
+
2118
+ /* ------------------------------ MPFR::Math Miscellaneous Functions Start ------------------------------ */
2119
+
2120
+ /* mpfr_min(ret, p1, p2, rnd). */
2121
+ static VALUE r_mpfr_math_min(int argc, VALUE *argv, VALUE self){
2122
+ mp_rnd_t rnd;
2123
+ mp_prec_t prec;
2124
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
2125
+ MPFR *ptr_arg1, *ptr_arg2, *ptr_return;
2126
+ VALUE val_ret;
2127
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
2128
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
2129
+ volatile VALUE tmp_argv1 = r_mpfr_new_fr_obj(argv[1]);
2130
+ r_mpfr_get_struct(ptr_arg2, tmp_argv1);
2131
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
2132
+ mpfr_min(ptr_return, ptr_arg1, ptr_arg2, rnd);
2133
+ return val_ret;
2134
+ }
2135
+
2136
+ /* mpfr_max(ret, p1, p2, rnd). */
2137
+ static VALUE r_mpfr_math_max(int argc, VALUE *argv, VALUE self){
2138
+ mp_rnd_t rnd;
2139
+ mp_prec_t prec;
2140
+ r_mpfr_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
2141
+ MPFR *ptr_arg1, *ptr_arg2, *ptr_return;
2142
+ VALUE val_ret;
2143
+ volatile VALUE tmp_argv0 = r_mpfr_new_fr_obj(argv[0]);
2144
+ r_mpfr_get_struct(ptr_arg1, tmp_argv0);
2145
+ volatile VALUE tmp_argv1 = r_mpfr_new_fr_obj(argv[1]);
2146
+ r_mpfr_get_struct(ptr_arg2, tmp_argv1);
2147
+ r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
2148
+ mpfr_max(ptr_return, ptr_arg1, ptr_arg2, rnd);
2149
+ return val_ret;
2150
+ }
2151
+
2152
+ /* ------------------------------ MPFR::Math Miscellaneous Functions End ------------------------------ */
2153
+
2154
+ void Init_mpfr(){
2155
+ /* ------------------------------ Class MPFR Start ------------------------------ */
2156
+
2157
+ /*
2158
+ = Ruby MPFR Library
2159
+ This extended library is ruby interface to {the MPFR library (http://www.mpfr.org/)}[http://www.mpfr.org/].
2160
+ Please see MPFR documents (for example {http://www.mpfr.org/mpfr-current/mpfr.html}[http://www.mpfr.org/mpfr-current/mpfr.html])
2161
+ along with this documents.
2162
+
2163
+ == Class and module
2164
+ In this library, class MPFR is that of multiple precision number and
2165
+ module MPFR::Math collects singleton methods which mainly have MPFR arguments.
2166
+
2167
+ Generally, the functions in the following items of MPFR reference manual is implemented
2168
+ as instance methods of class MPFR.
2169
+ - Conversion Functions
2170
+ - Basic Arithmetic Functions
2171
+ - Comparison Functions
2172
+
2173
+ And, the class method of module MPFR::Math mainly consints of the functions
2174
+ in "Special functions" items of MPFR reference manual.
2175
+
2176
+ == Name of methods and constants
2177
+ Except for some methods,
2178
+ the names of methods inherit that of MPFR functions in C language and
2179
+ each name of method is removed 'mpfr_' from that of correspnding MPFR function in C language.
2180
+ The methods m_rint, m_ceil, m_floor, m_round and m_trunc are exceptions and
2181
+ respectively corresponts to mpfr_rint, mpfr_ceil, mpfr_floor, mpfr_round and mpfr_trunc.
2182
+ Note that there are also methods rint, ceil, floor, round and trunc
2183
+ which have respectively defferent actions
2184
+ from mpfr_rint, mpfr_ceil, mpfr_floor, mpfr_round and mpfr_trunc.
2185
+
2186
+ Some macros in MPFR library become Constants in Ruby.
2187
+ These name is that got rid of 'MPFR_'.
2188
+
2189
+ == Optional arguments of methods (rounding mode and precision)
2190
+ If the most methods could have two optional arguments _rnd_ and _prec_,
2191
+ _prec_ is precision and _rnd_ is rounding mode.
2192
+ If you omit these arguments, default value is used.
2193
+ See MPFR.set_default_prec, MPFR.get_default_prec, MPFR.set_default_rounding_mode and MPFR.get_default_rounding_mode.
2194
+
2195
+ If the methods can have argument of rounding mode,
2196
+ we can hand the constants MPFR::RNDN, MPFR::RNDZ, MPFR::RNDU and MPFR::RNDD to that.
2197
+
2198
+ == Returned value of methods
2199
+ If there is no remark for returned value of method,
2200
+ the method returns ruby object corresponding to
2201
+ value of MPFR function in C language.
2202
+
2203
+ == Conversion for arguments of method
2204
+
2205
+ When method have argument of MPFR and we set instance of other class to the argument,
2206
+ the method tries to convert the argument to MPFR instance.
2207
+ */
2208
+ r_mpfr_class = rb_define_class("MPFR", rb_cNumeric);
2209
+ rb_include_module(r_mpfr_class, rb_mComparable);
2210
+
2211
+ /* ------------------------------ Class MPFR End ------------------------------ */
2212
+
2213
+ /* ------------------------------ Constants Start ------------------------------ */
2214
+
2215
+ /* Version string which mpfr_get_version() returns. */
2216
+ rb_define_const(r_mpfr_class, "MPFR_VERSION", rb_str_new2(mpfr_get_version()));
2217
+ /* String which mpfr_get_patches() returns. */
2218
+ rb_define_const(r_mpfr_class, "MPFR_PATCHES", rb_str_new2(mpfr_get_patches()));
2219
+ /* Integer which is macro MPFR_VERSION. */
2220
+ rb_define_const(r_mpfr_class, "MPFR_VERSION2", INT2NUM(MPFR_VERSION));
2221
+ /* Integer which is macro MPFR_VERSION_MAJOR. */
2222
+ rb_define_const(r_mpfr_class, "MPFR_VERSION_MAJOR", INT2NUM(MPFR_VERSION_MAJOR));
2223
+ /* Integer which is macro MPFR_VERSION_MINOR. */
2224
+ rb_define_const(r_mpfr_class, "MPFR_VERSION_MINOR", INT2NUM(MPFR_VERSION_MINOR));
2225
+ /* Integer which is macro MPFR_VERSION_PATCHLEVEL. */
2226
+ rb_define_const(r_mpfr_class, "MPFR_VERSION_PATCHLEVEL", INT2NUM(MPFR_VERSION_PATCHLEVEL));
2227
+ /* String whichi is macro MPFR_VERSION_STRING. */
2228
+ rb_define_const(r_mpfr_class, "MPFR_VERSION_STRING", rb_str_new2(MPFR_VERSION_STRING));
2229
+
2230
+ /* Integer which is macro MPFR_PREC_MAX. */
2231
+ rb_define_const(r_mpfr_class, "PREC_MAX", INT2NUM(MPFR_PREC_MAX));
2232
+ /* Integer which is macro MPFR_PREC_MIN. */
2233
+ rb_define_const(r_mpfr_class, "PREC_MIN", INT2NUM(MPFR_PREC_MIN));
2234
+ /* Integer which is macro MPFR_EMAX_DEFAULT. */
2235
+ rb_define_const(r_mpfr_class, "EMAX_DEFAULT", INT2NUM(MPFR_EMAX_DEFAULT));
2236
+ /* Integer whichi is MPFR_EMIN_DEFAULT. */
2237
+ rb_define_const(r_mpfr_class, "EMIN_DEFAULT", INT2NUM(MPFR_EMIN_DEFAULT));
2238
+
2239
+ /* Integer which is macro GMP_RNDN. */
2240
+ rb_define_const(r_mpfr_class, "RNDN", INT2NUM(GMP_RNDN));
2241
+ /* Integer which is macro GMP_RNDZ. */
2242
+ rb_define_const(r_mpfr_class, "RNDZ", INT2NUM(GMP_RNDZ));
2243
+ /* Integer which is macro GMP_RNDU. */
2244
+ rb_define_const(r_mpfr_class, "RNDU", INT2NUM(GMP_RNDU));
2245
+ /* Integer which is macro GMP_RNDD. */
2246
+ rb_define_const(r_mpfr_class, "RNDD", INT2NUM(GMP_RNDD));
2247
+
2248
+ /* ------------------------------ Constants End ------------------------------ */
2249
+
2250
+ /* ------------------------------ Precision and Rounding Mode Start ------------------------------ */
2251
+
2252
+ rb_define_singleton_method(r_mpfr_class, "set_default_prec", r_mpfr_set_default_prec, 1);
2253
+ rb_define_singleton_method(r_mpfr_class, "get_default_prec", r_mpfr_get_default_prec, 0);
2254
+ rb_define_singleton_method(r_mpfr_class, "set_default_rounding_mode", r_mpfr_set_default_rounding_mode, 1);
2255
+ rb_define_singleton_method(r_mpfr_class, "get_default_rounding_mode", r_mpfr_get_default_rounding_mode, 0);
2256
+
2257
+ /* ------------------------------ Precision and Rounding Mode End ------------------------------ */
2258
+
2259
+ /* ------------------------------ Exception Related Functions Start ------------------------------ */
2260
+
2261
+ rb_define_singleton_method(r_mpfr_class, "get_emin", r_mpfr_get_emin, 0);
2262
+ rb_define_singleton_method(r_mpfr_class, "get_emax", r_mpfr_get_emax, 0);
2263
+ rb_define_singleton_method(r_mpfr_class, "set_emin", r_mpfr_set_emin, 1);
2264
+ rb_define_singleton_method(r_mpfr_class, "set_emax", r_mpfr_set_emax, 1);
2265
+ rb_define_singleton_method(r_mpfr_class, "get_emin_min", r_mpfr_get_emin_min, 0);
2266
+ rb_define_singleton_method(r_mpfr_class, "get_emin_max", r_mpfr_get_emin_max, 0);
2267
+ rb_define_singleton_method(r_mpfr_class, "get_emax_min", r_mpfr_get_emax_min, 0);
2268
+ rb_define_singleton_method(r_mpfr_class, "get_emax_max", r_mpfr_get_emax_max, 0);
2269
+ rb_define_singleton_method(r_mpfr_class, "clear_underflow", r_mpfr_clear_underflow, 0);
2270
+ rb_define_singleton_method(r_mpfr_class, "clear_overflow", r_mpfr_clear_overflow, 0);
2271
+ rb_define_singleton_method(r_mpfr_class, "clear_nanflag", r_mpfr_clear_nanflag, 0);
2272
+ rb_define_singleton_method(r_mpfr_class, "clear_inexflag", r_mpfr_clear_inexflag, 0);
2273
+ rb_define_singleton_method(r_mpfr_class, "clear_erangeflag", r_mpfr_clear_erangeflag, 0);
2274
+ rb_define_singleton_method(r_mpfr_class, "set_underflow", r_mpfr_set_underflow, 0);
2275
+ rb_define_singleton_method(r_mpfr_class, "set_overflow", r_mpfr_set_overflow, 0);
2276
+ rb_define_singleton_method(r_mpfr_class, "set_nanflag", r_mpfr_set_nanflag, 0);
2277
+ rb_define_singleton_method(r_mpfr_class, "set_inexflag", r_mpfr_set_inexflag, 0);
2278
+ rb_define_singleton_method(r_mpfr_class, "set_erangeflag", r_mpfr_set_erangeflag, 0);
2279
+ rb_define_singleton_method(r_mpfr_class, "clear_flags", r_mpfr_clear_flags, 0);
2280
+ rb_define_singleton_method(r_mpfr_class, "underflow_p", r_mpfr_underflow_p, 0);
2281
+ rb_define_singleton_method(r_mpfr_class, "overflow_p", r_mpfr_overflow_p, 0);
2282
+ rb_define_singleton_method(r_mpfr_class, "nanflag_p", r_mpfr_nanflag_p, 0);
2283
+ rb_define_singleton_method(r_mpfr_class, "inexflag_p", r_mpfr_inexflag_p, 0);
2284
+ rb_define_singleton_method(r_mpfr_class, "erangeflag_p", r_mpfr_erangeflag_p, 0);
2285
+
2286
+ rb_define_method(r_mpfr_class, "check_range", r_mpfr_check_range, -1);
2287
+ rb_define_method(r_mpfr_class, "subnormalize", r_mpfr_subnormalize, -1);
2288
+
2289
+ /* ------------------------------ Exception Related Functions End ------------------------------ */
2290
+
2291
+ /* ------------------------------ MPFR allocation Start ------------------------------ */
2292
+
2293
+ rb_define_alloc_func(r_mpfr_class, r_mpfr_alloc);
2294
+ rb_define_private_method(r_mpfr_class, "initialize", r_mpfr_initialize, -1);
2295
+ rb_define_private_method(r_mpfr_class, "initialize_copy", r_mpfr_initialize_copy, 1);
2296
+ rb_define_method(r_mpfr_class, "coerce", r_mpfr_coerce, 1);
2297
+
2298
+ rb_define_singleton_method(r_mpfr_class, "nan", r_mpfr_nan, -1);
2299
+ rb_define_singleton_method(r_mpfr_class, "pinf", r_mpfr_pinf, -1);
2300
+ rb_define_singleton_method(r_mpfr_class, "minf", r_mpfr_minf, -1);
2301
+
2302
+ /* ------------------------------ MPFR allocation End ------------------------------ */
2303
+
2304
+ /* ------------------------------ Assignment Functions Start ------------------------------ */
2305
+
2306
+ rb_define_method(r_mpfr_class, "set_prec", r_mpfr_set_prec, 1);
2307
+ rb_define_method(r_mpfr_class, "get_prec", r_mpfr_get_prec, 0);
2308
+ rb_define_method(r_mpfr_class, "set", r_mpfr_set, -1);
2309
+ rb_define_method(r_mpfr_class, "set_fixnum_2exp", r_mpfr_set_fixnum_2exp, -1);
2310
+ rb_define_method(r_mpfr_class, "set_inf", r_mpfr_set_inf, 1);
2311
+ rb_define_method(r_mpfr_class, "set_nan", r_mpfr_set_nan, 0);
2312
+ rb_define_method(r_mpfr_class, "swap", r_mpfr_swap, 1);
2313
+
2314
+ /* ------------------------------ Assignment Functions End ------------------------------ */
2315
+
2316
+ /* ------------------------------ Methods related to string Start ------------------------------ */
2317
+
2318
+ rb_define_method(r_mpfr_class, "to_strf", r_mpfr_to_strf, 1);
2319
+ rb_define_method(r_mpfr_class, "to_s", r_mpfr_to_s, 0);
2320
+ rb_define_method(r_mpfr_class, "inspect", r_mpfr_inspect, 0);
2321
+ /* ------------------------------ Methods related to string End ------------------------------ */
2322
+
2323
+ /* ------------------------------ Conversion functions Start ------------------------------ */
2324
+
2325
+ rb_define_method(r_mpfr_class, "get_d", r_mpfr_get_d, -1);
2326
+ rb_define_method(r_mpfr_class, "get_d_2exp", r_mpfr_get_d_2exp, -1);
2327
+ rb_define_method(r_mpfr_class, "get_si", r_mpfr_get_si, -1);
2328
+ rb_define_method(r_mpfr_class, "get_str", r_mpfr_get_str, 0);
2329
+
2330
+ rb_define_method(r_mpfr_class, "round", r_mpfr_round_to_i, 0);
2331
+ rb_define_method(r_mpfr_class, "floor", r_mpfr_floor_to_i, 0);
2332
+ rb_define_method(r_mpfr_class, "ceil", r_mpfr_ceil_to_i, 0);
2333
+ rb_define_method(r_mpfr_class, "truncate", r_mpfr_truncate_to_i, 0);
2334
+
2335
+ rb_define_alias(r_mpfr_class, "to_f", "get_d");
2336
+ rb_define_alias(r_mpfr_class, "to_i", "truncate");
2337
+ rb_define_alias(r_mpfr_class, "to_int", "truncate");
2338
+
2339
+ /* ------------------------------ Conversion functions End ------------------------------ */
2340
+
2341
+ /* ------------------------------ Basic Arithmetic Functions Start ------------------------------ */
2342
+
2343
+ rb_define_method(r_mpfr_class, "+", r_mpfr_add, 1);
2344
+ rb_define_method(r_mpfr_class, "-", r_mpfr_sub, 1);
2345
+ rb_define_method(r_mpfr_class, "*", r_mpfr_mul, 1);
2346
+ rb_define_method(r_mpfr_class, "/", r_mpfr_div, 1);
2347
+ rb_define_method(r_mpfr_class, "**", r_mpfr_pow, 1);
2348
+ rb_define_method(r_mpfr_class, "neg", r_mpfr_neg, -1);
2349
+ rb_define_method(r_mpfr_class, "abs", r_mpfr_abs, -1);
2350
+
2351
+ /* ------------------------------ Basic Arithmetic Functions End ------------------------------ */
2352
+
2353
+ /* ------------------------------ Comparison Functions Start ------------------------------ */
2354
+
2355
+ rb_define_method(r_mpfr_class, "cmp", r_mpfr_cmp, 1);
2356
+ rb_define_method(r_mpfr_class, "cmp_ui_2exp", r_mpfr_cmp_ui_2exp, 2);
2357
+ rb_define_method(r_mpfr_class, "cmp_si_2exp", r_mpfr_cmp_si_2exp, 2);
2358
+ rb_define_method(r_mpfr_class, "cmpabs", r_mpfr_cmpabs, 1);
2359
+ rb_define_method(r_mpfr_class, "nan_p", r_mpfr_nan_p, 0);
2360
+ rb_define_method(r_mpfr_class, "inf_p", r_mpfr_inf_p, 0);
2361
+ rb_define_method(r_mpfr_class, "number_p", r_mpfr_number_p, 0);
2362
+ rb_define_method(r_mpfr_class, "zero_p", r_mpfr_zero_p, 0);
2363
+ rb_define_method(r_mpfr_class, "sgn", r_mpfr_sgn, 0);
2364
+ rb_define_method(r_mpfr_class, "greater_p", r_mpfr_greater_p, 1);
2365
+ rb_define_method(r_mpfr_class, "greaterequal_p", r_mpfr_greaterequal_p, 1);
2366
+ rb_define_method(r_mpfr_class, "less_p", r_mpfr_less_p, 1);
2367
+ rb_define_method(r_mpfr_class, "lessequal_p", r_mpfr_lessequal_p, 1);
2368
+ rb_define_method(r_mpfr_class, "lessgreater_p", r_mpfr_lessgreater_p, 1);
2369
+ rb_define_method(r_mpfr_class, "equal_p", r_mpfr_equal_p, 1);
2370
+ rb_define_method(r_mpfr_class, "unordered_p", r_mpfr_unordered_p, 1);
2371
+ rb_define_method(r_mpfr_class, "infinite?", r_mpfr_inf_p2, 0);
2372
+
2373
+ rb_define_alias(r_mpfr_class, "<=>", "cmp");
2374
+ rb_define_alias(r_mpfr_class, "nan?", "nan_p");
2375
+ rb_define_alias(r_mpfr_class, "finite?", "number_p");
2376
+ rb_define_alias(r_mpfr_class, "zero?", "zero_p");
2377
+ rb_define_method(r_mpfr_class, "nonzero?", r_mpfr_nonzero_p, 1);
2378
+
2379
+ /* ------------------------------ Comparison Functions Start ------------------------------ */
2380
+
2381
+ /* ------------------------------ Integer Related Functions Start ------------------------------ */
2382
+
2383
+ rb_define_method(r_mpfr_class, "m_rint", r_mpfr_m_rint, -1);
2384
+ rb_define_method(r_mpfr_class, "m_ceil", r_mpfr_m_ceil, -1);
2385
+ rb_define_method(r_mpfr_class, "m_floor", r_mpfr_m_floor, -1);
2386
+ rb_define_method(r_mpfr_class, "m_round", r_mpfr_m_round, -1);
2387
+ rb_define_method(r_mpfr_class, "m_trunc", r_mpfr_m_trunc, -1);
2388
+ rb_define_method(r_mpfr_class, "rint_ceil", r_mpfr_rint_ceil, -1);
2389
+ rb_define_method(r_mpfr_class, "rint_floor", r_mpfr_rint_floor, -1);
2390
+ rb_define_method(r_mpfr_class, "rint_round", r_mpfr_rint_round, -1);
2391
+ rb_define_method(r_mpfr_class, "rint_trunc", r_mpfr_rint_trunc, -1);
2392
+ rb_define_method(r_mpfr_class, "frac", r_mpfr_frac, -1);
2393
+ rb_define_method(r_mpfr_class, "modf", r_mpfr_modf, -1);
2394
+ rb_define_method(r_mpfr_class, "fmod", r_mpfr_fmod, -1);
2395
+ rb_define_method(r_mpfr_class, "remainder", r_mpfr_remainder, -1);
2396
+ rb_define_method(r_mpfr_class, "%", r_mpfr_remainder2, -1);
2397
+ rb_define_method(r_mpfr_class, "remquo", r_mpfr_remquo, -1);
2398
+ rb_define_method(r_mpfr_class, "integer_p", r_mpfr_integer_p, 0);
2399
+
2400
+ rb_define_alias(r_mpfr_class, "integer?", "integer_p");
2401
+ rb_define_alias(r_mpfr_class, "modulo", "%");
2402
+
2403
+ /* ------------------------------ Integer Related Functions End ------------------------------ */
2404
+
2405
+ /* ------------------------------ Miscellaneous Functions Start ------------------------------ */
2406
+
2407
+ rb_define_method(r_mpfr_class, "nexttoward", r_mpfr_nexttoward, 1);
2408
+ rb_define_method(r_mpfr_class, "nextabove", r_mpfr_nextabove, 0);
2409
+ rb_define_method(r_mpfr_class, "nextbelow", r_mpfr_nextbelow, 0);
2410
+ rb_define_method(r_mpfr_class, "get_exp", r_mpfr_get_exp, 0);
2411
+ rb_define_method(r_mpfr_class, "set_exp", r_mpfr_set_exp, 1);
2412
+ rb_define_method(r_mpfr_class, "signbit", r_mpfr_signbit, 0);
2413
+ rb_define_method(r_mpfr_class, "setsign", r_mpfr_setsign, -1);
2414
+ rb_define_method(r_mpfr_class, "copysign", r_mpfr_copysign, -1);
2415
+
2416
+ /* ------------------------------ Miscellaneous Functions End ------------------------------ */
2417
+
2418
+ /* ------------------------------ Rounding Mode Related Functions Start ------------------------------ */
2419
+
2420
+ rb_define_method(r_mpfr_class, "prec_round", r_mpfr_prec_round, -1);
2421
+ rb_define_method(r_mpfr_class, "prec_round!", r_mpfr_prec_round2, -1);
2422
+
2423
+ /* ------------------------------ Rounding Mode Related Functions End ------------------------------ */
2424
+
2425
+
2426
+ /* ------------------------------ Module MPFR::Math Start ------------------------------ */
2427
+
2428
+ /*
2429
+ Almost all class methods in this module is the functions in "Special Functions" item of MPFR reference manual.
2430
+ First arguments of these methods takes MPFR instance.
2431
+ If it is not MPFR instance, it is converted to MPFR instance in this method.
2432
+ And all methods have two optional arguments rnd and prec.
2433
+ That is, You may call MPFR::Math.some_method(some_required_args, rnd, prec) and
2434
+ last two arguments rnd and prec could be omitted.
2435
+ The methods collected in this module works as the following imitational C code.
2436
+ mpfr_t returned_val;
2437
+ mpfr_init2(returned_val, prec);
2438
+ mpfr_some_func(returned_val, p1, ..., pn, rnd);
2439
+ return returned_val;
2440
+ The returned value of mpfr_some_func is saved and if you want to get this value
2441
+ then you may use MPFR.get_special_func_state.
2442
+ */
2443
+ r_mpfr_math = rb_define_module_under(r_mpfr_class, "Math");
2444
+
2445
+ rb_define_class_variable(r_mpfr_class, SPECIAL_FUNC_STATE, INT2FIX(0));
2446
+ rb_define_singleton_method(r_mpfr_math, "get_special_func_state", r_mpfr_get_special_func_state, 0);
2447
+
2448
+ /* ------------------------------ Math Basic Arithmetic Functions Start ------------------------------ */
2449
+
2450
+ rb_define_module_function(r_mpfr_math, "add", r_mpfr_math_add, -1);
2451
+ rb_define_module_function(r_mpfr_math, "sub", r_mpfr_math_sub, -1);
2452
+ rb_define_module_function(r_mpfr_math, "mul", r_mpfr_math_mul, -1);
2453
+ rb_define_module_function(r_mpfr_math, "div", r_mpfr_math_div, -1);
2454
+ rb_define_module_function(r_mpfr_math, "sqr", r_mpfr_math_sqr, -1);
2455
+ rb_define_module_function(r_mpfr_math, "sqrt", r_mpfr_math_sqrt, -1);
2456
+ rb_define_module_function(r_mpfr_math, "rec_sqrt", r_mpfr_math_rec_sqrt, -1);
2457
+ rb_define_module_function(r_mpfr_math, "cbrt", r_mpfr_math_cbrt, -1);
2458
+ rb_define_module_function(r_mpfr_math, "root", r_mpfr_math_root, -1);
2459
+ rb_define_module_function(r_mpfr_math, "pow", r_mpfr_math_pow, -1);
2460
+ rb_define_module_function(r_mpfr_math, "dim", r_mpfr_math_dim, -1);
2461
+ rb_define_module_function(r_mpfr_math, "mul_2si", r_mpfr_math_mul_2si, -1);
2462
+ rb_define_module_function(r_mpfr_math, "div_2si", r_mpfr_math_div_2si, -1);
2463
+
2464
+ /* ------------------------------ Math Basic Arithmetic Functions End ------------------------------ */
2465
+
2466
+ /* ------------------------------ Special Functions Start ------------------------------ */
2467
+
2468
+ rb_define_module_function(r_mpfr_math, "log", r_mpfr_math_log, -1);
2469
+ rb_define_module_function(r_mpfr_math, "log2", r_mpfr_math_log2, -1);
2470
+ rb_define_module_function(r_mpfr_math, "log10", r_mpfr_math_log10, -1);
2471
+ rb_define_module_function(r_mpfr_math, "exp", r_mpfr_math_exp, -1);
2472
+ rb_define_module_function(r_mpfr_math, "exp2", r_mpfr_math_exp2, -1);
2473
+ rb_define_module_function(r_mpfr_math, "exp10", r_mpfr_math_exp10, -1);
2474
+ rb_define_module_function(r_mpfr_math, "cos", r_mpfr_math_cos, -1);
2475
+ rb_define_module_function(r_mpfr_math, "sin", r_mpfr_math_sin, -1);
2476
+ rb_define_module_function(r_mpfr_math, "tan", r_mpfr_math_tan, -1);
2477
+ rb_define_module_function(r_mpfr_math, "sec", r_mpfr_math_sec, -1);
2478
+ rb_define_module_function(r_mpfr_math, "csc", r_mpfr_math_csc, -1);
2479
+ rb_define_module_function(r_mpfr_math, "cot", r_mpfr_math_cot, -1);
2480
+ rb_define_module_function(r_mpfr_math, "sin_cos", r_mpfr_math_sin_cos, -1);
2481
+ rb_define_module_function(r_mpfr_math, "acos", r_mpfr_math_acos, -1);
2482
+ rb_define_module_function(r_mpfr_math, "asin", r_mpfr_math_asin, -1);
2483
+ rb_define_module_function(r_mpfr_math, "atan", r_mpfr_math_atan, -1);
2484
+ rb_define_module_function(r_mpfr_math, "atan2", r_mpfr_math_atan2, -1);
2485
+ rb_define_module_function(r_mpfr_math, "cosh", r_mpfr_math_cosh, -1);
2486
+ rb_define_module_function(r_mpfr_math, "sinh", r_mpfr_math_sinh, -1);
2487
+ rb_define_module_function(r_mpfr_math, "tanh", r_mpfr_math_tanh, -1);
2488
+ rb_define_module_function(r_mpfr_math, "sinh_cosh", r_mpfr_math_sinh_cosh, -1);
2489
+ rb_define_module_function(r_mpfr_math, "acosh", r_mpfr_math_acosh, -1);
2490
+ rb_define_module_function(r_mpfr_math, "asinh", r_mpfr_math_asinh, -1);
2491
+ rb_define_module_function(r_mpfr_math, "atanh", r_mpfr_math_atanh, -1);
2492
+ rb_define_module_function(r_mpfr_math, "fac_ui", r_mpfr_math_fac_ui, -1);
2493
+ rb_define_module_function(r_mpfr_math, "log1p", r_mpfr_math_log1p, -1);
2494
+ rb_define_module_function(r_mpfr_math, "expm1", r_mpfr_math_expm1, -1);
2495
+ rb_define_module_function(r_mpfr_math, "eint", r_mpfr_math_eint, -1);
2496
+ rb_define_module_function(r_mpfr_math, "li2", r_mpfr_math_li2, -1);
2497
+ rb_define_module_function(r_mpfr_math, "gamma", r_mpfr_math_gamma, -1);
2498
+ rb_define_module_function(r_mpfr_math, "lngamma", r_mpfr_math_lngamma, -1);
2499
+ rb_define_module_function(r_mpfr_math, "lgamma", r_mpfr_math_lgamma, -1);
2500
+ rb_define_module_function(r_mpfr_math, "zeta", r_mpfr_math_zeta, -1);
2501
+ rb_define_module_function(r_mpfr_math, "erf", r_mpfr_math_erf, -1);
2502
+ rb_define_module_function(r_mpfr_math, "erfc", r_mpfr_math_erfc, -1);
2503
+ rb_define_module_function(r_mpfr_math, "j0", r_mpfr_math_j0, -1);
2504
+ rb_define_module_function(r_mpfr_math, "j1", r_mpfr_math_j1, -1);
2505
+ rb_define_module_function(r_mpfr_math, "jn", r_mpfr_math_jn, -1);
2506
+ rb_define_module_function(r_mpfr_math, "y0", r_mpfr_math_y0, -1);
2507
+ rb_define_module_function(r_mpfr_math, "y1", r_mpfr_math_y1, -1);
2508
+ rb_define_module_function(r_mpfr_math, "yn", r_mpfr_math_yn, -1);
2509
+ rb_define_module_function(r_mpfr_math, "fma", r_mpfr_math_fma, -1);
2510
+ rb_define_module_function(r_mpfr_math, "fms", r_mpfr_math_fms, -1);
2511
+ rb_define_module_function(r_mpfr_math, "agm", r_mpfr_math_agm, -1);
2512
+ rb_define_module_function(r_mpfr_math, "hypot", r_mpfr_math_hypot, -1);
2513
+ rb_define_module_function(r_mpfr_math, "const_log2", r_mpfr_math_const_log2, -1);
2514
+ rb_define_module_function(r_mpfr_math, "const_pi", r_mpfr_math_const_pi, -1);
2515
+ rb_define_module_function(r_mpfr_math, "const_euler", r_mpfr_math_const_euler, -1);
2516
+ rb_define_module_function(r_mpfr_math, "const_catalan", r_mpfr_math_const_catalan, -1);
2517
+ rb_define_module_function(r_mpfr_math, "free_cache", r_mpfr_math_free_cache, 0);
2518
+ /* rb_define_module_function(r_mpfr_math, "sum", r_mpfr_math_sum, -1); */
2519
+
2520
+ /* ------------------------------ Special Functions End ------------------------------ */
2521
+
2522
+ /* ------------------------------ MPFR::Math Miscellaneous Functions Start ------------------------------ */
2523
+
2524
+ rb_define_module_function(r_mpfr_math, "min", r_mpfr_math_min, -1);
2525
+ rb_define_module_function(r_mpfr_math, "max", r_mpfr_math_max, -1);
2526
+
2527
+ /* ------------------------------ MPFR::Math Miscellaneous Functions End ------------------------------ */
2528
+
2529
+ /* ------------------------------ Module MPFR::Math End ------------------------------ */
2530
+
2531
+ eqq = rb_intern("===");
2532
+ to_s = rb_intern("to_s");
2533
+ new = rb_intern("new");
2534
+ class = rb_intern("class");
2535
+ method_defined = rb_intern("method_defined?");
2536
+ object_id = rb_intern("object_id");
2537
+
2538
+ __mpfr_class__ = rb_eval_string("MPFR");
2539
+ __sym_to_s__ = rb_eval_string(":to_s");
2540
+ __sym_to_str__ = rb_eval_string(":to_str");
2541
+
2542
+ }