add a test for IA64 Debian GNU/Linux Etch.
[ruby-svn.git] / rational.c
bloba81ffe16114fe6f69ae3470b32e41835e4712773
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 switch (rb_scan_args(argc, argv, "11", &num, &den)) {
504 case 1:
505 den = ONE;
506 break;
509 nurat_int_check(num);
510 nurat_int_check(den);
512 return nurat_s_canonicalize_internal(klass, num, den);
514 #endif
516 static VALUE
517 nurat_s_new(int argc, VALUE *argv, VALUE klass)
519 VALUE num, den;
521 switch (rb_scan_args(argc, argv, "11", &num, &den)) {
522 case 1:
523 den = ONE;
524 break;
527 nurat_int_check(num);
528 nurat_int_check(den);
530 return nurat_s_canonicalize_internal(klass, num, den);
533 inline static VALUE
534 f_rational_new1(VALUE klass, VALUE x)
536 assert(!k_rational_p(x));
537 return nurat_s_canonicalize_internal(klass, x, ONE);
540 inline static VALUE
541 f_rational_new2(VALUE klass, VALUE x, VALUE y)
543 assert(!k_rational_p(x));
544 assert(!k_rational_p(y));
545 return nurat_s_canonicalize_internal(klass, x, y);
548 inline static VALUE
549 f_rational_new_no_reduce1(VALUE klass, VALUE x)
551 assert(!k_rational_p(x));
552 return nurat_s_canonicalize_internal_no_reduce(klass, x, ONE);
555 inline static VALUE
556 f_rational_new_no_reduce2(VALUE klass, VALUE x, VALUE y)
558 assert(!k_rational_p(x));
559 assert(!k_rational_p(y));
560 return nurat_s_canonicalize_internal_no_reduce(klass, x, y);
563 static VALUE
564 nurat_f_rational(int argc, VALUE *argv, VALUE klass)
566 return rb_funcall2(rb_cRational, id_convert, argc, argv);
569 static VALUE
570 nurat_numerator(VALUE self)
572 get_dat1(self);
573 return dat->num;
576 static VALUE
577 nurat_denominator(VALUE self)
579 get_dat1(self);
580 return dat->den;
583 #ifndef NDEBUG
584 #define f_imul f_imul_orig
585 #endif
587 inline static VALUE
588 f_imul(long a, long b)
590 VALUE r;
591 long c;
593 if (a == 0 || b == 0)
594 return ZERO;
595 else if (a == 1)
596 return LONG2NUM(b);
597 else if (b == 1)
598 return LONG2NUM(a);
600 c = a * b;
601 r = LONG2NUM(c);
602 if (NUM2LONG(r) != c || (c / a) != b)
603 r = rb_big_mul(rb_int2big(a), rb_int2big(b));
604 return r;
607 #ifndef NDEBUG
608 #undef f_imul
610 inline static VALUE
611 f_imul(long x, long y)
613 VALUE r = f_imul_orig(x, y);
614 assert(f_equal_p(r, f_mul(LONG2NUM(x), LONG2NUM(y))));
615 return r;
617 #endif
619 inline static VALUE
620 f_addsub(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
622 VALUE num, den;
624 if (FIXNUM_P(anum) && FIXNUM_P(aden) &&
625 FIXNUM_P(bnum) && FIXNUM_P(bden)) {
626 long an = FIX2LONG(anum);
627 long ad = FIX2LONG(aden);
628 long bn = FIX2LONG(bnum);
629 long bd = FIX2LONG(bden);
630 long ig = i_gcd(ad, bd);
632 VALUE g = LONG2NUM(ig);
633 VALUE a = f_imul(an, bd / ig);
634 VALUE b = f_imul(bn, ad / ig);
635 VALUE c;
637 if (k == '+')
638 c = f_add(a, b);
639 else
640 c = f_sub(a, b);
642 b = f_idiv(aden, g);
643 g = f_gcd(c, g);
644 num = f_idiv(c, g);
645 a = f_idiv(bden, g);
646 den = f_mul(a, b);
648 else {
649 VALUE g = f_gcd(aden, bden);
650 VALUE a = f_mul(anum, f_idiv(bden, g));
651 VALUE b = f_mul(bnum, f_idiv(aden, g));
652 VALUE c;
654 if (k == '+')
655 c = f_add(a, b);
656 else
657 c = f_sub(a, b);
659 b = f_idiv(aden, g);
660 g = f_gcd(c, g);
661 num = f_idiv(c, g);
662 a = f_idiv(bden, g);
663 den = f_mul(a, b);
665 return f_rational_new_no_reduce2(CLASS_OF(self), num, den);
668 static VALUE
669 nurat_add(VALUE self, VALUE other)
671 switch (TYPE(other)) {
672 case T_FIXNUM:
673 case T_BIGNUM:
675 get_dat1(self);
677 return f_addsub(self,
678 dat->num, dat->den,
679 other, ONE, '+');
681 case T_FLOAT:
682 return f_add(f_to_f(self), other);
683 case T_RATIONAL:
685 get_dat2(self, other);
687 return f_addsub(self,
688 adat->num, adat->den,
689 bdat->num, bdat->den, '+');
691 default:
692 return rb_num_coerce_bin(self, other, '+');
696 static VALUE
697 nurat_sub(VALUE self, VALUE other)
699 switch (TYPE(other)) {
700 case T_FIXNUM:
701 case T_BIGNUM:
703 get_dat1(self);
705 return f_addsub(self,
706 dat->num, dat->den,
707 other, ONE, '-');
709 case T_FLOAT:
710 return f_sub(f_to_f(self), other);
711 case T_RATIONAL:
713 get_dat2(self, other);
715 return f_addsub(self,
716 adat->num, adat->den,
717 bdat->num, bdat->den, '-');
719 default:
720 return rb_num_coerce_bin(self, other, '-');
724 inline static VALUE
725 f_muldiv(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
727 VALUE num, den;
729 if (k == '/') {
730 VALUE t;
732 if (f_negative_p(bnum)) {
733 anum = f_negate(anum);
734 bnum = f_negate(bnum);
736 t = bnum;
737 bnum = bden;
738 bden = t;
741 if (FIXNUM_P(anum) && FIXNUM_P(aden) &&
742 FIXNUM_P(bnum) && FIXNUM_P(bden)) {
743 long an = FIX2LONG(anum);
744 long ad = FIX2LONG(aden);
745 long bn = FIX2LONG(bnum);
746 long bd = FIX2LONG(bden);
747 long g1 = i_gcd(an, bd);
748 long g2 = i_gcd(ad, bn);
750 num = f_imul(an / g1, bn / g2);
751 den = f_imul(ad / g2, bd / g1);
753 else {
754 VALUE g1 = f_gcd(anum, bden);
755 VALUE g2 = f_gcd(aden, bnum);
757 num = f_mul(f_idiv(anum, g1), f_idiv(bnum, g2));
758 den = f_mul(f_idiv(aden, g2), f_idiv(bden, g1));
760 return f_rational_new_no_reduce2(CLASS_OF(self), num, den);
763 static VALUE
764 nurat_mul(VALUE self, VALUE other)
766 switch (TYPE(other)) {
767 case T_FIXNUM:
768 case T_BIGNUM:
770 get_dat1(self);
772 return f_muldiv(self,
773 dat->num, dat->den,
774 other, ONE, '*');
776 case T_FLOAT:
777 return f_mul(f_to_f(self), other);
778 case T_RATIONAL:
780 get_dat2(self, other);
782 return f_muldiv(self,
783 adat->num, adat->den,
784 bdat->num, bdat->den, '*');
786 default:
787 return rb_num_coerce_bin(self, other, '*');
791 static VALUE
792 nurat_div(VALUE self, VALUE other)
794 switch (TYPE(other)) {
795 case T_FIXNUM:
796 case T_BIGNUM:
797 if (f_zero_p(other))
798 rb_raise(rb_eZeroDivError, "devided by zero");
800 get_dat1(self);
802 return f_muldiv(self,
803 dat->num, dat->den,
804 other, ONE, '/');
806 case T_FLOAT:
807 return rb_funcall(f_to_f(self), '/', 1, other);
808 case T_RATIONAL:
809 if (f_zero_p(other))
810 rb_raise(rb_eZeroDivError, "devided by zero");
812 get_dat2(self, other);
814 return f_muldiv(self,
815 adat->num, adat->den,
816 bdat->num, bdat->den, '/');
818 default:
819 return rb_num_coerce_bin(self, other, '/');
823 static VALUE
824 nurat_fdiv(VALUE self, VALUE other)
826 return f_div(f_to_f(self), other);
829 static VALUE
830 nurat_expt(VALUE self, VALUE other)
832 if (f_zero_p(other))
833 return f_rational_new_bang1(CLASS_OF(self), ONE);
835 if (k_rational_p(other)) {
836 get_dat1(other);
838 if (f_one_p(dat->den))
839 other = dat->num; /* good? */
842 switch (TYPE(other)) {
843 case T_FIXNUM:
844 case T_BIGNUM:
846 VALUE num, den;
848 get_dat1(self);
850 switch (FIX2INT(f_cmp(other, ZERO))) {
851 case 1:
852 num = f_expt(dat->num, other);
853 den = f_expt(dat->den, other);
854 break;
855 case -1:
856 num = f_expt(dat->den, f_negate(other));
857 den = f_expt(dat->num, f_negate(other));
858 break;
859 default:
860 num = ONE;
861 den = ONE;
862 break;
864 return f_rational_new2(CLASS_OF(self), num, den);
866 case T_FLOAT:
867 case T_RATIONAL:
868 return f_expt(f_to_f(self), other);
869 default:
870 return rb_num_coerce_bin(self, other, id_expt);
874 static VALUE
875 nurat_cmp(VALUE self, VALUE other)
877 switch (TYPE(other)) {
878 case T_FIXNUM:
879 case T_BIGNUM:
881 get_dat1(self);
883 if (FIXNUM_P(dat->den) && FIX2LONG(dat->den) == 1)
884 return f_cmp(dat->num, other);
885 else
886 return f_cmp(self, f_rational_new_bang1(CLASS_OF(self), other));
888 case T_FLOAT:
889 return f_cmp(f_to_f(self), other);
890 case T_RATIONAL:
892 VALUE num1, num2;
894 get_dat2(self, other);
896 if (FIXNUM_P(adat->num) && FIXNUM_P(adat->den) &&
897 FIXNUM_P(bdat->num) && FIXNUM_P(bdat->den)) {
898 num1 = f_imul(FIX2LONG(adat->num), FIX2LONG(bdat->den));
899 num2 = f_imul(FIX2LONG(bdat->num), FIX2LONG(adat->den));
901 else {
902 num1 = f_mul(adat->num, bdat->den);
903 num2 = f_mul(bdat->num, adat->den);
905 return f_cmp(f_sub(num1, num2), ZERO);
907 default:
908 return rb_num_coerce_bin(self, other, id_cmp);
912 static VALUE
913 nurat_equal_p(VALUE self, VALUE other)
915 switch (TYPE(other)) {
916 case T_FIXNUM:
917 case T_BIGNUM:
919 get_dat1(self);
921 if (!FIXNUM_P(dat->den))
922 return Qfalse;
923 if (FIX2LONG(dat->den) != 1)
924 return Qfalse;
925 if (f_equal_p(dat->num, other))
926 return Qtrue;
927 else
928 return Qfalse;
930 case T_FLOAT:
931 return f_equal_p(f_to_f(self), other);
932 case T_RATIONAL:
934 get_dat2(self, other);
936 return f_boolcast(f_equal_p(adat->num, bdat->num) &&
937 f_equal_p(adat->den, bdat->den));
939 default:
940 return f_equal_p(other, self);
944 static VALUE
945 nurat_coerce(VALUE self, VALUE other)
947 switch (TYPE(other)) {
948 case T_FIXNUM:
949 case T_BIGNUM:
950 return rb_assoc_new(f_rational_new_bang1(CLASS_OF(self), other), self);
951 case T_FLOAT:
952 return rb_assoc_new(other, f_to_f(self));
955 rb_raise(rb_eTypeError, "%s can't be coerced into %s",
956 rb_obj_classname(other), rb_obj_classname(self));
957 return Qnil;
960 static VALUE
961 nurat_idiv(VALUE self, VALUE other)
963 return f_floor(f_div(self, other));
966 static VALUE
967 nurat_mod(VALUE self, VALUE other)
969 VALUE val = f_floor(f_div(self, other));
970 return f_sub(self, f_mul(other, val));
973 static VALUE
974 nurat_divmod(VALUE self, VALUE other)
976 VALUE val = f_floor(f_div(self, other));
977 return rb_assoc_new(val, f_sub(self, f_mul(other, val)));
980 #if 0
981 static VALUE
982 nurat_quot(VALUE self, VALUE other)
984 return f_truncate(f_div(self, other));
986 #endif
988 static VALUE
989 nurat_rem(VALUE self, VALUE other)
991 VALUE val = f_truncate(f_div(self, other));
992 return f_sub(self, f_mul(other, val));
995 #if 0
996 static VALUE
997 nurat_quotrem(VALUE self, VALUE other)
999 VALUE val = f_truncate(f_div(self, other));
1000 return rb_assoc_new(val, f_sub(self, f_mul(other, val)));
1002 #endif
1004 static VALUE
1005 nurat_abs(VALUE self)
1007 if (!f_negative_p(self))
1008 return self;
1009 else
1010 return f_negate(self);
1013 #if 0
1014 static VALUE
1015 nurat_true(VALUE self)
1017 return Qtrue;
1019 #endif
1021 static VALUE
1022 nurat_floor(VALUE self)
1024 get_dat1(self);
1025 return f_idiv(dat->num, dat->den);
1028 static VALUE
1029 nurat_ceil(VALUE self)
1031 get_dat1(self);
1032 return f_negate(f_idiv(f_negate(dat->num), dat->den));
1035 static VALUE
1036 nurat_truncate(VALUE self)
1038 get_dat1(self);
1039 if (f_negative_p(dat->num))
1040 return f_negate(f_idiv(f_negate(dat->num), dat->den));
1041 return f_idiv(dat->num, dat->den);
1044 static VALUE
1045 nurat_round(VALUE self)
1047 get_dat1(self);
1049 if (f_negative_p(dat->num)) {
1050 VALUE num, den;
1052 num = f_negate(dat->num);
1053 num = f_add(f_mul(num, TWO), dat->den);
1054 den = f_mul(dat->den, TWO);
1055 return f_negate(f_idiv(num, den));
1057 else {
1058 VALUE num = f_add(f_mul(dat->num, TWO), dat->den);
1059 VALUE den = f_mul(dat->den, TWO);
1060 return f_idiv(num, den);
1064 #define f_size(x) rb_funcall(x, rb_intern("size"), 0)
1065 #define f_rshift(x,y) rb_funcall(x, rb_intern(">>"), 1, y)
1067 inline static long
1068 i_ilog2(VALUE x)
1070 long q, r, fx;
1072 assert(!f_lt_p(x, ONE));
1074 q = (NUM2LONG(f_size(x)) - sizeof(long)) * 8 + 1;
1076 if (q > 0)
1077 x = f_rshift(x, LONG2NUM(q));
1079 fx = NUM2LONG(x);
1081 r = -1;
1082 while (fx) {
1083 fx >>= 1;
1084 r += 1;
1087 return q + r;
1090 static VALUE
1091 nurat_to_f(VALUE self)
1093 VALUE num, den;
1094 int minus = 0;
1095 long nl, dl, ml, ne, de;
1096 int e;
1097 double f;
1100 get_dat1(self);
1102 if (f_zero_p(dat->num))
1103 return rb_float_new(0.0);
1105 num = dat->num;
1106 den = dat->den;
1109 if (f_negative_p(num)) {
1110 num = f_negate(num);
1111 minus = 1;
1114 nl = i_ilog2(num);
1115 dl = i_ilog2(den);
1116 ml = (long)(log(DBL_MAX) / log(2.0) - 1); /* should be a static */
1118 ne = 0;
1119 if (nl > ml) {
1120 ne = nl - ml;
1121 num = f_rshift(num, LONG2NUM(ne));
1124 de = 0;
1125 if (dl > ml) {
1126 de = dl - ml;
1127 den = f_rshift(den, LONG2NUM(de));
1130 e = (int)(ne - de);
1132 if ((e > DBL_MAX_EXP) || (e < DBL_MIN_EXP)) {
1133 rb_warning("%s out of Float range", rb_obj_classname(self));
1134 return rb_float_new(e > 0 ? HUGE_VAL : 0.0);
1137 f = NUM2DBL(num) / NUM2DBL(den);
1138 if (minus)
1139 f = -f;
1140 f = ldexp(f, e);
1142 if (isinf(f) || isnan(f))
1143 rb_warning("%s out of Float range", rb_obj_classname(self));
1145 return rb_float_new(f);
1148 static VALUE
1149 nurat_to_r(VALUE self)
1151 return self;
1154 static VALUE
1155 nurat_hash(VALUE self)
1157 get_dat1(self);
1158 return f_xor(dat->num, dat->den);
1161 static VALUE
1162 nurat_to_s(VALUE self)
1164 get_dat1(self);
1166 if (f_one_p(dat->den))
1167 return f_to_s(dat->num);
1168 else
1169 return rb_funcall(rb_mKernel, id_format, 3,
1170 rb_str_new2("%d/%d"), dat->num, dat->den);
1173 static VALUE
1174 nurat_inspect(VALUE self)
1176 get_dat1(self);
1177 return rb_funcall(rb_mKernel, id_format, 3,
1178 rb_str_new2("Rational(%d, %d)"), dat->num, dat->den);
1181 static VALUE
1182 nurat_marshal_dump(VALUE self)
1184 get_dat1(self);
1185 return rb_assoc_new(dat->num, dat->den);
1188 static VALUE
1189 nurat_marshal_load(VALUE self, VALUE a)
1191 get_dat1(self);
1192 dat->num = RARRAY_PTR(a)[0];
1193 dat->den = RARRAY_PTR(a)[1];
1195 if (f_zero_p(dat->den))
1196 rb_raise(rb_eZeroDivError, "devided by zero");
1198 return self;
1201 /* --- */
1203 VALUE
1204 rb_gcd(VALUE self, VALUE other)
1206 nurat_int_check(other);
1207 return f_gcd(self, other);
1210 VALUE
1211 rb_lcm(VALUE self, VALUE other)
1213 nurat_int_check(other);
1214 return f_lcm(self, other);
1217 VALUE
1218 rb_gcdlcm(VALUE self, VALUE other)
1220 nurat_int_check(other);
1221 return rb_assoc_new(f_gcd(self, other), f_lcm(self, other));
1224 VALUE
1225 rb_rational_raw(VALUE x, VALUE y)
1227 return nurat_s_new_internal(rb_cRational, x, y);
1230 VALUE
1231 rb_rational_new(VALUE x, VALUE y)
1233 return nurat_s_canonicalize_internal(rb_cRational, x, y);
1236 static VALUE nurat_s_convert(int argc, VALUE *argv, VALUE klass);
1238 VALUE
1239 rb_Rational(VALUE x, VALUE y)
1241 VALUE a[2];
1242 a[0] = x;
1243 a[1] = y;
1244 return nurat_s_convert(2, a, rb_cRational);
1247 static VALUE
1248 nilclass_to_r(VALUE self)
1250 return rb_rational_new1(INT2FIX(0));
1253 static VALUE
1254 integer_to_r(VALUE self)
1256 return rb_rational_new1(self);
1259 static VALUE
1260 float_decode(VALUE self)
1262 double f;
1263 int n;
1265 f = frexp(RFLOAT_VALUE(self), &n);
1266 f = ldexp(f, DBL_MANT_DIG);
1267 n -= DBL_MANT_DIG;
1268 return rb_assoc_new(f_to_i(rb_float_new(f)), INT2FIX(n));
1271 static VALUE
1272 float_to_r(VALUE self)
1274 VALUE a = float_decode(self);
1275 return f_mul(RARRAY_PTR(a)[0],
1276 f_expt(INT2FIX(FLT_RADIX), RARRAY_PTR(a)[1]));
1279 static VALUE rat_pat, an_e_pat, a_dot_pat, underscores_pat, an_underscore;
1281 #define DIGITS "(?:\\d(?:_\\d|\\d)*)"
1282 #define NUMERATOR "(?:" DIGITS "?\\.)?" DIGITS "(?:[eE][-+]?" DIGITS ")?"
1283 #define DENOMINATOR "[-+]?" DIGITS
1284 #define PATTERN "\\A([-+])?(" NUMERATOR ")(?:\\/(" DENOMINATOR "))?"
1286 static void
1287 make_patterns(void)
1289 static char rat_pat_source[] = PATTERN;
1290 static char an_e_pat_source[] = "[eE]";
1291 static char a_dot_pat_source[] = "\\.";
1292 static char underscores_pat_source[] = "_+";
1294 rat_pat = rb_reg_new(rat_pat_source, sizeof rat_pat_source - 1, 0);
1295 rb_global_variable(&rat_pat);
1297 an_e_pat = rb_reg_new(an_e_pat_source, sizeof an_e_pat_source - 1, 0);
1298 rb_global_variable(&an_e_pat);
1300 a_dot_pat = rb_reg_new(a_dot_pat_source, sizeof a_dot_pat_source - 1, 0);
1301 rb_global_variable(&a_dot_pat);
1303 underscores_pat = rb_reg_new(underscores_pat_source,
1304 sizeof underscores_pat_source - 1, 0);
1305 rb_global_variable(&underscores_pat);
1307 an_underscore = rb_str_new2("_");
1308 rb_global_variable(&an_underscore);
1311 #define id_strip rb_intern("strip")
1312 #define f_strip(x) rb_funcall(x, id_strip, 0)
1314 #define id_match rb_intern("match")
1315 #define f_match(x,y) rb_funcall(x, id_match, 1, y)
1317 #define id_aref rb_intern("[]")
1318 #define f_aref(x,y) rb_funcall(x, id_aref, 1, y)
1320 #define id_post_match rb_intern("post_match")
1321 #define f_post_match(x) rb_funcall(x, id_post_match, 0)
1323 #define id_split rb_intern("split")
1324 #define f_split(x,y) rb_funcall(x, id_split, 1, y)
1326 #include <ctype.h>
1328 static VALUE
1329 string_to_r_internal(VALUE self)
1331 VALUE s, m;
1333 s = f_strip(self);
1335 if (RSTRING_LEN(s) == 0)
1336 return rb_assoc_new(Qnil, self);
1338 m = f_match(rat_pat, s);
1340 if (!NIL_P(m)) {
1341 VALUE v, ifp, exp, ip, fp;
1342 VALUE si = f_aref(m, INT2FIX(1));
1343 VALUE nu = f_aref(m, INT2FIX(2));
1344 VALUE de = f_aref(m, INT2FIX(3));
1345 VALUE re = f_post_match(m);
1348 VALUE a;
1350 a = f_split(nu, an_e_pat);
1351 ifp = RARRAY_PTR(a)[0];
1352 if (RARRAY_LEN(a) != 2)
1353 exp = Qnil;
1354 else
1355 exp = RARRAY_PTR(a)[1];
1357 a = f_split(ifp, a_dot_pat);
1358 ip = RARRAY_PTR(a)[0];
1359 if (RARRAY_LEN(a) != 2)
1360 fp = Qnil;
1361 else
1362 fp = RARRAY_PTR(a)[1];
1365 v = rb_rational_new1(f_to_i(ip));
1367 if (!NIL_P(fp)) {
1368 char *p = StringValuePtr(fp);
1369 long count = 0;
1370 VALUE l;
1372 while (*p) {
1373 if (isdigit(*p))
1374 count++;
1375 p++;
1378 l = f_expt(INT2FIX(10), LONG2NUM(count));
1379 v = f_mul(v, l);
1380 v = f_add(v, f_to_i(fp));
1381 v = f_div(v, l);
1383 if (!NIL_P(exp))
1384 v = f_mul(v, f_expt(INT2FIX(10), f_to_i(exp)));
1385 if (!NIL_P(si) && *StringValuePtr(si) == '-')
1386 v = f_negate(v);
1387 if (!NIL_P(de))
1388 v = f_div(v, f_to_i(de));
1390 return rb_assoc_new(v, re);
1392 return rb_assoc_new(Qnil, self);
1395 static VALUE
1396 string_to_r_strict(VALUE self)
1398 VALUE a = string_to_r_internal(self);
1399 if (NIL_P(RARRAY_PTR(a)[0]) || RSTRING_LEN(RARRAY_PTR(a)[1]) > 0) {
1400 VALUE s = f_inspect(self);
1401 rb_raise(rb_eArgError, "invalid value for Rational: %s",
1402 StringValuePtr(s));
1404 return RARRAY_PTR(a)[0];
1407 #define id_gsub rb_intern("gsub")
1408 #define f_gsub(x,y,z) rb_funcall(x, id_gsub, 2, y, z)
1410 static VALUE
1411 string_to_r(VALUE self)
1413 VALUE s = f_gsub(self, underscores_pat, an_underscore);
1414 VALUE a = string_to_r_internal(s);
1415 if (!NIL_P(RARRAY_PTR(a)[0]))
1416 return RARRAY_PTR(a)[0];
1417 return rb_rational_new1(INT2FIX(0));
1420 #define id_to_r rb_intern("to_r")
1421 #define f_to_r(x) rb_funcall(x, id_to_r, 0)
1423 static VALUE
1424 nurat_s_convert(int argc, VALUE *argv, VALUE klass)
1426 VALUE a1, a2;
1428 a1 = Qnil;
1429 a2 = Qnil;
1430 rb_scan_args(argc, argv, "02", &a1, &a2);
1432 switch (TYPE(a1)) {
1433 case T_COMPLEX:
1434 if (k_float_p(RCOMPLEX(a1)->image) || !f_zero_p(RCOMPLEX(a1)->image)) {
1435 VALUE s = f_to_s(a1);
1436 rb_raise(rb_eRangeError, "can't accept %s",
1437 StringValuePtr(s));
1439 a1 = RCOMPLEX(a1)->real;
1442 switch (TYPE(a2)) {
1443 case T_COMPLEX:
1444 if (k_float_p(RCOMPLEX(a2)->image) || !f_zero_p(RCOMPLEX(a2)->image)) {
1445 VALUE s = f_to_s(a2);
1446 rb_raise(rb_eRangeError, "can't accept %s",
1447 StringValuePtr(s));
1449 a2 = RCOMPLEX(a2)->real;
1452 switch (TYPE(a1)) {
1453 case T_FIXNUM:
1454 case T_BIGNUM:
1455 break;
1456 case T_FLOAT:
1457 a1 = f_to_r(a1);
1458 break;
1459 case T_STRING:
1460 a1 = string_to_r_strict(a1);
1461 break;
1464 switch (TYPE(a2)) {
1465 case T_FIXNUM:
1466 case T_BIGNUM:
1467 break;
1468 case T_FLOAT:
1469 a2 = f_to_r(a2);
1470 break;
1471 case T_STRING:
1472 a2 = string_to_r_strict(a2);
1473 break;
1476 switch (TYPE(a1)) {
1477 case T_RATIONAL:
1478 if (NIL_P(a2) || f_zero_p(a2))
1479 return a1;
1480 else
1481 return f_div(a1, a2);
1484 switch (TYPE(a2)) {
1485 case T_RATIONAL:
1486 return f_div(a1, a2);
1490 VALUE argv2[2];
1491 argv2[0] = a1;
1492 argv2[1] = a2;
1493 return nurat_s_new(argc, argv2, klass);
1497 static VALUE
1498 nurat_s_induced_from(VALUE klass, VALUE n)
1500 return f_to_r(n);
1503 void
1504 Init_Rational(void)
1506 assert(fprintf(stderr, "assert() is now active\n"));
1508 id_Unify = rb_intern("Unify");
1509 id_abs = rb_intern("abs");
1510 id_cmp = rb_intern("<=>");
1511 id_convert = rb_intern("convert");
1512 id_equal_p = rb_intern("==");
1513 id_expt = rb_intern("**");
1514 id_floor = rb_intern("floor");
1515 id_format = rb_intern("format");
1516 id_idiv = rb_intern("div");
1517 id_inspect = rb_intern("inspect");
1518 id_negate = rb_intern("-@");
1519 id_new = rb_intern("new");
1520 id_new_bang = rb_intern("new!");
1521 id_to_f = rb_intern("to_f");
1522 id_to_i = rb_intern("to_i");
1523 id_to_s = rb_intern("to_s");
1524 id_truncate = rb_intern("truncate");
1526 rb_cRational = rb_define_class(RATIONAL_NAME, rb_cNumeric);
1528 rb_define_alloc_func(rb_cRational, nurat_s_alloc);
1529 rb_funcall(rb_cRational, rb_intern("private_class_method"), 1,
1530 ID2SYM(rb_intern("allocate")));
1532 rb_define_singleton_method(rb_cRational, "new!", nurat_s_new_bang, -1);
1533 rb_funcall(rb_cRational, rb_intern("private_class_method"), 1,
1534 ID2SYM(rb_intern("new!")));
1536 rb_define_singleton_method(rb_cRational, "new", nurat_s_new, -1);
1537 rb_funcall(rb_cRational, rb_intern("private_class_method"), 1,
1538 ID2SYM(rb_intern("new")));
1540 rb_define_global_function(RATIONAL_NAME, nurat_f_rational, -1);
1542 rb_define_method(rb_cRational, "numerator", nurat_numerator, 0);
1543 rb_define_method(rb_cRational, "denominator", nurat_denominator, 0);
1545 rb_define_method(rb_cRational, "+", nurat_add, 1);
1546 rb_define_method(rb_cRational, "-", nurat_sub, 1);
1547 rb_define_method(rb_cRational, "*", nurat_mul, 1);
1548 rb_define_method(rb_cRational, "/", nurat_div, 1);
1549 rb_define_method(rb_cRational, "quo", nurat_div, 1);
1550 rb_define_method(rb_cRational, "fdiv", nurat_fdiv, 1);
1551 rb_define_method(rb_cRational, "**", nurat_expt, 1);
1553 rb_define_method(rb_cRational, "<=>", nurat_cmp, 1);
1554 rb_define_method(rb_cRational, "==", nurat_equal_p, 1);
1555 rb_define_method(rb_cRational, "coerce", nurat_coerce, 1);
1557 rb_define_method(rb_cRational, "div", nurat_idiv, 1);
1558 #if NUBY
1559 rb_define_method(rb_cRational, "//", nurat_idiv, 1);
1560 #endif
1561 rb_define_method(rb_cRational, "modulo", nurat_mod, 1);
1562 rb_define_method(rb_cRational, "%", nurat_mod, 1);
1563 rb_define_method(rb_cRational, "divmod", nurat_divmod, 1);
1565 #if 0
1566 rb_define_method(rb_cRational, "quot", nurat_quot, 1);
1567 #endif
1568 rb_define_method(rb_cRational, "remainder", nurat_rem, 1);
1569 #if 0
1570 rb_define_method(rb_cRational, "quotrem", nurat_quotrem, 1);
1571 #endif
1573 rb_define_method(rb_cRational, "abs", nurat_abs, 0);
1575 #if 0
1576 rb_define_method(rb_cRational, "rational?", nurat_true, 0);
1577 rb_define_method(rb_cRational, "exact?", nurat_true, 0);
1578 #endif
1580 rb_define_method(rb_cRational, "floor", nurat_floor, 0);
1581 rb_define_method(rb_cRational, "ceil", nurat_ceil, 0);
1582 rb_define_method(rb_cRational, "truncate", nurat_truncate, 0);
1583 rb_define_method(rb_cRational, "round", nurat_round, 0);
1585 rb_define_method(rb_cRational, "to_i", nurat_truncate, 0);
1586 rb_define_method(rb_cRational, "to_f", nurat_to_f, 0);
1587 rb_define_method(rb_cRational, "to_r", nurat_to_r, 0);
1589 rb_define_method(rb_cRational, "hash", nurat_hash, 0);
1591 rb_define_method(rb_cRational, "to_s", nurat_to_s, 0);
1592 rb_define_method(rb_cRational, "inspect", nurat_inspect, 0);
1594 rb_define_method(rb_cRational, "marshal_dump", nurat_marshal_dump, 0);
1595 rb_define_method(rb_cRational, "marshal_load", nurat_marshal_load, 1);
1597 /* --- */
1599 rb_define_method(rb_cInteger, "gcd", rb_gcd, 1);
1600 rb_define_method(rb_cInteger, "lcm", rb_lcm, 1);
1601 rb_define_method(rb_cInteger, "gcdlcm", rb_gcdlcm, 1);
1603 rb_define_method(rb_cNilClass, "to_r", nilclass_to_r, 0);
1604 rb_define_method(rb_cInteger, "to_r", integer_to_r, 0);
1605 rb_define_method(rb_cFloat, "to_r", float_to_r, 0);
1607 make_patterns();
1609 rb_define_method(rb_cString, "to_r", string_to_r, 0);
1611 rb_define_singleton_method(rb_cRational, "convert", nurat_s_convert, -1);
1612 rb_funcall(rb_cRational, rb_intern("private_class_method"), 1,
1613 ID2SYM(rb_intern("convert")));
1615 rb_include_module(rb_cRational, rb_mPrecision);
1616 rb_define_singleton_method(rb_cRational, "induced_from",
1617 nurat_s_induced_from, 1);