forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rational.c
2847 lines (2498 loc) · 65.4 KB
/
rational.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
rational.c: Coded by Tadayoshi Funaba 2008-2012
This implementation is based on Keiju Ishitsuka's Rational library
which is written in ruby.
*/
#include "ruby/internal/config.h"
#include <ctype.h>
#include <float.h>
#include <math.h>
#ifdef HAVE_IEEEFP_H
#include <ieeefp.h>
#endif
#if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
#define USE_GMP
#include <gmp.h>
#endif
#include "id.h"
#include "internal.h"
#include "internal/array.h"
#include "internal/complex.h"
#include "internal/gc.h"
#include "internal/numeric.h"
#include "internal/object.h"
#include "internal/rational.h"
#include "ruby_assert.h"
#define ZERO INT2FIX(0)
#define ONE INT2FIX(1)
#define TWO INT2FIX(2)
#define GMP_GCD_DIGITS 1
#define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x))
VALUE rb_cRational;
static ID id_abs, id_integer_p,
id_i_num, id_i_den;
#define id_idiv idDiv
#define id_to_i idTo_i
#define f_inspect rb_inspect
#define f_to_s rb_obj_as_string
static VALUE nurat_to_f(VALUE self);
static VALUE float_to_r(VALUE self);
inline static VALUE
f_add(VALUE x, VALUE y)
{
if (FIXNUM_ZERO_P(y))
return x;
if (FIXNUM_ZERO_P(x))
return y;
if (RB_INTEGER_TYPE_P(x))
return rb_int_plus(x, y);
return rb_funcall(x, '+', 1, y);
}
inline static VALUE
f_div(VALUE x, VALUE y)
{
if (y == ONE)
return x;
if (RB_INTEGER_TYPE_P(x))
return rb_int_div(x, y);
return rb_funcall(x, '/', 1, y);
}
inline static int
f_lt_p(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y))
return (SIGNED_VALUE)x < (SIGNED_VALUE)y;
if (RB_INTEGER_TYPE_P(x)) {
VALUE r = rb_int_cmp(x, y);
if (!NIL_P(r)) return rb_int_negative_p(r);
}
return RTEST(rb_funcall(x, '<', 1, y));
}
#ifndef NDEBUG
/* f_mod is used only in f_gcd defined when NDEBUG is not defined */
inline static VALUE
f_mod(VALUE x, VALUE y)
{
if (RB_INTEGER_TYPE_P(x))
return rb_int_modulo(x, y);
return rb_funcall(x, '%', 1, y);
}
#endif
inline static VALUE
f_mul(VALUE x, VALUE y)
{
if (FIXNUM_ZERO_P(y) && RB_INTEGER_TYPE_P(x))
return ZERO;
if (y == ONE) return x;
if (FIXNUM_ZERO_P(x) && RB_INTEGER_TYPE_P(y))
return ZERO;
if (x == ONE) return y;
else if (RB_INTEGER_TYPE_P(x))
return rb_int_mul(x, y);
return rb_funcall(x, '*', 1, y);
}
inline static VALUE
f_sub(VALUE x, VALUE y)
{
if (FIXNUM_P(y) && FIXNUM_ZERO_P(y))
return x;
return rb_funcall(x, '-', 1, y);
}
inline static VALUE
f_abs(VALUE x)
{
if (RB_INTEGER_TYPE_P(x))
return rb_int_abs(x);
return rb_funcall(x, id_abs, 0);
}
inline static int
f_integer_p(VALUE x)
{
return RB_INTEGER_TYPE_P(x);
}
inline static VALUE
f_to_i(VALUE x)
{
if (RB_TYPE_P(x, T_STRING))
return rb_str_to_inum(x, 10, 0);
return rb_funcall(x, id_to_i, 0);
}
inline static int
f_eqeq_p(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y))
return x == y;
if (RB_INTEGER_TYPE_P(x))
return RTEST(rb_int_equal(x, y));
return (int)rb_equal(x, y);
}
inline static VALUE
f_idiv(VALUE x, VALUE y)
{
if (RB_INTEGER_TYPE_P(x))
return rb_int_idiv(x, y);
return rb_funcall(x, id_idiv, 1, y);
}
#define f_expt10(x) rb_int_pow(INT2FIX(10), x)
inline static int
f_zero_p(VALUE x)
{
if (RB_INTEGER_TYPE_P(x)) {
return FIXNUM_ZERO_P(x);
}
else if (RB_TYPE_P(x, T_RATIONAL)) {
VALUE num = RRATIONAL(x)->num;
return FIXNUM_ZERO_P(num);
}
return (int)rb_equal(x, ZERO);
}
#define f_nonzero_p(x) (!f_zero_p(x))
inline static int
f_one_p(VALUE x)
{
if (RB_INTEGER_TYPE_P(x)) {
return x == LONG2FIX(1);
}
else if (RB_TYPE_P(x, T_RATIONAL)) {
VALUE num = RRATIONAL(x)->num;
VALUE den = RRATIONAL(x)->den;
return num == LONG2FIX(1) && den == LONG2FIX(1);
}
return (int)rb_equal(x, ONE);
}
inline static int
f_minus_one_p(VALUE x)
{
if (RB_INTEGER_TYPE_P(x)) {
return x == LONG2FIX(-1);
}
else if (RB_BIGNUM_TYPE_P(x)) {
return Qfalse;
}
else if (RB_TYPE_P(x, T_RATIONAL)) {
VALUE num = RRATIONAL(x)->num;
VALUE den = RRATIONAL(x)->den;
return num == LONG2FIX(-1) && den == LONG2FIX(1);
}
return (int)rb_equal(x, INT2FIX(-1));
}
inline static int
f_kind_of_p(VALUE x, VALUE c)
{
return (int)rb_obj_is_kind_of(x, c);
}
inline static int
k_numeric_p(VALUE x)
{
return f_kind_of_p(x, rb_cNumeric);
}
inline static int
k_integer_p(VALUE x)
{
return RB_INTEGER_TYPE_P(x);
}
inline static int
k_float_p(VALUE x)
{
return RB_FLOAT_TYPE_P(x);
}
inline static int
k_rational_p(VALUE x)
{
return RB_TYPE_P(x, T_RATIONAL);
}
#define k_exact_p(x) (!k_float_p(x))
#define k_inexact_p(x) k_float_p(x)
#define k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
#define k_exact_one_p(x) (k_exact_p(x) && f_one_p(x))
#ifdef USE_GMP
VALUE
rb_gcd_gmp(VALUE x, VALUE y)
{
const size_t nails = (sizeof(BDIGIT)-SIZEOF_BDIGIT)*CHAR_BIT;
mpz_t mx, my, mz;
size_t count;
VALUE z;
long zn;
mpz_init(mx);
mpz_init(my);
mpz_init(mz);
mpz_import(mx, BIGNUM_LEN(x), -1, sizeof(BDIGIT), 0, nails, BIGNUM_DIGITS(x));
mpz_import(my, BIGNUM_LEN(y), -1, sizeof(BDIGIT), 0, nails, BIGNUM_DIGITS(y));
mpz_gcd(mz, mx, my);
mpz_clear(mx);
mpz_clear(my);
zn = (mpz_sizeinbase(mz, 16) + SIZEOF_BDIGIT*2 - 1) / (SIZEOF_BDIGIT*2);
z = rb_big_new(zn, 1);
mpz_export(BIGNUM_DIGITS(z), &count, -1, sizeof(BDIGIT), 0, nails, mz);
mpz_clear(mz);
return rb_big_norm(z);
}
#endif
#ifndef NDEBUG
#define f_gcd f_gcd_orig
#endif
inline static long
i_gcd(long x, long y)
{
unsigned long u, v, t;
int shift;
if (x < 0)
x = -x;
if (y < 0)
y = -y;
if (x == 0)
return y;
if (y == 0)
return x;
u = (unsigned long)x;
v = (unsigned long)y;
for (shift = 0; ((u | v) & 1) == 0; ++shift) {
u >>= 1;
v >>= 1;
}
while ((u & 1) == 0)
u >>= 1;
do {
while ((v & 1) == 0)
v >>= 1;
if (u > v) {
t = v;
v = u;
u = t;
}
v = v - u;
} while (v != 0);
return (long)(u << shift);
}
inline static VALUE
f_gcd_normal(VALUE x, VALUE y)
{
VALUE z;
if (FIXNUM_P(x) && FIXNUM_P(y))
return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
if (INT_NEGATIVE_P(x))
x = rb_int_uminus(x);
if (INT_NEGATIVE_P(y))
y = rb_int_uminus(y);
if (INT_ZERO_P(x))
return y;
if (INT_ZERO_P(y))
return x;
for (;;) {
if (FIXNUM_P(x)) {
if (FIXNUM_ZERO_P(x))
return y;
if (FIXNUM_P(y))
return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
}
z = x;
x = rb_int_modulo(y, x);
y = z;
}
/* NOTREACHED */
}
VALUE
rb_gcd_normal(VALUE x, VALUE y)
{
return f_gcd_normal(x, y);
}
inline static VALUE
f_gcd(VALUE x, VALUE y)
{
#ifdef USE_GMP
if (RB_BIGNUM_TYPE_P(x) && RB_BIGNUM_TYPE_P(y)) {
size_t xn = BIGNUM_LEN(x);
size_t yn = BIGNUM_LEN(y);
if (GMP_GCD_DIGITS <= xn || GMP_GCD_DIGITS <= yn)
return rb_gcd_gmp(x, y);
}
#endif
return f_gcd_normal(x, y);
}
#ifndef NDEBUG
#undef f_gcd
inline static VALUE
f_gcd(VALUE x, VALUE y)
{
VALUE r = f_gcd_orig(x, y);
if (f_nonzero_p(r)) {
assert(f_zero_p(f_mod(x, r)));
assert(f_zero_p(f_mod(y, r)));
}
return r;
}
#endif
inline static VALUE
f_lcm(VALUE x, VALUE y)
{
if (INT_ZERO_P(x) || INT_ZERO_P(y))
return ZERO;
return f_abs(f_mul(f_div(x, f_gcd(x, y)), y));
}
#define get_dat1(x) \
struct RRational *dat = RRATIONAL(x)
#define get_dat2(x,y) \
struct RRational *adat = RRATIONAL(x), *bdat = RRATIONAL(y)
inline static VALUE
nurat_s_new_internal(VALUE klass, VALUE num, VALUE den)
{
NEWOBJ_OF(obj, struct RRational, klass, T_RATIONAL | (RGENGC_WB_PROTECTED_RATIONAL ? FL_WB_PROTECTED : 0));
RATIONAL_SET_NUM((VALUE)obj, num);
RATIONAL_SET_DEN((VALUE)obj, den);
OBJ_FREEZE_RAW((VALUE)obj);
return (VALUE)obj;
}
static VALUE
nurat_s_alloc(VALUE klass)
{
return nurat_s_new_internal(klass, ZERO, ONE);
}
inline static VALUE
f_rational_new_bang1(VALUE klass, VALUE x)
{
return nurat_s_new_internal(klass, x, ONE);
}
inline static void
nurat_int_check(VALUE num)
{
if (!RB_INTEGER_TYPE_P(num)) {
if (!k_numeric_p(num) || !f_integer_p(num))
rb_raise(rb_eTypeError, "not an integer");
}
}
inline static VALUE
nurat_int_value(VALUE num)
{
nurat_int_check(num);
if (!k_integer_p(num))
num = f_to_i(num);
return num;
}
static void
nurat_canonicalize(VALUE *num, VALUE *den)
{
assert(num); assert(RB_INTEGER_TYPE_P(*num));
assert(den); assert(RB_INTEGER_TYPE_P(*den));
if (INT_NEGATIVE_P(*den)) {
*num = rb_int_uminus(*num);
*den = rb_int_uminus(*den);
}
else if (INT_ZERO_P(*den)) {
rb_num_zerodiv();
}
}
static void
nurat_reduce(VALUE *x, VALUE *y)
{
VALUE gcd;
if (*x == ONE || *y == ONE) return;
gcd = f_gcd(*x, *y);
*x = f_idiv(*x, gcd);
*y = f_idiv(*y, gcd);
}
inline static VALUE
nurat_s_canonicalize_internal(VALUE klass, VALUE num, VALUE den)
{
nurat_canonicalize(&num, &den);
nurat_reduce(&num, &den);
return nurat_s_new_internal(klass, num, den);
}
inline static VALUE
nurat_s_canonicalize_internal_no_reduce(VALUE klass, VALUE num, VALUE den)
{
nurat_canonicalize(&num, &den);
return nurat_s_new_internal(klass, num, den);
}
inline static VALUE
f_rational_new2(VALUE klass, VALUE x, VALUE y)
{
assert(!k_rational_p(x));
assert(!k_rational_p(y));
return nurat_s_canonicalize_internal(klass, x, y);
}
inline static VALUE
f_rational_new_no_reduce2(VALUE klass, VALUE x, VALUE y)
{
assert(!k_rational_p(x));
assert(!k_rational_p(y));
return nurat_s_canonicalize_internal_no_reduce(klass, x, y);
}
static VALUE nurat_convert(VALUE klass, VALUE numv, VALUE denv, int raise);
static VALUE nurat_s_convert(int argc, VALUE *argv, VALUE klass);
/*
* call-seq:
* Rational(x, y, exception: true) -> rational or nil
* Rational(arg, exception: true) -> rational or nil
*
* Returns +x/y+ or +arg+ as a Rational.
*
* Rational(2, 3) #=> (2/3)
* Rational(5) #=> (5/1)
* Rational(0.5) #=> (1/2)
* Rational(0.3) #=> (5404319552844595/18014398509481984)
*
* Rational("2/3") #=> (2/3)
* Rational("0.3") #=> (3/10)
*
* Rational("10 cents") #=> ArgumentError
* Rational(nil) #=> TypeError
* Rational(1, nil) #=> TypeError
*
* Rational("10 cents", exception: false) #=> nil
*
* Syntax of the string form:
*
* string form = extra spaces , rational , extra spaces ;
* rational = [ sign ] , unsigned rational ;
* unsigned rational = numerator | numerator , "/" , denominator ;
* numerator = integer part | fractional part | integer part , fractional part ;
* denominator = digits ;
* integer part = digits ;
* fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ;
* sign = "-" | "+" ;
* digits = digit , { digit | "_" , digit } ;
* digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
* extra spaces = ? \s* ? ;
*
* See also String#to_r.
*/
static VALUE
nurat_f_rational(int argc, VALUE *argv, VALUE klass)
{
VALUE a1, a2, opts = Qnil;
int raise = TRUE;
if (rb_scan_args(argc, argv, "11:", &a1, &a2, &opts) == 1) {
a2 = Qundef;
}
if (!NIL_P(opts)) {
raise = rb_opts_exception_p(opts, raise);
}
return nurat_convert(rb_cRational, a1, a2, raise);
}
/*
* call-seq:
* rat.numerator -> integer
*
* Returns the numerator.
*
* Rational(7).numerator #=> 7
* Rational(7, 1).numerator #=> 7
* Rational(9, -4).numerator #=> -9
* Rational(-2, -10).numerator #=> 1
*/
static VALUE
nurat_numerator(VALUE self)
{
get_dat1(self);
return dat->num;
}
/*
* call-seq:
* rat.denominator -> integer
*
* Returns the denominator (always positive).
*
* Rational(7).denominator #=> 1
* Rational(7, 1).denominator #=> 1
* Rational(9, -4).denominator #=> 4
* Rational(-2, -10).denominator #=> 5
*/
static VALUE
nurat_denominator(VALUE self)
{
get_dat1(self);
return dat->den;
}
/*
* call-seq:
* -rat -> rational
*
* Negates +rat+.
*/
VALUE
rb_rational_uminus(VALUE self)
{
const int unused = (assert(RB_TYPE_P(self, T_RATIONAL)), 0);
get_dat1(self);
(void)unused;
return f_rational_new2(CLASS_OF(self), rb_int_uminus(dat->num), dat->den);
}
#ifndef NDEBUG
#define f_imul f_imul_orig
#endif
inline static VALUE
f_imul(long a, long b)
{
VALUE r;
if (a == 0 || b == 0)
return ZERO;
else if (a == 1)
return LONG2NUM(b);
else if (b == 1)
return LONG2NUM(a);
if (MUL_OVERFLOW_LONG_P(a, b))
r = rb_big_mul(rb_int2big(a), rb_int2big(b));
else
r = LONG2NUM(a * b);
return r;
}
#ifndef NDEBUG
#undef f_imul
inline static VALUE
f_imul(long x, long y)
{
VALUE r = f_imul_orig(x, y);
assert(f_eqeq_p(r, f_mul(LONG2NUM(x), LONG2NUM(y))));
return r;
}
#endif
inline static VALUE
f_addsub(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
{
VALUE num, den;
if (FIXNUM_P(anum) && FIXNUM_P(aden) &&
FIXNUM_P(bnum) && FIXNUM_P(bden)) {
long an = FIX2LONG(anum);
long ad = FIX2LONG(aden);
long bn = FIX2LONG(bnum);
long bd = FIX2LONG(bden);
long ig = i_gcd(ad, bd);
VALUE g = LONG2NUM(ig);
VALUE a = f_imul(an, bd / ig);
VALUE b = f_imul(bn, ad / ig);
VALUE c;
if (k == '+')
c = rb_int_plus(a, b);
else
c = rb_int_minus(a, b);
b = rb_int_idiv(aden, g);
g = f_gcd(c, g);
num = rb_int_idiv(c, g);
a = rb_int_idiv(bden, g);
den = rb_int_mul(a, b);
}
else if (RB_INTEGER_TYPE_P(anum) && RB_INTEGER_TYPE_P(aden) &&
RB_INTEGER_TYPE_P(bnum) && RB_INTEGER_TYPE_P(bden)) {
VALUE g = f_gcd(aden, bden);
VALUE a = rb_int_mul(anum, rb_int_idiv(bden, g));
VALUE b = rb_int_mul(bnum, rb_int_idiv(aden, g));
VALUE c;
if (k == '+')
c = rb_int_plus(a, b);
else
c = rb_int_minus(a, b);
b = rb_int_idiv(aden, g);
g = f_gcd(c, g);
num = rb_int_idiv(c, g);
a = rb_int_idiv(bden, g);
den = rb_int_mul(a, b);
}
else {
double a = NUM2DBL(anum) / NUM2DBL(aden);
double b = NUM2DBL(bnum) / NUM2DBL(bden);
double c = k == '+' ? a + b : a - b;
return DBL2NUM(c);
}
return f_rational_new_no_reduce2(CLASS_OF(self), num, den);
}
static double nurat_to_double(VALUE self);
/*
* call-seq:
* rat + numeric -> numeric
*
* Performs addition.
*
* Rational(2, 3) + Rational(2, 3) #=> (4/3)
* Rational(900) + Rational(1) #=> (901/1)
* Rational(-2, 9) + Rational(-9, 2) #=> (-85/18)
* Rational(9, 8) + 4 #=> (41/8)
* Rational(20, 9) + 9.8 #=> 12.022222222222222
*/
VALUE
rb_rational_plus(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
{
get_dat1(self);
return f_rational_new_no_reduce2(CLASS_OF(self),
rb_int_plus(dat->num, rb_int_mul(other, dat->den)),
dat->den);
}
}
else if (RB_FLOAT_TYPE_P(other)) {
return DBL2NUM(nurat_to_double(self) + RFLOAT_VALUE(other));
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
{
get_dat2(self, other);
return f_addsub(self,
adat->num, adat->den,
bdat->num, bdat->den, '+');
}
}
else {
return rb_num_coerce_bin(self, other, '+');
}
}
/*
* call-seq:
* rat - numeric -> numeric
*
* Performs subtraction.
*
* Rational(2, 3) - Rational(2, 3) #=> (0/1)
* Rational(900) - Rational(1) #=> (899/1)
* Rational(-2, 9) - Rational(-9, 2) #=> (77/18)
* Rational(9, 8) - 4 #=> (-23/8)
* Rational(20, 9) - 9.8 #=> -7.577777777777778
*/
VALUE
rb_rational_minus(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
{
get_dat1(self);
return f_rational_new_no_reduce2(CLASS_OF(self),
rb_int_minus(dat->num, rb_int_mul(other, dat->den)),
dat->den);
}
}
else if (RB_FLOAT_TYPE_P(other)) {
return DBL2NUM(nurat_to_double(self) - RFLOAT_VALUE(other));
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
{
get_dat2(self, other);
return f_addsub(self,
adat->num, adat->den,
bdat->num, bdat->den, '-');
}
}
else {
return rb_num_coerce_bin(self, other, '-');
}
}
inline static VALUE
f_muldiv(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
{
VALUE num, den;
assert(RB_TYPE_P(self, T_RATIONAL));
/* Integer#** can return Rational with Float right now */
if (RB_FLOAT_TYPE_P(anum) || RB_FLOAT_TYPE_P(aden) ||
RB_FLOAT_TYPE_P(bnum) || RB_FLOAT_TYPE_P(bden)) {
double an = NUM2DBL(anum), ad = NUM2DBL(aden);
double bn = NUM2DBL(bnum), bd = NUM2DBL(bden);
double x = (an * bn) / (ad * bd);
return DBL2NUM(x);
}
assert(RB_INTEGER_TYPE_P(anum));
assert(RB_INTEGER_TYPE_P(aden));
assert(RB_INTEGER_TYPE_P(bnum));
assert(RB_INTEGER_TYPE_P(bden));
if (k == '/') {
VALUE t;
if (INT_NEGATIVE_P(bnum)) {
anum = rb_int_uminus(anum);
bnum = rb_int_uminus(bnum);
}
t = bnum;
bnum = bden;
bden = t;
}
if (FIXNUM_P(anum) && FIXNUM_P(aden) &&
FIXNUM_P(bnum) && FIXNUM_P(bden)) {
long an = FIX2LONG(anum);
long ad = FIX2LONG(aden);
long bn = FIX2LONG(bnum);
long bd = FIX2LONG(bden);
long g1 = i_gcd(an, bd);
long g2 = i_gcd(ad, bn);
num = f_imul(an / g1, bn / g2);
den = f_imul(ad / g2, bd / g1);
}
else {
VALUE g1 = f_gcd(anum, bden);
VALUE g2 = f_gcd(aden, bnum);
num = rb_int_mul(rb_int_idiv(anum, g1), rb_int_idiv(bnum, g2));
den = rb_int_mul(rb_int_idiv(aden, g2), rb_int_idiv(bden, g1));
}
return f_rational_new_no_reduce2(CLASS_OF(self), num, den);
}
/*
* call-seq:
* rat * numeric -> numeric
*
* Performs multiplication.
*
* Rational(2, 3) * Rational(2, 3) #=> (4/9)
* Rational(900) * Rational(1) #=> (900/1)
* Rational(-2, 9) * Rational(-9, 2) #=> (1/1)
* Rational(9, 8) * 4 #=> (9/2)
* Rational(20, 9) * 9.8 #=> 21.77777777777778
*/
VALUE
rb_rational_mul(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
{
get_dat1(self);
return f_muldiv(self,
dat->num, dat->den,
other, ONE, '*');
}
}
else if (RB_FLOAT_TYPE_P(other)) {
return DBL2NUM(nurat_to_double(self) * RFLOAT_VALUE(other));
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
{
get_dat2(self, other);
return f_muldiv(self,
adat->num, adat->den,
bdat->num, bdat->den, '*');
}
}
else {
return rb_num_coerce_bin(self, other, '*');
}
}
/*
* call-seq:
* rat / numeric -> numeric
* rat.quo(numeric) -> numeric
*
* Performs division.
*
* Rational(2, 3) / Rational(2, 3) #=> (1/1)
* Rational(900) / Rational(1) #=> (900/1)
* Rational(-2, 9) / Rational(-9, 2) #=> (4/81)
* Rational(9, 8) / 4 #=> (9/32)
* Rational(20, 9) / 9.8 #=> 0.22675736961451246
*/
VALUE
rb_rational_div(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
if (f_zero_p(other))
rb_num_zerodiv();
{
get_dat1(self);
return f_muldiv(self,
dat->num, dat->den,
other, ONE, '/');
}
}
else if (RB_FLOAT_TYPE_P(other)) {
VALUE v = nurat_to_f(self);
return rb_flo_div_flo(v, other);
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
if (f_zero_p(other))
rb_num_zerodiv();
{
get_dat2(self, other);
if (f_one_p(self))
return f_rational_new_no_reduce2(CLASS_OF(self),
bdat->den, bdat->num);
return f_muldiv(self,
adat->num, adat->den,
bdat->num, bdat->den, '/');
}
}
else {
return rb_num_coerce_bin(self, other, '/');
}
}
/*
* call-seq:
* rat.fdiv(numeric) -> float
*
* Performs division and returns the value as a Float.
*
* Rational(2, 3).fdiv(1) #=> 0.6666666666666666
* Rational(2, 3).fdiv(0.5) #=> 1.3333333333333333
* Rational(2).fdiv(3) #=> 0.6666666666666666
*/
static VALUE
nurat_fdiv(VALUE self, VALUE other)
{
VALUE div;
if (f_zero_p(other))
return rb_rational_div(self, rb_float_new(0.0));
if (FIXNUM_P(other) && other == LONG2FIX(1))
return nurat_to_f(self);
div = rb_rational_div(self, other);
if (RB_TYPE_P(div, T_RATIONAL))
return nurat_to_f(div);
if (RB_FLOAT_TYPE_P(div))
return div;
return rb_funcall(div, idTo_f, 0);
}
/*
* call-seq:
* rat ** numeric -> numeric
*
* Performs exponentiation.
*
* Rational(2) ** Rational(3) #=> (8/1)
* Rational(10) ** -2 #=> (1/100)
* Rational(10) ** -2.0 #=> 0.01
* Rational(-4) ** Rational(1, 2) #=> (0.0+2.0i)
* Rational(1, 2) ** 0 #=> (1/1)
* Rational(1, 2) ** 0.0 #=> 1.0
*/
VALUE
rb_rational_pow(VALUE self, VALUE other)
{
if (k_numeric_p(other) && k_exact_zero_p(other))
return f_rational_new_bang1(CLASS_OF(self), ONE);
if (k_rational_p(other)) {
get_dat1(other);
if (f_one_p(dat->den))
other = dat->num; /* c14n */
}
/* Deal with special cases of 0**n and 1**n */
if (k_numeric_p(other) && k_exact_p(other)) {
get_dat1(self);
if (f_one_p(dat->den)) {
if (f_one_p(dat->num)) {
return f_rational_new_bang1(CLASS_OF(self), ONE);
}
else if (f_minus_one_p(dat->num) && RB_INTEGER_TYPE_P(other)) {
return f_rational_new_bang1(CLASS_OF(self), INT2FIX(rb_int_odd_p(other) ? -1 : 1));
}
else if (INT_ZERO_P(dat->num)) {
if (rb_num_negative_p(other)) {
rb_num_zerodiv();
}
else {
return f_rational_new_bang1(CLASS_OF(self), ZERO);