gmp 0.4.1-x86-mingw32 → 0.4.3-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/ext/gmp.c CHANGED
@@ -121,6 +121,51 @@ static VALUE r_gmpfsg_set_default_prec(VALUE klass, VALUE arg)
121
121
  return Qnil;
122
122
  }
123
123
 
124
+ #ifdef MPFR
125
+ static VALUE r_gmpfsg_get_default_rounding_mode(VALUE klass)
126
+ {
127
+ (void)klass;
128
+ const char *rounding_string_val;
129
+ VALUE rounding_string;
130
+ ID method_to_sym = rb_intern("to_sym");
131
+ rounding_string_val = mpfr_print_rnd_mode (mpfr_get_default_rounding_mode ());
132
+ rounding_string = rb_str_new2 (rounding_string_val);
133
+ /*return rb_const_get (mGMP, rb_intern (rounding_string));*/
134
+ return rb_funcall (rounding_string, method_to_sym, 0);
135
+ }
136
+
137
+ static VALUE r_gmpfsg_set_default_rounding_mode(VALUE klass, VALUE arg)
138
+ {
139
+ (void)klass;
140
+ int arg_val;
141
+ if (FIXNUM_P(arg)) {
142
+ arg_val = FIX2INT(arg);
143
+ if (arg_val < 0 || arg_val > 3) {
144
+ rb_raise(rb_eRangeError, "rounding mode must be one of the rounding mode constants.");
145
+ }
146
+ } else {
147
+ rb_raise(rb_eTypeError, "rounding mode must be one of the rounding mode constants.");
148
+ }
149
+
150
+ switch (arg_val) {
151
+ case 0:
152
+ mpfr_set_default_rounding_mode (GMP_RNDN);
153
+ break;
154
+ case 1:
155
+ mpfr_set_default_rounding_mode (GMP_RNDZ);
156
+ break;
157
+ case 2:
158
+ mpfr_set_default_rounding_mode (GMP_RNDU);
159
+ break;
160
+ case 3:
161
+ mpfr_set_default_rounding_mode (GMP_RNDD);
162
+ break;
163
+ }
164
+
165
+ return Qnil;
166
+ }
167
+ #endif /* MPFR */
168
+
124
169
  #include "gmpf.h"
125
170
  #include "gmpq.h"
126
171
  /* #include "gmpz.h" */
