forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRubyRational.java
More file actions
1093 lines (945 loc) · 40.6 KB
/
Copy pathRubyRational.java
File metadata and controls
1093 lines (945 loc) · 40.6 KB
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
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby;
import static org.jruby.util.Numeric.checkInteger;
import static org.jruby.util.Numeric.f_abs;
import static org.jruby.util.Numeric.f_add;
import static org.jruby.util.Numeric.f_cmp;
import static org.jruby.util.Numeric.f_div;
import static org.jruby.util.Numeric.f_equal;
import static org.jruby.util.Numeric.f_expt;
import static org.jruby.util.Numeric.f_floor;
import static org.jruby.util.Numeric.f_gcd;
import static org.jruby.util.Numeric.f_idiv;
import static org.jruby.util.Numeric.f_inspect;
import static org.jruby.util.Numeric.f_integer_p;
import static org.jruby.util.Numeric.f_lt_p;
import static org.jruby.util.Numeric.f_mul;
import static org.jruby.util.Numeric.f_negate;
import static org.jruby.util.Numeric.f_negative_p;
import static org.jruby.util.Numeric.f_one_p;
import static org.jruby.util.Numeric.f_rshift;
import static org.jruby.util.Numeric.f_sub;
import static org.jruby.util.Numeric.f_to_f;
import static org.jruby.util.Numeric.f_to_i;
import static org.jruby.util.Numeric.f_to_r;
import static org.jruby.util.Numeric.f_to_s;
import static org.jruby.util.Numeric.f_truncate;
import static org.jruby.util.Numeric.f_xor;
import static org.jruby.util.Numeric.f_zero_p;
import static org.jruby.util.Numeric.i_gcd;
import static org.jruby.util.Numeric.i_ilog2;
import static org.jruby.util.Numeric.k_exact_p;
import static org.jruby.util.Numeric.ldexp;
import static org.jruby.util.Numeric.nurat_rationalize_internal;
import org.jcodings.specific.ASCIIEncoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.common.IRubyWarnings;
import org.jruby.runtime.Arity;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import org.jruby.util.Numeric;
import static org.jruby.javasupport.util.RuntimeHelpers.invokedynamic;
import static org.jruby.runtime.invokedynamic.MethodNames.HASH;
/**
* 1.9 rational.c as of revision: 20011
*/
@JRubyClass(name = "Rational", parent = "Numeric", include = "Precision")
public class RubyRational extends RubyNumeric {
public static RubyClass createRationalClass(Ruby runtime) {
RubyClass rationalc = runtime.defineClass("Rational", runtime.getNumeric(), RATIONAL_ALLOCATOR);
runtime.setRational(rationalc);
rationalc.index = ClassIndex.RATIONAL;
rationalc.setReifiedClass(RubyRational.class);
rationalc.kindOf = new RubyModule.KindOf() {
@Override
public boolean isKindOf(IRubyObject obj, RubyModule type) {
return obj instanceof RubyRational;
}
};
rationalc.defineAnnotatedMethods(RubyRational.class);
rationalc.getSingletonClass().undefineMethod("allocate");
rationalc.getSingletonClass().undefineMethod("new");
return rationalc;
}
private static ObjectAllocator RATIONAL_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
RubyFixnum zero = RubyFixnum.zero(runtime);
return new RubyRational(runtime, klass, zero, zero);
}
};
/** internal
*
*/
private RubyRational(Ruby runtime, IRubyObject clazz, IRubyObject num, IRubyObject den) {
super(runtime, (RubyClass)clazz);
this.num = num;
this.den = den;
}
/** rb_rational_raw
*
*/
static RubyRational newRationalRaw(Ruby runtime, IRubyObject x, IRubyObject y) {
return new RubyRational(runtime, runtime.getRational(), x, y);
}
/** rb_rational_raw1
*
*/
static RubyRational newRationalRaw(Ruby runtime, IRubyObject x) {
return new RubyRational(runtime, runtime.getRational(), x, RubyFixnum.one(runtime));
}
/** rb_rational_new1
*
*/
static IRubyObject newRationalCanonicalize(ThreadContext context, IRubyObject x) {
return newRationalCanonicalize(context, x, RubyFixnum.one(context.runtime));
}
/** rb_rational_new
*
*/
private static IRubyObject newRationalCanonicalize(ThreadContext context, IRubyObject x, IRubyObject y) {
return canonicalizeInternal(context, context.runtime.getRational(), x, y);
}
/** f_rational_new2
*
*/
private static IRubyObject newRational(ThreadContext context, IRubyObject clazz, IRubyObject x, IRubyObject y) {
assert !(x instanceof RubyRational) && !(y instanceof RubyRational);
return canonicalizeInternal(context, clazz, x, y);
}
/** f_rational_new_no_reduce2
*
*/
private static IRubyObject newRationalNoReduce(ThreadContext context, IRubyObject clazz, IRubyObject x, IRubyObject y) {
assert !(x instanceof RubyRational) && !(y instanceof RubyRational);
return canonicalizeInternalNoReduce(context, clazz, x, y);
}
/** f_rational_new_bang2
*
*/
private static RubyRational newRationalBang(ThreadContext context, IRubyObject clazz, IRubyObject x, IRubyObject y) {
assert !f_negative_p(context, y) && !(f_zero_p(context, y));
return new RubyRational(context.runtime, clazz, x, y);
}
/** f_rational_new_bang1
*
*/
private static RubyRational newRationalBang(ThreadContext context, IRubyObject clazz, IRubyObject x) {
return newRationalBang(context, clazz, x, RubyFixnum.one(context.runtime));
}
private IRubyObject num;
private IRubyObject den;
/** nurat_s_new_bang
*
*/
@Deprecated
public static IRubyObject newInstanceBang(ThreadContext context, IRubyObject recv, IRubyObject[]args) {
switch (args.length) {
case 1: return newInstanceBang(context, recv, args[0]);
case 2: return newInstanceBang(context, recv, args[0], args[1]);
}
Arity.raiseArgumentError(context.runtime, args.length, 1, 1);
return null;
}
@JRubyMethod(name = "new!", meta = true, visibility = Visibility.PRIVATE, compat = CompatVersion.RUBY1_8)
public static IRubyObject newInstanceBang(ThreadContext context, IRubyObject recv, IRubyObject num) {
return newInstanceBang(context, recv, num, RubyFixnum.one(context.runtime));
}
@JRubyMethod(name = "new!", meta = true, visibility = Visibility.PRIVATE, compat = CompatVersion.RUBY1_8)
public static IRubyObject newInstanceBang(ThreadContext context, IRubyObject recv, IRubyObject num, IRubyObject den) {
if (!(num instanceof RubyInteger)) num = f_to_i(context, num);
if (!(den instanceof RubyInteger)) den = f_to_i(context, den);
Ruby runtime = context.runtime;
IRubyObject res = f_cmp(context, den, RubyFixnum.zero(runtime));
if (res == RubyFixnum.minus_one(runtime)) {
num = f_negate(context, num);
den = f_negate(context, den);
} else if (res == RubyFixnum.zero(runtime)) {
throw runtime.newZeroDivisionError();
}
return new RubyRational(runtime, recv, num, den);
}
/** nurat_canonicalization
*
*/
private static boolean canonicalization = false;
public static void setCanonicalization(boolean canonical) {
canonicalization = canonical;
}
/** nurat_int_check
*
*/
static void intCheck(ThreadContext context, IRubyObject num) {
if (num instanceof RubyFixnum || num instanceof RubyBignum) return;
if (!(num instanceof RubyNumeric) || !num.callMethod(context, "integer?").isTrue()) {
Ruby runtime = num.getRuntime();
if (runtime.is1_9()) {
throw runtime.newTypeError("can't convert "
+ num.getMetaClass().getName() + " into Rational");
} else {
throw runtime.newArgumentError("not an integer");
}
}
}
/** nurat_int_value
*
*/
static IRubyObject intValue(ThreadContext context, IRubyObject num) {
intCheck(context, num);
if (!(num instanceof RubyInteger)) num = num.callMethod(context, "to_f");
return num;
}
/** nurat_s_canonicalize_internal
*
*/
private static IRubyObject canonicalizeInternal(ThreadContext context, IRubyObject clazz, IRubyObject num, IRubyObject den) {
Ruby runtime = context.runtime;
IRubyObject res = f_cmp(context, den, RubyFixnum.zero(runtime));
if (res == RubyFixnum.minus_one(runtime)) {
num = f_negate(context, num);
den = f_negate(context, den);
} else if (res == RubyFixnum.zero(runtime)) {
throw runtime.newZeroDivisionError();
}
IRubyObject gcd = f_gcd(context, num, den);
num = f_idiv(context, num, gcd);
den = f_idiv(context, den, gcd);
if (Numeric.CANON) {
if (f_one_p(context, den) && canonicalization) return num;
}
return new RubyRational(context.runtime, clazz, num, den);
}
/** nurat_s_canonicalize_internal_no_reduce
*
*/
private static IRubyObject canonicalizeInternalNoReduce(ThreadContext context, IRubyObject clazz, IRubyObject num, IRubyObject den) {
Ruby runtime = context.runtime;
IRubyObject res = f_cmp(context, den, RubyFixnum.zero(runtime));
if (res == RubyFixnum.minus_one(runtime)) {
num = f_negate(context, num);
den = f_negate(context, den);
} else if (res == RubyFixnum.zero(runtime)) {
throw runtime.newZeroDivisionError();
}
if (Numeric.CANON) {
if (f_one_p(context, den) && canonicalization) return num;
}
return new RubyRational(context.runtime, clazz, num, den);
}
/** nurat_s_new
*
*/
@Deprecated
public static IRubyObject newInstance(ThreadContext context, IRubyObject clazz, IRubyObject[]args) {
switch (args.length) {
case 1: return newInstance(context, clazz, args[0]);
case 2: return newInstance(context, clazz, args[0], args[1]);
}
Arity.raiseArgumentError(context.runtime, args.length, 1, 1);
return null;
}
// @JRubyMethod(name = "new", meta = true, visibility = Visibility.PRIVATE)
public static IRubyObject newInstance(ThreadContext context, IRubyObject clazz, IRubyObject num) {
num = intValue(context, num);
return canonicalizeInternal(context, clazz, num, RubyFixnum.one(context.runtime));
}
// @JRubyMethod(name = "new", meta = true, visibility = Visibility.PRIVATE)
public static IRubyObject newInstance(ThreadContext context, IRubyObject clazz, IRubyObject num, IRubyObject den) {
num = intValue(context, num);
den = intValue(context, den);
return canonicalizeInternal(context, clazz, num, den);
}
/** rb_Rational1
*
*/
public static IRubyObject newRationalConvert(ThreadContext context, IRubyObject x) {
return newRationalConvert(context, x, RubyFixnum.one(context.runtime));
}
/** rb_Rational/rb_Rational2
*
*/
public static IRubyObject newRationalConvert(ThreadContext context, IRubyObject x, IRubyObject y) {
return convert(context, context.runtime.getRational(), x, y);
}
public static RubyRational newRational(Ruby runtime, long x, long y) {
return new RubyRational(runtime, runtime.getRational(), runtime.newFixnum(x), runtime.newFixnum(y));
}
@Deprecated
public static IRubyObject convert(ThreadContext context, IRubyObject clazz, IRubyObject[]args) {
switch (args.length) {
case 1: return convert(context, clazz, args[0]);
case 2: return convert(context, clazz, args[0], args[1]);
}
Arity.raiseArgumentError(context.runtime, args.length, 1, 1);
return null;
}
/** nurat_s_convert
*
*/
@JRubyMethod(name = "convert", meta = true, visibility = Visibility.PRIVATE)
public static IRubyObject convert(ThreadContext context, IRubyObject recv, IRubyObject a1) {
if (a1.isNil()) {
throw context.runtime.newTypeError("can't convert nil into Rational");
}
return convertCommon(context, recv, a1, context.runtime.getNil());
}
/** nurat_s_convert
*
*/
@JRubyMethod(name = "convert", meta = true, visibility = Visibility.PRIVATE)
public static IRubyObject convert(ThreadContext context, IRubyObject recv, IRubyObject a1, IRubyObject a2) {
if (a1.isNil() || a2.isNil()) {
throw context.runtime.newTypeError("can't convert nil into Rational");
}
return convertCommon(context, recv, a1, a2);
}
private static IRubyObject convertCommon(ThreadContext context, IRubyObject recv, IRubyObject a1, IRubyObject a2) {
if (a1 instanceof RubyComplex) {
RubyComplex a1Complex = (RubyComplex)a1;
if (k_exact_p(a1Complex.getImage()) && f_zero_p(context, a1Complex.getImage())) a1 = a1Complex.getReal();
}
if (a2 instanceof RubyComplex) {
RubyComplex a2Complex = (RubyComplex)a2;
if (k_exact_p(a2Complex.getImage()) && f_zero_p(context, a2Complex.getImage())) a2 = a2Complex.getReal();
}
DynamicScope scope = context.getCurrentScope();
IRubyObject backref = scope.getBackRef(context.runtime);
if (backref instanceof RubyMatchData) ((RubyMatchData)backref).use();
if (a1 instanceof RubyFloat) {
a1 = f_to_r(context, a1);
} else if (a1 instanceof RubyString) {
a1 = str_to_r_strict(context, a1);
} else {
if (a1.respondsTo("to_r")) {
a1 = f_to_r(context, a1);
}
}
if (a2 instanceof RubyFloat) {
a2 = f_to_r(context, a2);
} else if (a2 instanceof RubyString) {
a2 = str_to_r_strict(context, a2);
}
scope.setBackRef(backref);
if (a1 instanceof RubyRational) {
if (a2.isNil() || (k_exact_p(a2) && f_one_p(context, a2))) return a1;
}
if (a2.isNil()) {
if (a1 instanceof RubyNumeric && !f_integer_p(context, a1).isTrue()) return a1;
return newInstance(context, recv, a1);
} else {
if (a1 instanceof RubyNumeric && a2 instanceof RubyNumeric &&
(!f_integer_p(context, a1).isTrue() || !f_integer_p(context, a2).isTrue())) {
return f_div(context, a1, a2);
}
return newInstance(context, recv, a1, a2);
}
}
/** nurat_numerator
*
*/
@JRubyMethod(name = "numerator")
@Override
public IRubyObject numerator(ThreadContext context) {
return num;
}
/** nurat_denominator
*
*/
@JRubyMethod(name = "denominator")
@Override
public IRubyObject denominator(ThreadContext context) {
return den;
}
/** f_imul
*
*/
private static IRubyObject f_imul(ThreadContext context, long a, long b) {
Ruby runtime = context.runtime;
if (a == 0 || b == 0) {
return RubyFixnum.zero(runtime);
} else if (a == 1) {
return RubyFixnum.newFixnum(runtime, b);
} else if (b == 1) {
return RubyFixnum.newFixnum(runtime, a);
}
long c = a * b;
if(c / a != b) {
return RubyBignum.newBignum(runtime, a).op_mul(context, RubyBignum.newBignum(runtime, b));
}
return RubyFixnum.newFixnum(runtime, c);
}
/** f_addsub
*
*/
private IRubyObject f_addsub(ThreadContext context, IRubyObject anum, IRubyObject aden, IRubyObject bnum, IRubyObject bden, boolean plus) {
Ruby runtime = context.runtime;
IRubyObject newNum, newDen, g, a, b;
if (anum instanceof RubyFixnum && aden instanceof RubyFixnum &&
bnum instanceof RubyFixnum && bden instanceof RubyFixnum) {
long an = ((RubyFixnum)anum).getLongValue();
long ad = ((RubyFixnum)aden).getLongValue();
long bn = ((RubyFixnum)bnum).getLongValue();
long bd = ((RubyFixnum)bden).getLongValue();
long ig = i_gcd(ad, bd);
g = RubyFixnum.newFixnum(runtime, ig);
a = f_imul(context, an, bd / ig);
b = f_imul(context, bn, ad / ig);
} else {
g = f_gcd(context, aden, bden);
a = f_mul(context, anum, f_idiv(context, bden, g));
b = f_mul(context, bnum, f_idiv(context, aden, g));
}
IRubyObject c = plus ? f_add(context, a, b) : f_sub(context, a, b);
b = f_idiv(context, aden, g);
g = f_gcd(context, c, g);
newNum = f_idiv(context, c, g);
a = f_idiv(context, bden, g);
newDen = f_mul(context, a, b);
return RubyRational.newRationalNoReduce(context, getMetaClass(), newNum, newDen);
}
/** nurat_add
*
*/
@JRubyMethod(name = "+")
public IRubyObject op_add(ThreadContext context, IRubyObject other) {
if (other instanceof RubyFixnum || other instanceof RubyBignum) {
return f_addsub(context, num, den, other, RubyFixnum.one(context.runtime), true);
} else if (other instanceof RubyFloat) {
return f_add(context, f_to_f(context, this), other);
} else if (other instanceof RubyRational) {
RubyRational otherRational = (RubyRational)other;
return f_addsub(context, num, den, otherRational.num, otherRational.den, true);
}
return coerceBin(context, "+", other);
}
/** nurat_sub
*
*/
@JRubyMethod(name = "-")
public IRubyObject op_sub(ThreadContext context, IRubyObject other) {
if (other instanceof RubyFixnum || other instanceof RubyBignum) {
return f_addsub(context, num, den, other, RubyFixnum.one(context.runtime), false);
} else if (other instanceof RubyFloat) {
return f_sub(context, f_to_f(context, this), other);
} else if (other instanceof RubyRational) {
RubyRational otherRational = (RubyRational)other;
return f_addsub(context, num, den, otherRational.num, otherRational.den, false);
}
return coerceBin(context, "-", other);
}
/** f_muldiv
*
*/
private IRubyObject f_muldiv(ThreadContext context, IRubyObject anum, IRubyObject aden, IRubyObject bnum, IRubyObject bden, boolean mult) {
if (!mult) {
if (f_negative_p(context, bnum)) {
anum = f_negate(context, anum);
bnum = f_negate(context, bnum);
}
IRubyObject tmp = bnum;
bnum = bden;
bden = tmp;
}
final IRubyObject newNum, newDen;
if (anum instanceof RubyFixnum && aden instanceof RubyFixnum &&
bnum instanceof RubyFixnum && bden instanceof RubyFixnum) {
long an = ((RubyFixnum)anum).getLongValue();
long ad = ((RubyFixnum)aden).getLongValue();
long bn = ((RubyFixnum)bnum).getLongValue();
long bd = ((RubyFixnum)bden).getLongValue();
long g1 = i_gcd(an, bd);
long g2 = i_gcd(ad, bn);
newNum = f_imul(context, an / g1, bn / g2);
newDen = f_imul(context, ad / g2, bd / g1);
} else {
IRubyObject g1 = f_gcd(context, anum, bden);
IRubyObject g2 = f_gcd(context, aden, bnum);
newNum = f_mul(context, f_idiv(context, anum, g1), f_idiv(context, bnum, g2));
newDen = f_mul(context, f_idiv(context, aden, g2), f_idiv(context, bden, g1));
}
return RubyRational.newRationalNoReduce(context, getMetaClass(), newNum, newDen);
}
/** nurat_mul
*
*/
@JRubyMethod(name = "*")
public IRubyObject op_mul(ThreadContext context, IRubyObject other) {
if (other instanceof RubyFixnum || other instanceof RubyBignum) {
return f_muldiv(context, num, den, other, RubyFixnum.one(context.runtime), true);
} else if (other instanceof RubyFloat) {
return f_mul(context, f_to_f(context, this), other);
} else if (other instanceof RubyRational) {
RubyRational otherRational = (RubyRational)other;
return f_muldiv(context, num, den, otherRational.num, otherRational.den, true);
}
return coerceBin(context, "*", other);
}
/** nurat_div
*
*/
@JRubyMethod(name = {"/", "quo"})
public IRubyObject op_div(ThreadContext context, IRubyObject other) {
if (other instanceof RubyFixnum || other instanceof RubyBignum) {
if (f_zero_p(context, other)) {
throw context.runtime.newZeroDivisionError();
}
return f_muldiv(context, num, den, other, RubyFixnum.one(context.runtime), false);
} else if (other instanceof RubyFloat) {
return f_to_f(context, this).callMethod(context, "/", other);
} else if (other instanceof RubyRational) {
if (f_zero_p(context, other)) {
throw context.runtime.newZeroDivisionError();
}
RubyRational otherRational = (RubyRational)other;
return f_muldiv(context, num, den, otherRational.num, otherRational.den, false);
}
return coerceBin(context, "/", other);
}
/** nurat_fdiv
*
*/
@JRubyMethod(name = "fdiv")
public IRubyObject op_fdiv(ThreadContext context, IRubyObject other) {
return f_div(context, f_to_f(context, this), other);
}
/** nurat_expt
*
*/
@JRubyMethod(name = "**")
public IRubyObject op_expt(ThreadContext context, IRubyObject other) {
Ruby runtime = context.runtime;
if (k_exact_p(other) && f_zero_p(context, other)) {
return RubyRational.newRationalBang(context, getMetaClass(), RubyFixnum.one(runtime));
}
if (other instanceof RubyRational) {
RubyRational otherRational = (RubyRational)other;
if (f_one_p(context, otherRational.den)) other = otherRational.num;
}
if (other instanceof RubyFixnum || other instanceof RubyBignum) {
final IRubyObject tnum, tden;
IRubyObject res = f_cmp(context, other, RubyFixnum.zero(runtime));
if (res == RubyFixnum.one(runtime)) {
tnum = f_expt(context, num, other);
tden = f_expt(context, den, other);
} else if (res == RubyFixnum.minus_one(runtime)){
tnum = f_expt(context, den, f_negate(context, other));
tden = f_expt(context, num, f_negate(context, other));
} else {
tnum = tden = RubyFixnum.one(runtime);
}
return RubyRational.newRational(context, getMetaClass(), tnum, tden);
} else if (other instanceof RubyFloat || other instanceof RubyRational) {
return f_expt(context, f_to_f(context, this), other);
}
return coerceBin(context, "**", other);
}
/** nurat_cmp
*
*/
@JRubyMethod(name = "<=>")
public IRubyObject op_cmp(ThreadContext context, IRubyObject other) {
if (other instanceof RubyFixnum || other instanceof RubyBignum) {
if (den instanceof RubyFixnum && ((RubyFixnum)den).getLongValue() == 1) return f_cmp(context, num, other);
return f_cmp(context, this, RubyRational.newRationalBang(context, getMetaClass(), other));
} else if (other instanceof RubyFloat) {
return f_cmp(context, f_to_f(context, this), other);
} else if (other instanceof RubyRational) {
RubyRational otherRational = (RubyRational)other;
final IRubyObject num1, num2;
if (num instanceof RubyFixnum && den instanceof RubyFixnum &&
otherRational.num instanceof RubyFixnum && otherRational.den instanceof RubyFixnum) {
num1 = f_imul(context, ((RubyFixnum)num).getLongValue(), ((RubyFixnum)otherRational.den).getLongValue());
num2 = f_imul(context, ((RubyFixnum)otherRational.num).getLongValue(), ((RubyFixnum)den).getLongValue());
} else {
num1 = f_mul(context, num, otherRational.den);
num2 = f_mul(context, otherRational.num, den);
}
return f_cmp(context, f_sub(context, num1, num2), RubyFixnum.zero(context.runtime));
}
return coerceBin(context, "<=>", other);
}
/** nurat_equal_p
*
*/
@JRubyMethod(name = "==")
@Override
public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
Ruby runtime = context.runtime;
if (other instanceof RubyFixnum || other instanceof RubyBignum) {
if (f_zero_p(context, num) && f_zero_p(context, other)) return runtime.getTrue();
if (!(den instanceof RubyFixnum) || ((RubyFixnum)den).getLongValue() != 1) return runtime.getFalse();
return f_equal(context, num, other);
} else if (other instanceof RubyFloat) {
return f_equal(context, f_to_f(context, this), other);
} else if (other instanceof RubyRational) {
RubyRational otherRational = (RubyRational)other;
if (f_zero_p(context, num) && f_zero_p(context, otherRational.num)) return runtime.getTrue();
return runtime.newBoolean(f_equal(context, num, otherRational.num).isTrue() &&
f_equal(context, den, otherRational.den).isTrue());
}
return f_equal(context, other, this);
}
/** nurat_coerce
*
*/
@JRubyMethod(name = "coerce")
public IRubyObject op_coerce(ThreadContext context, IRubyObject other) {
Ruby runtime = context.runtime;
if (other instanceof RubyFixnum || other instanceof RubyBignum) {
return runtime.newArray(RubyRational.newRationalBang(context, getMetaClass(), other), this);
} else if (other instanceof RubyFloat) {
return runtime.newArray(other, f_to_f(context, this));
} else if (other instanceof RubyRational) {
return runtime.newArray(other, this);
}
throw runtime.newTypeError(other.getMetaClass() + " can't be coerced into " + getMetaClass());
}
/** nurat_idiv
*
*/
@JRubyMethod(name = "div", compat = CompatVersion.RUBY1_8)
public IRubyObject op_idiv(ThreadContext context, IRubyObject other) {
return f_floor(context, f_div(context, this, other));
}
@JRubyMethod(name = "div", compat = CompatVersion.RUBY1_9)
public IRubyObject op_idiv19(ThreadContext context, IRubyObject other) {
if (num2dbl(other) == 0.0) {
throw context.runtime.newZeroDivisionError();
}
return op_idiv(context, other);
}
/** nurat_mod
*
*/
@JRubyMethod(name = {"modulo", "%"}, compat = CompatVersion.RUBY1_8)
public IRubyObject op_mod(ThreadContext context, IRubyObject other) {
IRubyObject val = f_floor(context, f_div(context, this, other));
return f_sub(context, this, f_mul(context, other, val));
}
@JRubyMethod(name = {"modulo", "%"}, compat = CompatVersion.RUBY1_9)
public IRubyObject op_mod19(ThreadContext context, IRubyObject other) {
if (num2dbl(other) == 0.0) {
throw context.runtime.newZeroDivisionError();
}
return op_mod(context, other);
}
/** nurat_divmod
*
*/
@JRubyMethod(name = "divmod", compat = CompatVersion.RUBY1_8)
public IRubyObject op_divmod(ThreadContext context, IRubyObject other) {
IRubyObject val = f_floor(context, f_div(context, this, other));
return context.runtime.newArray(val, f_sub(context, this, f_mul(context, other, val)));
}
@JRubyMethod(name = "divmod", compat = CompatVersion.RUBY1_9)
public IRubyObject op_divmod19(ThreadContext context, IRubyObject other) {
if (num2dbl(other) == 0.0) {
throw context.runtime.newZeroDivisionError();
}
return op_divmod(context, other);
}
/** nurat_rem
*
*/
@JRubyMethod(name = "remainder")
public IRubyObject op_rem(ThreadContext context, IRubyObject other) {
IRubyObject val = f_truncate(context, f_div(context, this, other));
return f_sub(context, this, f_mul(context, other, val));
}
/** nurat_abs
*
*/
@JRubyMethod(name = "abs")
public IRubyObject op_abs(ThreadContext context) {
if (!f_negative_p(context, this)) return this;
return f_negate(context, this);
}
private IRubyObject op_roundCommonPre(ThreadContext context, IRubyObject n) {
checkInteger(context, n);
Ruby runtime = context.runtime;
return f_expt(context, RubyFixnum.newFixnum(runtime, 10), n);
}
private IRubyObject op_roundCommonPost(ThreadContext context, IRubyObject s, IRubyObject n, IRubyObject b) {
s = f_div(context, newRationalBang(context, getMetaClass(), s), b);
if (f_lt_p(context, n, RubyFixnum.one(context.runtime)).isTrue()) s = f_to_i(context, s);
return s;
}
/** nurat_floor
*
*/
@JRubyMethod(name = "floor")
public IRubyObject op_floor(ThreadContext context) {
return f_idiv(context, num, den);
}
@JRubyMethod(name = "floor")
public IRubyObject op_floor(ThreadContext context, IRubyObject n) {
IRubyObject b = op_roundCommonPre(context, n);
return op_roundCommonPost(context, ((RubyRational)f_mul(context, this, b)).op_floor(context), n, b);
}
/** nurat_ceil
*
*/
@JRubyMethod(name = "ceil")
public IRubyObject op_ceil(ThreadContext context) {
return f_negate(context, f_idiv(context, f_negate(context, num), den));
}
@JRubyMethod(name = "ceil")
public IRubyObject op_ceil(ThreadContext context, IRubyObject n) {
IRubyObject b = op_roundCommonPre(context, n);
return op_roundCommonPost(context, ((RubyRational)f_mul(context, this, b)).op_ceil(context), n, b);
}
@JRubyMethod(name = "to_i")
public IRubyObject to_i(ThreadContext context) {
return op_truncate(context);
}
/** nurat_truncate
*
*/
@JRubyMethod(name = "truncate")
public IRubyObject op_truncate(ThreadContext context) {
if (f_negative_p(context, num)) {
return f_negate(context, f_idiv(context, f_negate(context, num), den));
}
return f_idiv(context, num, den);
}
@JRubyMethod(name = "truncate")
public IRubyObject op_truncate(ThreadContext context, IRubyObject n) {
IRubyObject b = op_roundCommonPre(context, n);
return op_roundCommonPost(context, ((RubyRational)f_mul(context, this, b)).op_truncate(context), n, b);
}
/** nurat_round
*
*/
@JRubyMethod(name = "round")
public IRubyObject op_round(ThreadContext context) {
IRubyObject myNum = this.num;
boolean neg = f_negative_p(context, myNum);
if (neg) myNum = f_negate(context, myNum);
IRubyObject myDen = this.den;
IRubyObject two = RubyFixnum.two(context.runtime);
myNum = f_add(context, f_mul(context, myNum, two), myDen);
myDen = f_mul(context, myDen, two);
myNum = f_idiv(context, myNum, myDen);
if (neg) myNum = f_negate(context, myNum);
return myNum;
}
@JRubyMethod(name = "round")
public IRubyObject op_round(ThreadContext context, IRubyObject n) {
IRubyObject b = op_roundCommonPre(context, n);
return op_roundCommonPost(context, ((RubyRational)f_mul(context, this, b)).op_round(context), n, b);
}
/** nurat_to_f
*
*/
private static long ML = (long)(Math.log(Double.MAX_VALUE) / Math.log(2.0) - 1);
@JRubyMethod(name = "to_f")
public IRubyObject to_f(ThreadContext context) {
return context.runtime.newFloat(getDoubleValue(context));
}
@Override
public double getDoubleValue() {
return getDoubleValue(getRuntime().getCurrentContext());
}
public double getDoubleValue(ThreadContext context) {
Ruby runtime = context.runtime;
if (f_zero_p(context, num)) return 0;
IRubyObject myNum = this.num;
IRubyObject myDen = this.den;
boolean minus = false;
if (f_negative_p(context, myNum)) {
myNum = f_negate(context, myNum);
minus = true;
}
long nl = i_ilog2(context, myNum);
long dl = i_ilog2(context, myDen);
long ne = 0;
if (nl > ML) {
ne = nl - ML;
myNum = f_rshift(context, myNum, RubyFixnum.newFixnum(runtime, ne));
}
long de = 0;
if (dl > ML) {
de = dl - ML;
myDen = f_rshift(context, myDen, RubyFixnum.newFixnum(runtime, de));
}
long e = ne - de;
if (e > 1023 || e < -1022) {
runtime.getWarnings().warn(IRubyWarnings.ID.FLOAT_OUT_OF_RANGE, "out of Float range");
return e > 0 ? Double.MAX_VALUE : 0;
}
double f = RubyNumeric.num2dbl(myNum) / RubyNumeric.num2dbl(myDen);
if (minus) f = -f;
f = ldexp(f, e);
if (Double.isInfinite(f) || Double.isNaN(f)) {
runtime.getWarnings().warn(IRubyWarnings.ID.FLOAT_OUT_OF_RANGE, "out of Float range");
}
return f;
}
/** nurat_to_r
*
*/
@JRubyMethod(name = "to_r")
public IRubyObject to_r(ThreadContext context) {
return this;
}
/** nurat_rationalize
*
*/
@JRubyMethod(name = "rationalize", optional = 1, compat = CompatVersion.RUBY1_9)
public IRubyObject rationalize(ThreadContext context, IRubyObject[] args) {
IRubyObject a, b;
if (args.length == 0) return to_r(context);
if (f_negative_p(context, this)) {
return f_negate(context,
((RubyRational) f_abs(context, this)).rationalize(context, args));
}
IRubyObject eps = f_abs(context, args[0]);
a = f_sub(context, this, eps);
b = f_add(context, this, eps);
if (f_equal(context, a, b).isTrue()) return this;
IRubyObject[] ary = new IRubyObject[2];
ary[0] = a;
ary[1] = b;
IRubyObject[] ans = nurat_rationalize_internal(context, ary);
return newRational(context, this.metaClass, ans[0], ans[1]);
}
/** nurat_hash
*
*/
@JRubyMethod(name = "hash")
public IRubyObject hash(ThreadContext context) {
return f_xor(context, invokedynamic(context, num, HASH), invokedynamic(context, den, HASH));
}
/** nurat_to_s
*
*/
@JRubyMethod(name = "to_s")
public IRubyObject to_s(ThreadContext context) {
RubyString str = RubyString.newEmptyString(context.getRuntime());
str.append(f_to_s(context, num));
str.cat((byte)'/');
str.append(f_to_s(context, den));
return str;
}
/** nurat_inspect
*
*/
@JRubyMethod(name = "inspect")
public IRubyObject inspect(ThreadContext context) {
RubyString str = RubyString.newEmptyString(context.getRuntime());
str.cat((byte)'(');
str.append(f_inspect(context, num));
str.cat((byte)'/');
str.append(f_inspect(context, den));
str.cat((byte)')');
return str;
}
/** nurat_marshal_dump
*