* file.c (rb_find_file_ext): guard load_path from GC.
[ruby-svn.git] / rational.c
blobf54cb70e249a835cf53aa796299f9d63263b706b
1 /*
2 rational.c: Coded by Tadayoshi Funaba 2008
4 This implementation is based on Keiju Ishitsuka's Rational library
5 which is written in ruby.
6 */
8 #include "ruby.h"
9 #include <math.h>
10 #include <float.h>
12 #define NDEBUG
13 #include <assert.h>
15 #ifndef RATIONAL_NAME
16 #define RATIONAL_NAME "Rational"
17 #endif
19 #define ZERO INT2FIX(0)
20 #define ONE INT2FIX(1)
21 #define TWO INT2FIX(2)
23 VALUE rb_cRational;
25 static ID id_Unify, id_abs, id_cmp, id_convert, id_equal_p,
26 id_expt, id_floor, id_format, id_idiv, id_inspect, id_negate, id_new,
27 id_new_bang, id_to_f, id_to_i, id_to_s, id_truncate;
29 #define f_boolcast(x) ((x) ? Qtrue : Qfalse)
31 #define binop(n,op) \
32 inline static VALUE \
33 f_##n(VALUE x, VALUE y)\
35 return rb_funcall(x, op, 1, y);\
38 #define fun1(n) \
39 inline static VALUE \
40 f_##n(VALUE x)\
42 return rb_funcall(x, id_##n, 0);\
45 #define fun2(n) \
46 inline static VALUE \
47 f_##n(VALUE x, VALUE y)\
49 return rb_funcall(x, id_##n, 1, y);\
52 inline static VALUE
53 f_add(VALUE x, VALUE y)
55 VALUE r;
56 if (FIXNUM_P(y)) {
57 if (FIX2LONG(y) == 0)
58 r = x;
59 else
60 r = rb_funcall(x, '+', 1, y);
62 else if (FIXNUM_P(x)) {
63 if (FIX2LONG(x) == 0)
64 r = y;
65 else
66 r = rb_funcall(x, '+', 1, y);
68 else
69 r = rb_funcall(x, '+', 1, y);
70 return r;
73 inline static VALUE
74 f_cmp(VALUE x, VALUE y)
76 VALUE r;
77 if (FIXNUM_P(x) && FIXNUM_P(y)) {
78 long c = FIX2LONG(x) - FIX2LONG(y);
79 if (c > 0)
80 c = 1;
81 else if (c < 0)
82 c = -1;
83 r = INT2FIX(c);
85 else
86 r = rb_funcall(x, id_cmp, 1, y);
87 return r;
90 inline static VALUE
91 f_div(VALUE x, VALUE y)
93 VALUE r;
94 if (FIXNUM_P(y) && FIX2LONG(y) == 1)
95 r = x;
96 else
97 r = rb_funcall(x, '/', 1, y);
98 return r;
101 inline static VALUE
102 f_gt_p(VALUE x, VALUE y)
104 VALUE r;
105 if (FIXNUM_P(x) && FIXNUM_P(y))
106 r = f_boolcast(FIX2LONG(x) > FIX2LONG(y));
107 else
108 r = rb_funcall(x, '>', 1, y);
109 return r;
112 inline static VALUE
113 f_lt_p(VALUE x, VALUE y)
115 VALUE r;
116 if (FIXNUM_P(x) && FIXNUM_P(y))
117 r = f_boolcast(FIX2LONG(x) < FIX2LONG(y));
118 else
119 r = rb_funcall(x, '<', 1, y);
120 return r;
123 binop(mod, '%')
125 inline static VALUE
126 f_mul(VALUE x, VALUE y)
128 VALUE r;
129 if (FIXNUM_P(y)) {
130 long _iy = FIX2LONG(y);
131 if (_iy == 0) {
132 if (TYPE(x) == T_FLOAT)
133 r = rb_float_new(0.0);
134 else
135 r = ZERO;
137 else if (_iy == 1)
138 r = x;
139 else
140 r = rb_funcall(x, '*', 1, y);
142 else if (FIXNUM_P(x)) {
143 long _ix = FIX2LONG(x);
144 if (_ix == 0) {
145 if (TYPE(y) == T_FLOAT)
146 r = rb_float_new(0.0);
147 else
148 r = ZERO;
150 else if (_ix == 1)
151 r = y;
152 else
153 r = rb_funcall(x, '*', 1, y);
155 else
156 r = rb_funcall(x, '*', 1, y);
157 return r;
160 inline static VALUE
161 f_sub(VALUE x, VALUE y)
163 VALUE r;
164 if (FIXNUM_P(y)) {
165 if (FIX2LONG(y) == 0)
166 r = x;
167 else
168 r = rb_funcall(x, '-', 1, y);
170 else
171 r = rb_funcall(x, '-', 1, y);
172 return r;
175 binop(xor, '^')
177 fun1(abs)
178 fun1(floor)
179 fun1(inspect)
180 fun1(negate)
181 fun1(to_f)
182 fun1(to_i)
183 fun1(to_s)
184 fun1(truncate)
186 inline static VALUE
187 f_equal_p(VALUE x, VALUE y)
189 VALUE r;
190 if (FIXNUM_P(x) && FIXNUM_P(y))
191 r = f_boolcast(FIX2LONG(x) == FIX2LONG(y));
192 else
193 r = rb_funcall(x, id_equal_p, 1, y);
194 return r;
197 fun2(expt)
198 fun2(idiv)
200 inline static VALUE
201 f_negative_p(VALUE x)
203 VALUE r;
204 if (FIXNUM_P(x))
205 r = f_boolcast(FIX2LONG(x) < 0);
206 else
207 r = rb_funcall(x, '<', 1, ZERO);
208 return r;
211 inline static VALUE
212 f_zero_p(VALUE x)
214 VALUE r;
215 if (FIXNUM_P(x))
216 r = f_boolcast(FIX2LONG(x) == 0);
217 else
218 r = rb_funcall(x, id_equal_p, 1, ZERO);
219 return r;
222 inline static VALUE
223 f_one_p(VALUE x)
225 VALUE r;
226 if (FIXNUM_P(x))
227 r = f_boolcast(FIX2LONG(x) == 1);
228 else
229 r = rb_funcall(x, id_equal_p, 1, ONE);
230 return r;
233 inline static VALUE
234 f_kind_of_p(VALUE x, VALUE c)
236 return rb_obj_is_kind_of(x, c);
239 inline static VALUE
240 k_numeric_p(VALUE x)
242 return f_kind_of_p(x, rb_cNumeric);
245 inline static VALUE
246 k_integer_p(VALUE x)
248 return f_kind_of_p(x, rb_cInteger);
251 inline static VALUE
252 k_float_p(VALUE x)
254 return f_kind_of_p(x, rb_cFloat);
257 inline static VALUE
258 k_rational_p(VALUE x)
260 return f_kind_of_p(x, rb_cRational);
263 #ifndef NDEBUG
264 #define f_gcd f_gcd_orig
265 #endif
267 inline static long
268 i_gcd(long x, long y)
270 long b;
272 if (x < 0)
273 x = -x;
274 if (y < 0)
275 y = -y;
277 if (x == 0)
278 return y;
279 if (y == 0)
280 return x;
282 b = 0;
283 while ((x & 1) == 0 && (y & 1) == 0) {
284 b += 1;
285 x >>= 1;
286 y >>= 1;
289 while ((x & 1) == 0)
290 x >>= 1;
292 while ((y & 1) == 0)
293 y >>= 1;
295 while (x != y) {
296 if (y > x) {
297 long t;
298 t = x;
299 x = y;
300 y = t;
302 x -= y;
303 while ((x & 1) == 0)
304 x >>= 1;
307 return x << b;
310 inline static VALUE
311 f_gcd(VALUE x, VALUE y)
313 VALUE z;
315 if (FIXNUM_P(x) && FIXNUM_P(y))
316 return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
318 if (f_negative_p(x))
319 x = f_negate(x);
320 if (f_negative_p(y))
321 y = f_negate(y);
323 if (f_zero_p(x))
324 return y;
325 if (f_zero_p(y))
326 return x;
328 for (;;) {
329 if (FIXNUM_P(x)) {
330 if (FIX2LONG(x) == 0)
331 return y;
332 if (FIXNUM_P(y))
333 return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
335 z = x;
336 x = f_mod(y, x);
337 y = z;
339 /* NOTREACHED */
342 #ifndef NDEBUG
343 #undef f_gcd
345 inline static VALUE
346 f_gcd(VALUE x, VALUE y)
348 VALUE r = f_gcd_orig(x, y);
349 if (!f_zero_p(r)) {
350 assert(f_zero_p(f_mod(x, r)));
351 assert(f_zero_p(f_mod(y, r)));
353 return r;
355 #endif
357 inline static VALUE
358 f_lcm(VALUE x, VALUE y)
360 if (f_zero_p(x) || f_zero_p(y))
361 return ZERO;
362 else
363 return f_abs(f_mul(f_div(x, f_gcd(x, y)), y));
366 #define get_dat1(x) \
367 struct RRational *dat;\
368 dat = ((struct RRational *)(x))
370 #define get_dat2(x,y) \
371 struct RRational *adat, *bdat;\
372 adat = ((struct RRational *)(x));\
373 bdat = ((struct RRational *)(y))
375 inline static VALUE
376 nurat_s_new_internal(VALUE klass, VALUE num, VALUE den)
378 NEWOBJ(obj, struct RRational);
379 OBJSETUP(obj, klass, T_RATIONAL);
381 obj->num = num;
382 obj->den = den;
384 return (VALUE)obj;
387 static VALUE
388 nurat_s_alloc(VALUE klass)
390 return nurat_s_new_internal(klass, ZERO, ONE);
393 static VALUE
394 nurat_s_new_bang(int argc, VALUE *argv, VALUE klass)
396 VALUE num, den;
398 switch (rb_scan_args(argc, argv, "11", &num, &den)) {
399 case 1:
400 if (!k_integer_p(num))
401 num = f_to_i(num);
402 den = ONE;
403 break;
404 default:
405 if (!k_integer_p(num))
406 num = f_to_i(num);
407 if (!k_integer_p(den))
408 den = f_to_i(den);
410 switch (FIX2INT(f_cmp(den, ZERO))) {
411 case -1:
412 num = f_negate(num);
413 den = f_negate(den);
414 break;
415 case 0:
416 rb_raise(rb_eZeroDivError, "devided by zero");
417 break;
419 break;
422 return nurat_s_new_internal(klass, num, den);
425 inline static VALUE
426 f_rational_new_bang1(VALUE klass, VALUE x)
428 return nurat_s_new_internal(klass, x, ONE);
431 inline static VALUE
432 f_rational_new_bang2(VALUE klass, VALUE x, VALUE y)
434 assert(!f_negative_p(y));
435 assert(!f_zero_p(y));
436 return nurat_s_new_internal(klass, x, y);
439 #define f_unify_p(klass) rb_const_defined(klass, id_Unify)
441 inline static void
442 nurat_int_check(VALUE num)
444 switch (TYPE(num)) {
445 case T_FIXNUM:
446 case T_BIGNUM:
447 break;
448 default:
449 rb_raise(rb_eArgError, "not an integer");
453 inline static VALUE
454 nurat_s_canonicalize_internal(VALUE klass, VALUE num, VALUE den)
456 VALUE gcd;
458 switch (FIX2INT(f_cmp(den, ZERO))) {
459 case -1:
460 num = f_negate(num);
461 den = f_negate(den);
462 break;
463 case 0:
464 rb_raise(rb_eZeroDivError, "devided by zero");
465 break;
468 gcd = f_gcd(num, den);
469 num = f_idiv(num, gcd);
470 den = f_idiv(den, gcd);
472 if (f_one_p(den) && f_unify_p(klass))
473 return num;
474 else
475 return nurat_s_new_internal(klass, num, den);
478 inline static VALUE
479 nurat_s_canonicalize_internal_no_reduce(VALUE klass, VALUE num, VALUE den)
481 switch (FIX2INT(f_cmp(den, ZERO))) {
482 case -1:
483 num = f_negate(num);
484 den = f_negate(den);
485 break;
486 case 0:
487 rb_raise(rb_eZeroDivError, "devided by zero");
488 break;
491 if (f_equal_p(den, ONE) && f_unify_p(klass))
492 return num;
493 else
494 return nurat_s_new_internal(klass, num, den);
497 #if 0
498 static VALUE
499 nurat_s_canonicalize(int argc, VALUE *argv, VALUE klass)
501 VALUE num, den;
503 if (rb_scan_args(argc, argv, "11", &num, &den) == 1) {
504 den = ONE;
507 nurat_int_check(num);
508 nurat_int_check(den);
510 return nurat_s_canonicalize_internal(klass, num, den);
512 #endif
514 static VALUE
515 nurat_s_new(VALUE klass, VALUE num, VALUE den)
517 nurat_int_check(num);
518 nurat_int_check(den);
520 return nurat_s_canonicalize_internal(klass, num, den);
523 static VALUE
524 nurat_s_new_m(int argc, VALUE *argv, VALUE klass)
526 VALUE num, den;
528 if (rb_scan_args(argc, argv, "11", &num, &den) == 1) {
529 den = ONE;
531 return nurat_s_new(klass, num, den);
534 inline static VALUE
535 f_rational_new1(VALUE klass, VALUE x)
537 assert(!k_rational_p(x));
538 return nurat_s_canonicalize_internal(klass, x, ONE);
541 inline static VALUE
542 f_rational_new2(VALUE klass, VALUE x, VALUE y)
544 assert(!k_rational_p(x));
545 assert(!k_rational_p(y));
546 return nurat_s_canonicalize_internal(klass, x, y);
549 inline static VALUE
550 f_rational_new_no_reduce1(VALUE klass, VALUE x)
552 assert(!k_rational_p(x));
553 return nurat_s_canonicalize_internal_no_reduce(klass, x, ONE);
556 inline static VALUE
557 f_rational_new_no_reduce2(VALUE klass, VALUE x, VALUE y)
559 assert(!k_rational_p(x));
560 assert(!k_rational_p(y));
561 return nurat_s_canonicalize_internal_no_reduce(klass, x, y);
564 static VALUE
565 nurat_f_rational(int argc, VALUE *argv, VALUE klass)
567 return rb_funcall2(rb_cRational, id_convert, argc, argv);
570 static VALUE
571 nurat_numerator(VALUE self)
573 get_dat1(self);
574 return dat->num;
577 static VALUE
578 nurat_denominator(VALUE self)
580 get_dat1(self);
581 return dat->den;
584 #ifndef NDEBUG
585 #define f_imul f_imul_orig
586 #endif
588 inline static VALUE
589 f_imul(long a, long b)
591 VALUE r;
592 long c;
594 if (a == 0 || b == 0)
595 return ZERO;
596 else if (a == 1)
597 return LONG2NUM(b);
598 else if (b == 1)
599 return LONG2NUM(a);
601 c = a * b;
602 r = LONG2NUM(c);
603 if (NUM2LONG(r) != c || (c / a) != b)
604 r = rb_big_mul(rb_int2big(a), rb_int2big(b));
605 return r;
608 #ifndef NDEBUG
609 #undef f_imul
611 inline static VALUE
612 f_imul(long x, long y)
614 VALUE r = f_imul_orig(x, y);
615 assert(f_equal_p(r, f_mul(LONG2NUM(x), LONG2NUM(y))));
616 return r;
618 #endif
620 inline static VALUE
621 f_addsub(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
623 VALUE num, den;
625 if (FIXNUM_P(anum) && FIXNUM_P(aden) &&
626 FIXNUM_P(bnum) && FIXNUM_P(bden)) {
627 long an = FIX2LONG(anum);
628 long ad = FIX2LONG(aden);
629 long bn = FIX2LONG(bnum);
630 long bd = FIX2LONG(bden);
631 long ig = i_gcd(ad, bd);
633 VALUE g = LONG2NUM(ig);
634 VALUE a = f_imul(an, bd / ig);
635 VALUE b = f_imul(bn, ad / ig);
636 VALUE c;
638 if (k == '+')
639 c = f_add(a, b);
640 else
641 c = f_sub(a, b);
643 b = f_idiv(aden, g);
644 g = f_gcd(c, g);
645 num = f_idiv(c, g);
646 a = f_idiv(bden, g);
647 den = f_mul(a, b);
649 else {
650 VALUE g = f_gcd(aden, bden);
651 VALUE a = f_mul(anum, f_idiv(bden, g));
652 VALUE b = f_mul(bnum, f_idiv(aden, g));
653 VALUE c;
655 if (k == '+')
656 c = f_add(a, b);
657 else
658 c = f_sub(a, b);
660 b = f_idiv(aden, g);
661 g = f_gcd(c, g);
662 num = f_idiv(c, g);
663 a = f_idiv(bden, g);
664 den = f_mul(a, b);
666 return f_rational_new_no_reduce2(CLASS_OF(self), num, den);
669 static VALUE
670 nurat_add(VALUE self, VALUE other)
672 switch (TYPE(other)) {
673 case T_FIXNUM:
674 case T_BIGNUM:
676 get_dat1(self);
678 return f_addsub(self,
679 dat->num, dat->den,
680 other, ONE, '+');
682 case T_FLOAT:
683 return f_add(f_to_f(self), other);
684 case T_RATIONAL:
686 get_dat2(self, other);
688 return f_addsub(self,
689 adat->num, adat->den,
690 bdat->num, bdat->den, '+');
692 default:
693 return rb_num_coerce_bin(self, other, '+');
697 static VALUE
698 nurat_sub(VALUE self, VALUE other)
700 switch (TYPE(other)) {
701 case T_FIXNUM:
702 case T_BIGNUM:
704 get_dat1(self);
706 return f_addsub(self,
707 dat->num, dat->den,
708 other, ONE, '-');
710 case T_FLOAT:
711 return f_sub(f_to_f(self), other);
712 case T_RATIONAL:
714 get_dat2(self, other);
716 return f_addsub(self,
717 adat->num, adat->den,
718 bdat->num, bdat->den, '-');
720 default:
721 return rb_num_coerce_bin(self, other, '-');
725 inline static VALUE
726 f_muldiv(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
728 VALUE num, den;
730 if (k == '/') {
731 VALUE t;
733 if (f_negative_p(bnum)) {
734 anum = f_negate(anum);
735 bnum = f_negate(bnum);
737 t = bnum;
738 bnum = bden;
739 bden = t;
742 if (FIXNUM_P(anum) && FIXNUM_P(aden) &&
743 FIXNUM_P(bnum) && FIXNUM_P(bden)) {
744 long an = FIX2LONG(anum);
745 long ad = FIX2LONG(aden);
746 long bn = FIX2LONG(bnum);
747 long bd = FIX2LONG(bden);
748 long g1 = i_gcd(an, bd);
749 long g2 = i_gcd(ad, bn);
751 num = f_imul(an / g1, bn / g2);
752 den = f_imul(ad / g2, bd / g1);
754 else {
755 VALUE g1 = f_gcd(anum, bden);
756 VALUE g2 = f_gcd(aden, bnum);
758 num = f_mul(f_idiv(anum, g1), f_idiv(bnum, g2));
759 den = f_mul(f_idiv(aden, g2), f_idiv(bden, g1));
761 return f_rational_new_no_reduce2(CLASS_OF(self), num, den);
764 static VALUE
765 nurat_mul(VALUE self, VALUE other)
767 switch (TYPE(other)) {
768 case T_FIXNUM:
769 case T_BIGNUM:
771 get_dat1(self);
773 return f_muldiv(self,
774 dat->num, dat->den,
775 other, ONE, '*');
777 case T_FLOAT:
778 return f_mul(f_to_f(self), other);
779 case T_RATIONAL:
781 get_dat2(self, other);
783 return f_muldiv(self,
784 adat->num, adat->den,
785 bdat->num, bdat->den, '*');
787 default:
788 return rb_num_coerce_bin(self, other, '*');
792 static VALUE
793 nurat_div(VALUE self, VALUE other)
795 switch (TYPE(other)) {
796 case T_FIXNUM:
797 case T_BIGNUM:
798 if (f_zero_p(other))
799 rb_raise(rb_eZeroDivError, "devided by zero");
801 get_dat1(self);
803 return f_muldiv(self,
804 dat->num, dat->den,
805 other, ONE, '/');
807 case T_FLOAT:
808 return rb_funcall(f_to_f(self), '/', 1, other);
809 case T_RATIONAL:
810 if (f_zero_p(other))
811 rb_raise(rb_eZeroDivError, "devided by zero");
813 get_dat2(self, other);
815 return f_muldiv(self,
816 adat->num, adat->den,
817 bdat->num, bdat->den, '/');
819 default:
820 return rb_num_coerce_bin(self, other, '/');
824 static VALUE
825 nurat_fdiv(VALUE self, VALUE other)
827 return f_div(f_to_f(self), other);
830 static VALUE
831 nurat_expt(VALUE self, VALUE other)
833 if (f_zero_p(other))
834 return f_rational_new_bang1(CLASS_OF(self), ONE);
836 if (k_rational_p(other)) {
837 get_dat1(other);
839 if (f_one_p(dat->den))
840 other = dat->num; /* good? */
843 switch (TYPE(other)) {
844 case T_FIXNUM:
845 case T_BIGNUM:
847 VALUE num, den;
849 get_dat1(self);
851 switch (FIX2INT(f_cmp(other, ZERO))) {
852 case 1:
853 num = f_expt(dat->num, other);
854 den = f_expt(dat->den, other);
855 break;
856 case -1:
857 num = f_expt(dat->den, f_negate(other));
858 den = f_expt(dat->num, f_negate(other));
859 break;
860 default:
861 num = ONE;
862 den = ONE;
863 break;
865 return f_rational_new2(CLASS_OF(self), num, den);
867 case T_FLOAT:
868 case T_RATIONAL:
869 return f_expt(f_to_f(self), other);
870 default:
871 return rb_num_coerce_bin(self, other, id_expt);
875 static VALUE
876 nurat_cmp(VALUE self, VALUE other)
878 switch (TYPE(other)) {
879 case T_FIXNUM:
880 case T_BIGNUM:
882 get_dat1(self);
884 if (FIXNUM_P(dat->den) && FIX2LONG(dat->den) == 1)
885 return f_cmp(dat->num, other);
886 else
887 return f_cmp(self, f_rational_new_bang1(CLASS_OF(self), other));
889 case T_FLOAT:
890 return f_cmp(f_to_f(self), other);
891 case T_RATIONAL:
893 VALUE num1, num2;
895 get_dat2(self, other);
897 if (FIXNUM_P(adat->num) && FIXNUM_P(adat->den) &&
898 FIXNUM_P(bdat->num) && FIXNUM_P(bdat->den)) {
899 num1 = f_imul(FIX2LONG(adat->num), FIX2LONG(bdat->den));
900 num2 = f_imul(FIX2LONG(bdat->num), FIX2LONG(adat->den));
902 else {
903 num1 = f_mul(adat->num, bdat->den);
904 num2 = f_mul(bdat->num, adat->den);
906 return f_cmp(f_sub(num1, num2), ZERO);
908 default:
909 return rb_num_coerce_bin(self, other, id_cmp);
913 static VALUE
914 nurat_equal_p(VALUE self, VALUE other)
916 switch (TYPE(other)) {
917 case T_FIXNUM:
918 case T_BIGNUM:
920 get_dat1(self);
922 if (!FIXNUM_P(dat->den))
923 return Qfalse;
924 if (FIX2LONG(dat->den) != 1)
925 return Qfalse;
926 if (f_equal_p(dat->num, other))
927 return Qtrue;
928 else
929 return Qfalse;
931 case T_FLOAT:
932 return f_equal_p(f_to_f(self), other);
933 case T_RATIONAL:
935 get_dat2(self, other);
937 return f_boolcast(f_equal_p(adat->num, bdat->num) &&
938 f_equal_p(adat->den, bdat->den));
940 default:
941 return f_equal_p(other, self);
945 static VALUE
946 nurat_coerce(VALUE self, VALUE other)
948 switch (TYPE(other)) {
949 case T_FIXNUM:
950 case T_BIGNUM:
951 return rb_assoc_new(f_rational_new_bang1(CLASS_OF(self), other), self);
952 case T_FLOAT:
953 return rb_assoc_new(other, f_to_f(self));
956 rb_raise(rb_eTypeError, "%s can't be coerced into %s",
957 rb_obj_classname(other), rb_obj_classname(self));
958 return Qnil;
961 static VALUE
962 nurat_idiv(VALUE self, VALUE other)
964 return f_floor(f_div(self, other));
967 static VALUE
968 nurat_mod(VALUE self, VALUE other)
970 VALUE val = f_floor(f_div(self, other));
971 return f_sub(self, f_mul(other, val));
974 static VALUE
975 nurat_divmod(VALUE self, VALUE other)
977 VALUE val = f_floor(f_div(self, other));
978 return rb_assoc_new(val, f_sub(self, f_mul(other, val)));
981 #if 0
982 static VALUE
983 nurat_quot(VALUE self, VALUE other)
985 return f_truncate(f_div(self, other));
987 #endif
989 static VALUE
990 nurat_rem(VALUE self, VALUE other)
992 VALUE val = f_truncate(f_div(self, other));
993 return f_sub(self, f_mul(other, val));
996 #if 0
997 static VALUE
998 nurat_quotrem(VALUE self, VALUE other)
1000 VALUE val = f_truncate(f_div(self, other));
1001 return rb_assoc_new(val, f_sub(self, f_mul(other, val)));
1003 #endif
1005 static VALUE
1006 nurat_abs(VALUE self)
1008 if (!f_negative_p(self))
1009 return self;
1010 else
1011 return f_negate(self);
1014 #if 0
1015 static VALUE
1016 nurat_true(VALUE self)
1018 return Qtrue;
1020 #endif
1022 static VALUE
1023 nurat_floor(VALUE self)
1025 get_dat1(self);
1026 return f_idiv(dat->num, dat->den);
1029 static VALUE
1030 nurat_ceil(VALUE self)
1032 get_dat1(self);
1033 return f_negate(f_idiv(f_negate(dat->num), dat->den));
1036 static VALUE
1037 nurat_truncate(VALUE self)
1039 get_dat1(self);
1040 if (f_negative_p(dat->num))
1041 return f_negate(f_idiv(f_negate(dat->num), dat->den));
1042 return f_idiv(dat->num, dat->den);
1045 static VALUE
1046 nurat_round(VALUE self)
1048 get_dat1(self);
1050 if (f_negative_p(dat->num)) {
1051 VALUE num, den;
1053 num = f_negate(dat->num);
1054 num = f_add(f_mul(num, TWO), dat->den);
1055 den = f_mul(dat->den, TWO);
1056 return f_negate(f_idiv(num, den));
1058 else {
1059 VALUE num = f_add(f_mul(dat->num, TWO), dat->den);
1060 VALUE den = f_mul(dat->den, TWO);
1061 return f_idiv(num, den);
1065 #define f_size(x) rb_funcall(x, rb_intern("size"), 0)
1066 #define f_rshift(x,y) rb_funcall(x, rb_intern(">>"), 1, y)
1068 inline static long
1069 i_ilog2(VALUE x)
1071 long q, r, fx;
1073 assert(!f_lt_p(x, ONE));
1075 q = (NUM2LONG(f_size(x)) - sizeof(long)) * 8 + 1;
1077 if (q > 0)
1078 x = f_rshift(x, LONG2NUM(q));
1080 fx = NUM2LONG(x);
1082 r = -1;
1083 while (fx) {
1084 fx >>= 1;
1085 r += 1;
1088 return q + r;
1091 static VALUE
1092 nurat_to_f(VALUE self)
1094 VALUE num, den;
1095 int minus = 0;
1096 long nl, dl, ml, ne, de;
1097 int e;
1098 double f;
1101 get_dat1(self);
1103 if (f_zero_p(dat->num))
1104 return rb_float_new(0.0);
1106 num = dat->num;
1107 den = dat->den;
1110 if (f_negative_p(num)) {
1111 num = f_negate(num);
1112 minus = 1;
1115 nl = i_ilog2(num);
1116 dl = i_ilog2(den);
1117 ml = (long)(log(DBL_MAX) / log(2.0) - 1); /* should be a static */
1119 ne = 0;
1120 if (nl > ml) {
1121 ne = nl - ml;
1122 num = f_rshift(num, LONG2NUM(ne));
1125 de = 0;
1126 if (dl > ml) {
1127 de = dl - ml;
1128 den = f_rshift(den, LONG2NUM(de));
1131 e = (int)(ne - de);
1133 if ((e > DBL_MAX_EXP) || (e < DBL_MIN_EXP)) {
1134 rb_warning("%s out of Float range", rb_obj_classname(self));
1135 return rb_float_new(e > 0 ? HUGE_VAL : 0.0);
1138 f = NUM2DBL(num) / NUM2DBL(den);
1139 if (minus)
1140 f = -f;
1141 f = ldexp(f, e);
1143 if (isinf(f) || isnan(f))
1144 rb_warning("%s out of Float range", rb_obj_classname(self));
1146 return rb_float_new(f);
1149 static VALUE
1150 nurat_to_r(VALUE self)
1152 return self;
1155 static VALUE
1156 nurat_hash(VALUE self)
1158 get_dat1(self);
1159 return f_xor(dat->num, dat->den);
1162 static VALUE
1163 nurat_to_s(VALUE self)
1165 get_dat1(self);
1167 if (f_one_p(dat->den))
1168 return f_to_s(dat->num);
1169 else
1170 return rb_funcall(rb_mKernel, id_format, 3,
1171 rb_str_new2("%d/%d"), dat->num, dat->den);
1174 static VALUE
1175 nurat_inspect(VALUE self)
1177 get_dat1(self);
1178 return rb_funcall(rb_mKernel, id_format, 3,
1179 rb_str_new2("Rational(%d, %d)"), dat->num, dat->den);
1182 static VALUE
1183 nurat_marshal_dump(VALUE self)
1185 get_dat1(self);
1186 return rb_assoc_new(dat->num, dat->den);
1189 static VALUE
1190 nurat_marshal_load(VALUE self, VALUE a)
1192 get_dat1(self);
1193 dat->num = RARRAY_PTR(a)[0];
1194 dat->den = RARRAY_PTR(a)[1];
1196 if (f_zero_p(dat->den))
1197 rb_raise(rb_eZeroDivError, "devided by zero");
1199 return self;
1202 /* --- */
1204 VALUE
1205 rb_gcd(VALUE self, VALUE other)
1207 nurat_int_check(other);
1208 return f_gcd(self, other);
1211 VALUE
1212 rb_lcm(VALUE self, VALUE other)
1214 nurat_int_check(other);
1215 return f_lcm(self, other);
1218 VALUE
1219 rb_gcdlcm(VALUE self, VALUE other)
1221 nurat_int_check(other);
1222 return rb_assoc_new(f_gcd(self, other), f_lcm(self, other));
1225 VALUE
1226 rb_rational_raw(VALUE x, VALUE y)
1228 return nurat_s_new_internal(rb_cRational, x, y);
1231 VALUE
1232 rb_rational_new(VALUE x, VALUE y)
1234 return nurat_s_canonicalize_internal(rb_cRational, x, y);
1237 static VALUE nurat_s_convert(int argc, VALUE *argv, VALUE klass);
1239 VALUE
1240 rb_Rational(VALUE x, VALUE y)
1242 VALUE a[2];
1243 a[0] = x;
1244 a[1] = y;
1245 return nurat_s_convert(2, a, rb_cRational);
1248 static VALUE
1249 nilclass_to_r(VALUE self)
1251 return rb_rational_new1(INT2FIX(0));
1254 static VALUE
1255 integer_to_r(VALUE self)
1257 return rb_rational_new1(self);
1260 static VALUE
1261 float_decode(VALUE self)
1263 double f;
1264 int n;
1266 f = frexp(RFLOAT_VALUE(self), &n);
1267 f = ldexp(f, DBL_MANT_DIG);
1268 n -= DBL_MANT_DIG;
1269 return rb_assoc_new(f_to_i(rb_float_new(f)), INT2FIX(n));
1272 static VALUE
1273 float_to_r(VALUE self)
1275 VALUE a = float_decode(self);
1276 return f_mul(RARRAY_PTR(a)[0],
1277 f_expt(INT2FIX(FLT_RADIX), RARRAY_PTR(a)[1]));
1280 static VALUE rat_pat, an_e_pat, a_dot_pat, underscores_pat, an_underscore;
1282 #define DIGITS "(?:\\d(?:_\\d|\\d)*)"
1283 #define NUMERATOR "(?:" DIGITS "?\\.)?" DIGITS "(?:[eE][-+]?" DIGITS ")?"
1284 #define DENOMINATOR "[-+]?" DIGITS
1285 #define PATTERN "\\A([-+])?(" NUMERATOR ")(?:\\/(" DENOMINATOR "))?"
1287 static void
1288 make_patterns(void)
1290 static char rat_pat_source[] = PATTERN;
1291 static char an_e_pat_source[] = "[eE]";
1292 static char a_dot_pat_source[] = "\\.";
1293 static char underscores_pat_source[] = "_+";
1295 rat_pat = rb_reg_new(rat_pat_source, sizeof rat_pat_source - 1, 0);
1296 rb_global_variable(&rat_pat);
1298 an_e_pat = rb_reg_new(an_e_pat_source, sizeof an_e_pat_source - 1, 0);
1299 rb_global_variable(&an_e_pat);
1301 a_dot_pat = rb_reg_new(a_dot_pat_source, sizeof a_dot_pat_source - 1, 0);
1302 rb_global_variable(&a_dot_pat);
1304 underscores_pat = rb_reg_new(underscores_pat_source,
1305 sizeof underscores_pat_source - 1, 0);
1306 rb_global_variable(&underscores_pat);
1308 an_underscore = rb_str_new2("_");
1309 rb_global_variable(&an_underscore);
1312 #define id_strip rb_intern("strip")
1313 #define f_strip(x) rb_funcall(x, id_strip, 0)
1315 #define id_match rb_intern("match")
1316 #define f_match(x,y) rb_funcall(x, id_match, 1, y)
1318 #define id_aref rb_intern("[]")
1319 #define f_aref(x,y) rb_funcall(x, id_aref, 1, y)
1321 #define id_post_match rb_intern("post_match")
1322 #define f_post_match(x) rb_funcall(x, id_post_match, 0)
1324 #define id_split rb_intern("split")
1325 #define f_split(x,y) rb_funcall(x, id_split, 1, y)
1327 #include <ctype.h>
1329 static VALUE
1330 string_to_r_internal(VALUE self)
1332 VALUE s, m;
1334 s = f_strip(self);
1336 if (RSTRING_LEN(s) == 0)
1337 return rb_assoc_new(Qnil, self);
1339 m = f_match(rat_pat, s);
1341 if (!NIL_P(m)) {
1342 VALUE v, ifp, exp, ip, fp;
1343 VALUE si = f_aref(m, INT2FIX(1));
1344 VALUE nu = f_aref(m, INT2FIX(2));
1345 VALUE de = f_aref(m, INT2FIX(3));
1346 VALUE re = f_post_match(m);
1349 VALUE a;
1351 a = f_split(nu, an_e_pat);
1352 ifp = RARRAY_PTR(a)[0];
1353 if (RARRAY_LEN(a) != 2)
1354 exp = Qnil;
1355 else
1356 exp = RARRAY_PTR(a)[1];
1358 a = f_split(ifp, a_dot_pat);
1359 ip = RARRAY_PTR(a)[0];
1360 if (RARRAY_LEN(a) != 2)
1361 fp = Qnil;
1362 else
1363 fp = RARRAY_PTR(a)[1];
1366 v = rb_rational_new1(f_to_i(ip));
1368 if (!NIL_P(fp)) {
1369 char *p = StringValuePtr(fp);
1370 long count = 0;
1371 VALUE l;
1373 while (*p) {
1374 if (isdigit(*p))
1375 count++;
1376 p++;
1379 l = f_expt(INT2FIX(10), LONG2NUM(count));
1380 v = f_mul(v, l);
1381 v = f_add(v, f_to_i(fp));
1382 v = f_div(v, l);
1384 if (!NIL_P(exp))
1385 v = f_mul(v, f_expt(INT2FIX(10), f_to_i(exp)));
1386 if (!NIL_P(si) && *StringValuePtr(si) == '-')
1387 v = f_negate(v);
1388 if (!NIL_P(de))
1389 v = f_div(v, f_to_i(de));
1391 return rb_assoc_new(v, re);
1393 return rb_assoc_new(Qnil, self);
1396 static VALUE
1397 string_to_r_strict(VALUE self)
1399 VALUE a = string_to_r_internal(self);
1400 if (NIL_P(RARRAY_PTR(a)[0]) || RSTRING_LEN(RARRAY_PTR(a)[1]) > 0) {
1401 VALUE s = f_inspect(self);
1402 rb_raise(rb_eArgError, "invalid value for Rational: %s",
1403 StringValuePtr(s));
1405 return RARRAY_PTR(a)[0];
1408 #define id_gsub rb_intern("gsub")
1409 #define f_gsub(x,y,z) rb_funcall(x, id_gsub, 2, y, z)
1411 static VALUE
1412 string_to_r(VALUE self)
1414 VALUE s = f_gsub(self, underscores_pat, an_underscore);
1415 VALUE a = string_to_r_internal(s);
1416 if (!NIL_P(RARRAY_PTR(a)[0]))
1417 return RARRAY_PTR(a)[0];
1418 return rb_rational_new1(INT2FIX(0));
1421 #define id_to_r rb_intern("to_r")
1422 #define f_to_r(x) rb_funcall(x, id_to_r, 0)
1424 static VALUE
1425 nurat_s_convert(int argc, VALUE *argv, VALUE klass)
1427 VALUE a1, a2;
1429 if (rb_scan_args(argc, argv, "02", &a1, &a2) == 1) {
1430 a2 = ONE;
1433 switch (TYPE(a1)) {
1434 case T_COMPLEX:
1435 if (k_float_p(RCOMPLEX(a1)->image) || !f_zero_p(RCOMPLEX(a1)->image)) {
1436 VALUE s = f_to_s(a1);
1437 rb_raise(rb_eRangeError, "can't accept %s",
1438 StringValuePtr(s));
1440 a1 = RCOMPLEX(a1)->real;
1443 switch (TYPE(a2)) {
1444 case T_COMPLEX:
1445 if (k_float_p(RCOMPLEX(a2)->image) || !f_zero_p(RCOMPLEX(a2)->image)) {
1446 VALUE s = f_to_s(a2);
1447 rb_raise(rb_eRangeError, "can't accept %s",
1448 StringValuePtr(s));
1450 a2 = RCOMPLEX(a2)->real;
1453 switch (TYPE(a1)) {
1454 case T_FIXNUM:
1455 case T_BIGNUM:
1456 break;
1457 case T_FLOAT:
1458 a1 = f_to_r(a1);
1459 break;
1460 case T_STRING:
1461 a1 = string_to_r_strict(a1);
1462 break;
1465 switch (TYPE(a2)) {
1466 case T_FIXNUM:
1467 case T_BIGNUM:
1468 break;
1469 case T_FLOAT:
1470 a2 = f_to_r(a2);
1471 break;
1472 case T_STRING:
1473 a2 = string_to_r_strict(a2);
1474 break;
1477 switch (TYPE(a1)) {
1478 case T_RATIONAL:
1479 if (NIL_P(a2) || f_zero_p(a2))
1480 return a1;
1481 else
1482 return f_div(a1, a2);
1485 switch (TYPE(a2)) {
1486 case T_RATIONAL:
1487 return f_div(a1, a2);
1490 return nurat_s_new(klass, a1, a2);
1493 static VALUE
1494 nurat_s_induced_from(VALUE klass, VALUE n)
1496 return f_to_r(n);
1499 void
1500 Init_Rational(void)
1502 assert(fprintf(stderr, "assert() is now active\n"));
1504 id_Unify = rb_intern("Unify");
1505 id_abs = rb_intern("abs");
1506 id_cmp = rb_intern("<=>");
1507 id_convert = rb_intern("convert");
1508 id_equal_p = rb_intern("==");
1509 id_expt = rb_intern("**");
1510 id_floor = rb_intern("floor");
1511 id_format = rb_intern("format");
1512 id_idiv = rb_intern("div");
1513 id_inspect = rb_intern("inspect");
1514 id_negate = rb_intern("-@");
1515 id_new = rb_intern("new");
1516 id_new_bang = rb_intern("new!");
1517 id_to_f = rb_intern("to_f");
1518 id_to_i = rb_intern("to_i");
1519 id_to_s = rb_intern("to_s");
1520 id_truncate = rb_intern("truncate");
1522 rb_cRational = rb_define_class(RATIONAL_NAME, rb_cNumeric);
1524 rb_define_alloc_func(rb_cRational, nurat_s_alloc);
1525 rb_funcall(rb_cRational, rb_intern("private_class_method"), 1,
1526 ID2SYM(rb_intern("allocate")));
1528 rb_define_singleton_method(rb_cRational, "new!", nurat_s_new_bang, -1);
1529 rb_funcall(rb_cRational, rb_intern("private_class_method"), 1,
1530 ID2SYM(rb_intern("new!")));
1532 rb_define_singleton_method(rb_cRational, "new", nurat_s_new_m, -1);
1533 rb_funcall(rb_cRational, rb_intern("private_class_method"), 1,
1534 ID2SYM(rb_intern("new")));
1536 rb_define_global_function(RATIONAL_NAME, nurat_f_rational, -1);
1538 rb_define_method(rb_cRational, "numerator", nurat_numerator, 0);
1539 rb_define_method(rb_cRational, "denominator", nurat_denominator, 0);
1541 rb_define_method(rb_cRational, "+", nurat_add, 1);
1542 rb_define_method(rb_cRational, "-", nurat_sub, 1);
1543 rb_define_method(rb_cRational, "*", nurat_mul, 1);
1544 rb_define_method(rb_cRational, "/", nurat_div, 1);
1545 rb_define_method(rb_cRational, "quo", nurat_div, 1);
1546 rb_define_method(rb_cRational, "fdiv", nurat_fdiv, 1);
1547 rb_define_method(rb_cRational, "**", nurat_expt, 1);
1549 rb_define_method(rb_cRational, "<=>", nurat_cmp, 1);
1550 rb_define_method(rb_cRational, "==", nurat_equal_p, 1);
1551 rb_define_method(rb_cRational, "coerce", nurat_coerce, 1);
1553 rb_define_method(rb_cRational, "div", nurat_idiv, 1);
1554 #if NUBY
1555 rb_define_method(rb_cRational, "//", nurat_idiv, 1);
1556 #endif
1557 rb_define_method(rb_cRational, "modulo", nurat_mod, 1);
1558 rb_define_method(rb_cRational, "%", nurat_mod, 1);
1559 rb_define_method(rb_cRational, "divmod", nurat_divmod, 1);
1561 #if 0
1562 rb_define_method(rb_cRational, "quot", nurat_quot, 1);
1563 #endif
1564 rb_define_method(rb_cRational, "remainder", nurat_rem, 1);
1565 #if 0
1566 rb_define_method(rb_cRational, "quotrem", nurat_quotrem, 1);
1567 #endif
1569 rb_define_method(rb_cRational, "abs", nurat_abs, 0);
1571 #if 0
1572 rb_define_method(rb_cRational, "rational?", nurat_true, 0);
1573 rb_define_method(rb_cRational, "exact?", nurat_true, 0);
1574 #endif
1576 rb_define_method(rb_cRational, "floor", nurat_floor, 0);
1577 rb_define_method(rb_cRational, "ceil", nurat_ceil, 0);
1578 rb_define_method(rb_cRational, "truncate", nurat_truncate, 0);
1579 rb_define_method(rb_cRational, "round", nurat_round, 0);
1581 rb_define_method(rb_cRational, "to_i", nurat_truncate, 0);
1582 rb_define_method(rb_cRational, "to_f", nurat_to_f, 0);
1583 rb_define_method(rb_cRational, "to_r", nurat_to_r, 0);
1585 rb_define_method(rb_cRational, "hash", nurat_hash, 0);
1587 rb_define_method(rb_cRational, "to_s", nurat_to_s, 0);
1588 rb_define_method(rb_cRational, "inspect", nurat_inspect, 0);
1590 rb_define_method(rb_cRational, "marshal_dump", nurat_marshal_dump, 0);
1591 rb_define_method(rb_cRational, "marshal_load", nurat_marshal_load, 1);
1593 /* --- */
1595 rb_define_method(rb_cInteger, "gcd", rb_gcd, 1);
1596 rb_define_method(rb_cInteger, "lcm", rb_lcm, 1);
1597 rb_define_method(rb_cInteger, "gcdlcm", rb_gcdlcm, 1);
1599 rb_define_method(rb_cNilClass, "to_r", nilclass_to_r, 0);
1600 rb_define_method(rb_cInteger, "to_r", integer_to_r, 0);
1601 rb_define_method(rb_cFloat, "to_r", float_to_r, 0);
1603 make_patterns();
1605 rb_define_method(rb_cString, "to_r", string_to_r, 0);
1607 rb_define_singleton_method(rb_cRational, "convert", nurat_s_convert, -1);
1608 rb_funcall(rb_cRational, rb_intern("private_class_method"), 1,
1609 ID2SYM(rb_intern("convert")));
1611 rb_include_module(rb_cRational, rb_mPrecision);
1612 rb_define_singleton_method(rb_cRational, "induced_from",
1613 nurat_s_induced_from, 1);