@@ -135,6 +180,18 @@ static VALUE r_gmpfsg_set_default_prec(VALUE klass, VALUE arg)
135
180
  void Init_gmp() {
136
181
  mGMP = rb_define_module("GMP");
137
182
  rb_define_const(mGMP, "GMP_VERSION", rb_str_new2(gmp_version));
183
+ rb_define_const(mGMP, "GMP_CC", rb_str_new2(__GMP_CC));
184
+ rb_define_const(mGMP, "GMP_CFLAGS", rb_str_new2(__GMP_CFLAGS));
185
+ rb_define_const(mGMP, "GMP_BITS_PER_LIMB", INT2FIX(mp_bits_per_limb));
186
+ #ifdef MPFR
187
+ rb_define_const(mGMP, "MPFR_VERSION", rb_str_new2(MPFR_VERSION_STRING));
188
+ rb_define_const(mGMP, "MPFR_PREC_MIN", INT2FIX(MPFR_PREC_MIN));
189
+ rb_define_const(mGMP, "MPFR_PREC_MAX", INT2FIX(MPFR_PREC_MAX));
190
+ rb_define_const(mGMP, "GMP_RNDN", INT2FIX(0));
191
+ rb_define_const(mGMP, "GMP_RNDZ", INT2FIX(1));
192
+ rb_define_const(mGMP, "GMP_RNDU", INT2FIX(2));
193
+ rb_define_const(mGMP, "GMP_RNDD", INT2FIX(3));
194
+ #endif /* MPFR */
138
195
 
139
196
  cGMP_Z = rb_define_class_under(mGMP, "Z", rb_cInteger);
140
197
  init_gmpz();
@@ -149,8 +206,12 @@ void Init_gmp() {
149
206
 
150
207
  cGMP_F = rb_define_class_under (mGMP, "F", rb_cNumeric);
151
208
  init_gmpf();
152
- rb_define_singleton_method(cGMP_F, "default_prec", r_gmpfsg_get_default_prec, 0);
153
- rb_define_singleton_method(cGMP_F, "default_prec=", r_gmpfsg_set_default_prec, 1);
209
+ rb_define_singleton_method (cGMP_F, "default_prec", r_gmpfsg_get_default_prec, 0);
210
+ rb_define_singleton_method (cGMP_F, "default_prec=", r_gmpfsg_set_default_prec, 1);
211
+ #ifdef MPFR
212
+ rb_define_singleton_method (cGMP_F, "default_rounding_mode", r_gmpfsg_get_default_rounding_mode, 0);
213
+ rb_define_singleton_method (cGMP_F, "default_rounding_mode=", r_gmpfsg_set_default_rounding_mode, 1);
214
+ #endif /* MPFR */
154
215
  rb_define_method(cGMP_F, "coerce", r_gmpf_coerce, 1); // new method - testing
155
216
 
156
217
  /* rb_define_method(cGMP_F, "cmpabs", r_gmpf_cmpabs, 1);*/
@@ -160,34 +221,6 @@ void Init_gmp() {
160
221
 
161
222
  init_gmpbench_timing();
162
223
 
163
- #ifdef MPFR
164
- rb_define_method(cGMP_F, "exp", r_gmpfr_exp, 0);
165
- rb_define_method(cGMP_F, "log", r_gmpfr_log, 0);
166
- rb_define_method(cGMP_F, "sqrt", r_gmpfr_sqrt, 0);
167
- rb_define_method(cGMP_F, "cos", r_gmpfr_cos, 0);
168
- rb_define_method(cGMP_F, "sin", r_gmpfr_sin, 0);
169
- rb_define_method(cGMP_F, "tan", r_gmpfr_tan, 0);
170
- rb_define_method(cGMP_F, "acos", r_gmpfr_acos, 0);
171
- rb_define_method(cGMP_F, "asin", r_gmpfr_asin, 0);
172
- rb_define_method(cGMP_F, "atan", r_gmpfr_atan, 0);
173
- rb_define_method(cGMP_F, "cosh", r_gmpfr_cosh, 0);
174
- rb_define_method(cGMP_F, "sinh", r_gmpfr_sinh, 0);
175
- rb_define_method(cGMP_F, "tanh", r_gmpfr_tanh, 0);
176
- rb_define_method(cGMP_F, "acosh", r_gmpfr_acosh, 0);
177
- rb_define_method(cGMP_F, "asinh", r_gmpfr_asinh, 0);
178
- rb_define_method(cGMP_F, "atanh", r_gmpfr_atanh, 0);
179
- rb_define_method(cGMP_F, "log1p", r_gmpfr_log1p, 0);
180
- rb_define_method(cGMP_F, "expm1", r_gmpfr_expm1, 0);
181
- rb_define_method(cGMP_F, "log2", r_gmpfr_log2, 0);
182
- rb_define_method(cGMP_F, "log10", r_gmpfr_log10, 0);
183
-
184
- rb_define_method(cGMP_F, "nan?", r_gmpfr_nan_p, 0);
185
- rb_define_method(cGMP_F, "infinite?", r_gmpfr_inf_p, 0);
186
- rb_define_method(cGMP_F, "finite?", r_gmpfr_fin_p, 0);
187
- rb_define_method(cGMP_F, "number?", r_gmpfr_number_p, 0);
188
-
189
- rb_define_method(cGMP_F, "**", r_gmpfr_pow, 1);
190
- #endif /* MPFR */
191
224
  // more
192
225
 
193
226
  REGISTER_TAKEOVER(and, "&", "old_and")
data/ext/gmp.so ADDED
Binary file
data/ext/gmpf.c CHANGED
@@ -105,7 +105,7 @@ VALUE r_gmpf_initialize(int argc, VALUE *argv, VALUE self)
105
105
  else
106
106
  rb_raise(rb_eRangeError, "prec must be non-negative");
107
107
  } else {
108
- rb_raise(rb_eTypeError, "prec must be FixNum");
108
+ rb_raise(rb_eTypeError, "prec must be a Fixnum");
109
109
  }
110
110
  } else if (GMPF_P(arg)) {
111
111
  mpf_get_struct (arg, arg_val_f);
@@ -515,6 +515,166 @@ DEFUN_FLOAT_CMP(gt,>)
515
515
  DEFUN_FLOAT_CMP(ge,>=)
516
516
 
517
517
 
518
+ #ifdef MPFR
519
+
520
+ #define MPFR_SINGLE_FUNCTION(name) \
521
+ VALUE r_gmpfr_##name(VALUE self) \
522
+ { \
523
+ MP_FLOAT *self_val, *res_val; \
524
+ unsigned long prec; \
525
+ VALUE res; \
526
+ \
527
+ mpf_get_struct_prec (self, self_val, prec); \
528
+ mpf_make_struct_init (res, res_val, prec); \
529
+ mpfr_##name (res_val, self_val, __gmp_default_rounding_mode); \
530
+ \
531
+ return res; \
532
+ }
533
+
534
+ #define MPFR_CONST_FUNCTION(name) \
535
+ VALUE r_gmpfrsg_##name() \
536
+ { \
537
+ MP_FLOAT *res_val; \
538
+ VALUE res; \
539
+ \
540
+ mpf_make_struct (res, res_val); \
541
+ mpf_init (res_val); \
542
+ mpfr_##name (res_val, __gmp_default_rounding_mode); \
543
+ \
544
+ return res; \
545
+ }
546
+
547
+ MPFR_SINGLE_FUNCTION(sqrt)
548
+
549
+ MPFR_SINGLE_FUNCTION(log)
550
+ MPFR_SINGLE_FUNCTION(log2)
551
+ MPFR_SINGLE_FUNCTION(log10)
552
+ MPFR_SINGLE_FUNCTION(exp)
553
+
554
+ MPFR_SINGLE_FUNCTION(cos)
555
+ MPFR_SINGLE_FUNCTION(sin)
556
+ MPFR_SINGLE_FUNCTION(tan)
557
+ MPFR_SINGLE_FUNCTION(sec)
558
+ MPFR_SINGLE_FUNCTION(csc)
559
+ MPFR_SINGLE_FUNCTION(cot)
560
+
561
+ MPFR_SINGLE_FUNCTION(acos)
562
+ MPFR_SINGLE_FUNCTION(asin)
563
+ MPFR_SINGLE_FUNCTION(atan)
564
+
565
+ MPFR_SINGLE_FUNCTION(cosh)
566
+ MPFR_SINGLE_FUNCTION(sinh)
567
+ MPFR_SINGLE_FUNCTION(tanh)
568
+
569
+ MPFR_SINGLE_FUNCTION(acosh)
570
+ MPFR_SINGLE_FUNCTION(asinh)
571
+ MPFR_SINGLE_FUNCTION(atanh)
572
+
573
+ MPFR_SINGLE_FUNCTION(log1p)
574
+ MPFR_SINGLE_FUNCTION(expm1)
575
+
576
+ MPFR_CONST_FUNCTION(const_log2)
577
+ MPFR_CONST_FUNCTION(const_pi)
578
+ MPFR_CONST_FUNCTION(const_euler)
579
+ MPFR_CONST_FUNCTION(const_catalan)
580
+
581
+ static VALUE r_gmpfr_nan_p(VALUE self)
582
+ {
583
+ MP_FLOAT *self_val;
584
+
585
+ mpf_get_struct(self, self_val);
586
+ if (mpfr_nan_p(self_val)) {
587
+ return Qtrue;
588
+ }
589
+ else {
590
+ return Qfalse;
591
+ }
592
+ }
593
+
594
+ static VALUE r_gmpfr_inf_p(VALUE self)
595
+ {
596
+ MP_FLOAT *self_val;
597
+
598
+ mpf_get_struct(self, self_val);
599
+ if (mpfr_inf_p(self_val)) {
600
+ return Qtrue;
601
+ }
602
+ else {
603
+ return Qfalse;
604
+ }
605
+ }
606
+
607
+ static VALUE r_gmpfr_fin_p(VALUE self)
608
+ {
609
+ if (r_gmpfr_inf_p(self)) {
610
+ return Qfalse;
611
+ }
612
+ else {
613
+ return Qtrue;
614
+ }
615
+ }
616
+
617
+ static VALUE r_gmpfr_number_p(VALUE self)
618
+ {
619
+ MP_FLOAT *self_val;
620
+
621
+ mpf_get_struct(self, self_val);
622
+ if (mpfr_number_p(self_val)) {
623
+ return Qtrue;
624
+ }
625
+ else {
626
+ return Qfalse;
627
+ }
628
+ }
629
+
630
+ static VALUE r_gmpfr_pow(VALUE self, VALUE arg)
631
+ {
632
+ MP_FLOAT *self_val, *res_val, *arg_val_f;
633
+ MP_RAT *arg_val_q;
634
+ MP_INT *arg_val_z;
635
+ unsigned long prec;
636
+ VALUE res;
637
+
638
+ mpf_get_struct_prec(self, self_val, prec);
639
+
640
+ if (GMPF_P(arg)) {
641
+ mpf_get_struct(arg, arg_val_f);
642
+ prec_max(prec, arg_val_f);
643
+ mpf_make_struct_init(res, res_val, prec);
644
+ mpfr_pow(res_val, self_val, arg_val_f, __gmp_default_rounding_mode);
645
+ } else {
646
+ mpf_make_struct_init(res, res_val, prec);
647
+
648
+ if (GMPZ_P(arg)) {
649
+ mpz_get_struct(arg, arg_val_z);
650
+ mpf_set_z(res_val, arg_val_z);
651
+ mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
652
+ } else if (GMPQ_P(arg)) {
653
+ mpq_get_struct(arg, arg_val_q);
654
+ mpf_set_q(res_val, arg_val_q);
655
+ mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
656
+ } else if (FLOAT_P(arg)) {
657
+ mpf_set_d(res_val, NUM2DBL(arg));
658
+ mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
659
+ } else if (FIXNUM_P(arg)) {
660
+ mpfr_pow_si(res_val, self_val, FIX2INT(arg), __gmp_default_rounding_mode);
661
+ } else if (BIGNUM_P(arg)) {
662
+ mpz_temp_from_bignum(arg_val_z, arg);
663
+ mpf_set_z(res_val, arg_val_z);
664
+ mpz_temp_free(arg_val_z);
665
+ mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
666
+ } else {
667
+ typeerror(ZQFXBD);
668
+ }
669
+
670
+ }
671
+
672
+ return res;
673
+ }
674
+
675
+ #endif
676
+
677
+
518
678
  /**********************************************************************
519
679
  * _unsorted_ *
520
680
  **********************************************************************/
@@ -549,8 +709,6 @@ VALUE r_gmpf_get_prec(VALUE self)
549
709
  void init_gmpf()
550
710
  {
551
711
  mGMP = rb_define_module("GMP");
552
- // rb_define_module_function(mGMP, "Z", r_gmpmod_z, -1);
553
- // rb_define_module_function(mGMP, "Q", r_gmpmod_q, -1);
554
712
  rb_define_module_function(mGMP, "F", r_gmpmod_f, -1);
555
713
 
556
714
  cGMP_F = rb_define_class_under (mGMP, "F", rb_cNumeric);
@@ -583,6 +741,52 @@ void init_gmpf()
583
741
  rb_define_method(cGMP_F, "<=", r_gmpf_cmp_le, 1);
584
742
  rb_define_method(cGMP_F, "==", r_gmpf_eq, 1);
585
743
 
744
+ #ifdef MPFR
745
+ // Basic Arithmetic Functions
746
+ rb_define_method(cGMP_F, "sqrt", r_gmpfr_sqrt, 0);
747
+
748
+ rb_define_method(cGMP_F, "**", r_gmpfr_pow, 1);
749
+
750
+ // Comparison Functions
751
+ rb_define_method(cGMP_F, "nan?", r_gmpfr_nan_p, 0);
752
+ rb_define_method(cGMP_F, "infinite?", r_gmpfr_inf_p, 0);
753
+ rb_define_method(cGMP_F, "finite?", r_gmpfr_fin_p, 0);
754
+ rb_define_method(cGMP_F, "number?", r_gmpfr_number_p, 0);
755
+
756
+ // Special Functions
757
+ rb_define_method(cGMP_F, "log", r_gmpfr_log, 0);
758
+ rb_define_method(cGMP_F, "log2", r_gmpfr_log2, 0);
759
+ rb_define_method(cGMP_F, "log10", r_gmpfr_log10, 0);
760
+ rb_define_method(cGMP_F, "exp", r_gmpfr_exp, 0);
761
+
762
+ rb_define_method(cGMP_F, "cos", r_gmpfr_cos, 0);
763
+ rb_define_method(cGMP_F, "sin", r_gmpfr_sin, 0);
764
+ rb_define_method(cGMP_F, "tan", r_gmpfr_tan, 0);
765
+ rb_define_method(cGMP_F, "sec", r_gmpfr_sec, 0);
766
+ rb_define_method(cGMP_F, "csc", r_gmpfr_csc, 0);
767
+ rb_define_method(cGMP_F, "cot", r_gmpfr_cot, 0);
768
+
769
+ rb_define_method(cGMP_F, "acos", r_gmpfr_acos, 0);
770
+ rb_define_method(cGMP_F, "asin", r_gmpfr_asin, 0);
771
+ rb_define_method(cGMP_F, "atan", r_gmpfr_atan, 0);
772
+
773
+ rb_define_method(cGMP_F, "cosh", r_gmpfr_cosh, 0);
774
+ rb_define_method(cGMP_F, "sinh", r_gmpfr_sinh, 0);
775
+ rb_define_method(cGMP_F, "tanh", r_gmpfr_tanh, 0);
776
+
777
+ rb_define_method(cGMP_F, "acosh", r_gmpfr_acosh, 0);
778
+ rb_define_method(cGMP_F, "asinh", r_gmpfr_asinh, 0);
779
+ rb_define_method(cGMP_F, "atanh", r_gmpfr_atanh, 0);
780
+
781
+ rb_define_method(cGMP_F, "log1p", r_gmpfr_log1p, 0);
782
+ rb_define_method(cGMP_F, "expm1", r_gmpfr_expm1, 0);
783
+
784
+ rb_define_singleton_method(cGMP_F, "const_log2", r_gmpfrsg_const_log2, 0);
785
+ rb_define_singleton_method(cGMP_F, "const_pi", r_gmpfrsg_const_pi, 0);
786
+ rb_define_singleton_method(cGMP_F, "const_euler", r_gmpfrsg_const_euler, 0);
787
+ rb_define_singleton_method(cGMP_F, "const_catalan", r_gmpfrsg_const_catalan, 0);
788
+ #endif /* MPFR */
789
+
586
790
  // _unsorted_
587
791
  rb_define_method(cGMP_F, "floor", r_gmpf_floor, 0);
588
792
  rb_define_method(cGMP_F, "floor!", r_gmpf_floor_self, 0);
data/ext/gmpf.h CHANGED
@@ -9,136 +9,4 @@
9
9
 
10
10
  #include <ruby_gmp.h>
11
11
 
12
- #ifdef MPFR
13
- #define MPFR_SINGLE_FUNCTION(name) \
14
- static VALUE r_gmpfr_##name(VALUE self) \
15
- { \
16
- MP_FLOAT *self_val, *res_val; \
17
- unsigned long prec; \
18
- VALUE res; \
19
- \
20
- mpf_get_struct_prec(self, self_val, prec); \
21
- mpf_make_struct_init(res, res_val, prec); \
22
- mpfr_##name(res_val, self_val, __gmp_default_rounding_mode); \
23
- \
24
- return res; \
25
- }
26
-
27
- MPFR_SINGLE_FUNCTION(log)
28
- MPFR_SINGLE_FUNCTION(exp)
29
- MPFR_SINGLE_FUNCTION(sqrt)
30
- MPFR_SINGLE_FUNCTION(cos)
31
- MPFR_SINGLE_FUNCTION(sin)
32
- MPFR_SINGLE_FUNCTION(tan)
33
- MPFR_SINGLE_FUNCTION(acos)
34
- MPFR_SINGLE_FUNCTION(asin)
35
- MPFR_SINGLE_FUNCTION(atan)
36
- MPFR_SINGLE_FUNCTION(cosh)
37
- MPFR_SINGLE_FUNCTION(sinh)
38
- MPFR_SINGLE_FUNCTION(tanh)
39
- MPFR_SINGLE_FUNCTION(acosh)
40
- MPFR_SINGLE_FUNCTION(asinh)
41
- MPFR_SINGLE_FUNCTION(atanh)
42
- MPFR_SINGLE_FUNCTION(log1p)
43
- MPFR_SINGLE_FUNCTION(expm1)
44
- MPFR_SINGLE_FUNCTION(log2)
45
- MPFR_SINGLE_FUNCTION(log10)
46
-
47
- static VALUE r_gmpfr_nan_p(VALUE self)
48
- {
49
- MP_FLOAT *self_val;
50
-
51
- mpf_get_struct(self, self_val);
52
- if (mpfr_nan_p(self_val)) {
53
- return Qtrue;
54
- }
55
- else {
56
- return Qfalse;
57
- }
58
- }
59
-
60
- static VALUE r_gmpfr_inf_p(VALUE self)
61
- {
62
- MP_FLOAT *self_val;
63
-
64
- mpf_get_struct(self, self_val);
65
- if (mpfr_inf_p(self_val)) {
66
- return Qtrue;
67
- }
68
- else {
69
- return Qfalse;
70
- }
71
- }
72
-
73
- static VALUE r_gmpfr_fin_p(VALUE self)
74
- {
75
- if (r_gmpfr_inf_p(self)) {
76
- return Qfalse;
77
- }
78
- else {
79
- return Qtrue;
80
- }
81
- }
82
-
83
- static VALUE r_gmpfr_number_p(VALUE self)
84
- {
85
- MP_FLOAT *self_val;
86
-
87
- mpf_get_struct(self, self_val);
88
- if (mpfr_number_p(self_val)) {
89
- return Qtrue;
90
- }
91
- else {
92
- return Qfalse;
93
- }
94
- }
95
-
96
- static VALUE r_gmpfr_pow(VALUE self, VALUE arg)
97
- {
98
- MP_FLOAT *self_val, *res_val, *arg_val_f;
99
- MP_RAT *arg_val_q;
100
- MP_INT *arg_val_z;
101
- unsigned long prec;
102
- VALUE res;
103
-
104
- mpf_get_struct_prec(self, self_val, prec);
105
-
106
- if (GMPF_P(arg)) {
107
- mpf_get_struct(arg, arg_val_f);
108
- prec_max(prec, arg_val_f);
109
- mpf_make_struct_init(res, res_val, prec);
110
- mpfr_pow(res_val, self_val, arg_val_f, __gmp_default_rounding_mode);
111
- } else {
112
- mpf_make_struct_init(res, res_val, prec);
113
-
114
- if (GMPZ_P(arg)) {
115
- mpz_get_struct(arg, arg_val_z);
116
- mpf_set_z(res_val, arg_val_z);
117
- mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
118
- } else if (GMPQ_P(arg)) {
119
- mpq_get_struct(arg, arg_val_q);
120
- mpf_set_q(res_val, arg_val_q);
121
- mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
122
- } else if (FLOAT_P(arg)) {
123
- mpf_set_d(res_val, FLT2DBL(arg));
124
- mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
125
- } else if (FIXNUM_P(arg)) {
126
- mpfr_pow_si(res_val, self_val, FIX2INT(arg), __gmp_default_rounding_mode);
127
- } else if (BIGNUM_P(arg)) {
128
- mpz_temp_from_bignum(arg_val_z, arg);
129
- mpf_set_z(res_val, arg_val_z);
130
- mpz_temp_free(arg_val_z);
131
- mpfr_pow(res_val, self_val, res_val, __gmp_default_rounding_mode);
132
- } else {
133
- typeerror(ZQFXBD);
134
- }
135
-
136
- }
137
-
138
- return res;
139
- }
140
-
141
- #endif
142
-
143
-
144
12
  #endif