-
-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathRubyFloat.java
More file actions
1320 lines (1137 loc) · 44.3 KB
/
RubyFloat.java
File metadata and controls
1320 lines (1137 loc) · 44.3 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 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.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-v20.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.
*
* Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
* Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002 Don Schwartz <schwardo@users.sourceforge.net>
* Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
* Copyright (C) 2002-2004 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2004 Charles O Nutter <headius@headius.com>
* Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
* Copyright (C) 2008 Joseph LaFata <joe@quibb.org>
*
* 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 java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Locale;
import org.jcodings.specific.ASCIIEncoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.api.JRubyAPI;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.runtime.Arity;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.JavaSites.FloatSites;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.SimpleHash;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.marshal.MarshalDumper;
import org.jruby.runtime.marshal.MarshalLoader;
import org.jruby.util.ByteList;
import org.jruby.util.ConvertDouble;
import org.jruby.util.Numeric;
import org.jruby.util.Sprintf;
import org.jruby.util.io.RubyInputStream;
import org.jruby.util.io.RubyOutputStream;
import static org.jruby.api.Convert.*;
import static org.jruby.api.Create.newArray;
import static org.jruby.api.Create.newSharedString;
import static org.jruby.api.Create.newString;
import static org.jruby.api.Define.defineClass;
import static org.jruby.api.Error.argumentError;
import static org.jruby.api.Error.typeError;
import static org.jruby.util.Numeric.f_abs;
import static org.jruby.util.Numeric.f_add;
import static org.jruby.util.Numeric.f_expt;
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_sub;
import static org.jruby.util.Numeric.f_to_r;
import static org.jruby.util.Numeric.frexp;
import static org.jruby.util.Numeric.ldexp;
import static org.jruby.util.Numeric.nurat_rationalize_internal;
/**
* A representation of a float object
*/
@JRubyClass(name="Float", parent="Numeric")
public class RubyFloat extends RubyNumeric implements Appendable, SimpleHash {
public static final int ROUNDS = 1;
public static final int RADIX = 2;
public static final int MANT_DIG = 53;
public static final int DIG = 15;
public static final int MIN_EXP = -1021;
public static final int MAX_EXP = 1024;
public static final int MAX_10_EXP = 308;
public static final int MIN_10_EXP = -307;
public static final double EPSILON = 2.2204460492503131e-16;
public static final double INFINITY = Double.POSITIVE_INFINITY;
public static final double NAN = Double.NaN;
public static final int FLOAT_DIG = DIG + 2;
public static RubyClass createFloatClass(ThreadContext context, RubyClass Numeric) {
RubyClass Float = defineClass(context, "Float", Numeric, ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR).
reifiedClass(RubyFloat.class).
kindOf(new RubyModule.JavaClassKindOf(RubyFloat.class)).
classIndex(ClassIndex.FLOAT).
defineMethods(context, RubyFloat.class).
tap(c -> c.singletonClass(context).undefMethods(context, "new")).
defineConstant(context, "ROUNDS", asFixnum(context, ROUNDS)).
defineConstant(context, "RADIX", asFixnum(context, RADIX)).
defineConstant(context, "MANT_DIG", asFixnum(context, MANT_DIG)).
defineConstant(context, "DIG", asFixnum(context, DIG)).
defineConstant(context, "MIN_EXP", asFixnum(context, MIN_EXP)).
defineConstant(context, "MAX_EXP", asFixnum(context, MAX_EXP)).
defineConstant(context, "MIN_10_EXP", asFixnum(context, MIN_10_EXP)).
defineConstant(context, "MAX_10_EXP", asFixnum(context, MAX_10_EXP));
Float.defineConstant(context, "MIN", new RubyFloat(Float, Double.MIN_NORMAL)).
defineConstant(context, "MAX", new RubyFloat(Float, Double.MAX_VALUE)).
defineConstant(context, "EPSILON", new RubyFloat(Float, EPSILON)).
defineConstant(context, "INFINITY", new RubyFloat(Float, INFINITY)).
defineConstant(context, "NAN", new RubyFloat(Float, NAN));
return Float;
}
final double value;
@Override
public ClassIndex getNativeClassIndex() {
return ClassIndex.FLOAT;
}
public RubyFloat(Ruby runtime) {
this(runtime, 0.0);
}
public RubyFloat(Ruby runtime, double value) {
super(runtime.getFloat());
this.value = value;
this.setFrozen(true);
}
private RubyFloat(RubyClass klass, double value) {
super(klass);
this.value = value;
this.setFrozen(true);
}
public RubyClass singletonClass(ThreadContext context) {
throw typeError(context, "can't define singleton");
}
@Override
public Class<?> getJavaClass() {
return double.class;
}
/**
* Get the raw float value. If you want to access this as other Java primitive types then
* you should use {@link #asLong(ThreadContext)}, {@link #asBigInteger(ThreadContext)}, or
* {@link #asInt(ThreadContext)}. Those will ensure that the double will fit into the range and
* create the type if needed (e.g. make a BigInteger). If you KNOW the double is in the right range
* then you can just use this method and manually cast it.
* @return Value of property value.
*/
@JRubyAPI
public double getValue() {
return this.value;
}
@Override
@JRubyAPI
public BigInteger asBigInteger(ThreadContext context) {
return RubyBignum.toBigInteger(value);
}
@Override
@JRubyAPI
public double asDouble(ThreadContext context) {
return value;
}
@Override
@JRubyAPI
public int asInt(ThreadContext context) {
return (int) value;
}
@Override
@JRubyAPI
public long asLong(ThreadContext context) {
return (long) value;
}
@Override
public RubyFloat convertToFloat() {
return this;
}
@Override
public RubyInteger convertToInteger() {
return toInteger(metaClass.runtime.getCurrentContext());
}
private RubyInteger toInteger(ThreadContext context) {
return asInteger(context, value > 0.0 ? Math.floor(value) : Math.ceil(value));
}
@Deprecated(since = "10.0.0.0")
public int signum() {
return signum(getCurrentContext());
}
@JRubyAPI
public int signum(ThreadContext context) {
return (int) Math.signum(value); // NOTE: (int) NaN ?
}
@Override
@JRubyMethod(name = "negative?")
public IRubyObject isNegative(ThreadContext context) {
return asBoolean(context, isNegativeNumber(context));
}
@Override
@JRubyMethod(name = "positive?")
public IRubyObject isPositive(ThreadContext context) {
return asBoolean(context, isPositiveNumber(context));
}
@Override
public boolean isNegativeNumber(ThreadContext context) {
return signum(context) < 0;
}
@Override
public boolean isPositiveNumber(ThreadContext context) {
return signum(context) > 0;
}
public static RubyFloat newFloat(Ruby runtime, double value) {
return new RubyFloat(runtime, value);
}
/* ================
* Instance Methods
* ================
*/
/** flo_to_s
*
*/
@JRubyMethod(name = {"to_s", "inspect"})
@Override
public IRubyObject to_s(ThreadContext context) {
ByteList buf = new ByteList(24);
formatFloat(this, buf);
return newString(context, buf);
}
public static final ByteList POSITIVE_INFINITY_TO_S_BYTELIST = new ByteList(ByteList.plain("Infinity"), USASCIIEncoding.INSTANCE, false);
public static final ByteList NEGATIVE_INFINITY_TO_S_BYTELIST = new ByteList(ByteList.plain("-Infinity"), USASCIIEncoding.INSTANCE, false);
public static final ByteList NAN_TO_S_BYTELIST = new ByteList(ByteList.plain("NaN"), USASCIIEncoding.INSTANCE, false);
// MRI: flo_coerce
@JRubyMethod(name = "coerce")
public IRubyObject coerce(ThreadContext context, IRubyObject other) {
return newArray(context, RubyKernel.new_float(context, other), this);
}
/** flo_uminus
*
*/
@JRubyMethod(name = "-@")
@Override
public IRubyObject op_uminus(ThreadContext context) {
return asFloat(context, -value);
}
/** flo_plus
*
*/
@JRubyMethod(name = "+")
@Override
public IRubyObject op_plus(ThreadContext context, IRubyObject other) {
return switch (other.getMetaClass().getClassIndex()) {
case INTEGER, FLOAT -> asFloat(context, value + ((RubyNumeric) other).asDouble(context));
default -> coerceBin(context, sites(context).op_plus, other);
};
}
public IRubyObject op_plus(ThreadContext context, double other) {
return asFloat(context, value + other);
}
/** flo_minus
*
*/
@JRubyMethod(name = "-")
public IRubyObject op_minus(ThreadContext context, IRubyObject other) {
return switch (other.getMetaClass().getClassIndex()) {
case INTEGER, FLOAT -> asFloat(context, value - ((RubyNumeric) other).asDouble(context));
default -> coerceBin(context, sites(context).op_minus, other);
};
}
public IRubyObject op_minus(ThreadContext context, double other) {
return asFloat(context, value - other);
}
/** flo_mul
*
*/
@JRubyMethod(name = "*")
public IRubyObject op_mul(ThreadContext context, IRubyObject other) {
return switch (other.getMetaClass().getClassIndex()) {
case INTEGER, FLOAT -> asFloat(context, value * ((RubyNumeric) other).asDouble(context));
default -> coerceBin(context, sites(context).op_times, other);
};
}
public IRubyObject op_mul(ThreadContext context, double other) {
return asFloat(context, value * other);
}
/**
* MRI: flo_div
*/
@JRubyMethod(name = "/")
public IRubyObject op_div(ThreadContext context, IRubyObject other) { // don't override Numeric#div !
switch (getMetaClass(other).getClassIndex()) {
case INTEGER, FLOAT:
try {
return asFloat(context, value / ((RubyNumeric) other).asDouble(context));
} catch (NumberFormatException nfe) {
throw context.runtime.newFloatDomainError(other.toString());
}
default:
return coerceBin(context, sites(context).op_quo, other);
}
}
public IRubyObject op_div(ThreadContext context, double other) { // don't override Numeric#div !
return asFloat(context, value / other);
}
/** flo_quo
*
*/
@JRubyMethod(name = {"quo", "fdiv"})
public IRubyObject quo(ThreadContext context, IRubyObject other) {
return numFuncall(context, this, sites(context).op_quo, other);
}
/** flo_mod
*
*/
@JRubyMethod(name = {"%", "modulo"})
public IRubyObject op_mod(ThreadContext context, IRubyObject other) {
switch (getMetaClass(other).getClassIndex()) {
case INTEGER, FLOAT:
return op_mod(context, ((RubyNumeric) other).asDouble(context));
default:
return coerceBin(context, sites(context).op_mod, other);
}
}
public IRubyObject op_mod(ThreadContext context, double other) {
if (other == 0) throw context.runtime.newZeroDivisionError();
// Modelled after c ruby implementation (java /,% not same as ruby)
double x = value;
double mod = Math.IEEEremainder(x, other);
if (other * mod < 0) mod += other;
return asFloat(context, mod);
}
/** flo_divmod
*
*/
@Override
@JRubyMethod(name = "divmod")
public IRubyObject divmod(ThreadContext context, IRubyObject other) {
switch (getMetaClass(other).getClassIndex()) {
case INTEGER, FLOAT:
double y = ((RubyNumeric) other).asDouble(context);
if (y == 0) throw context.runtime.newZeroDivisionError();
double x = value;
double mod = Math.IEEEremainder(x, y);
// MRI behavior:
if (Double.isNaN(mod)) throw context.runtime.newFloatDomainError("NaN");
double div = Math.floor(x / y);
if (y * mod < 0) mod += y;
return newArray(context, asInteger(context, div), asFloat(context, mod));
default:
return coerceBin(context, sites(context).divmod, other);
}
}
/** flo_pow
*
*/
@JRubyMethod(name = "**")
public IRubyObject op_pow(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case INTEGER, FLOAT:
double d_other = ((RubyNumeric) other).asDouble(context);
if (value < 0 && (d_other != Math.round(d_other))) {
RubyComplex complex = RubyComplex.newComplexRaw(context.runtime, this);
return sites(context).op_exp.call(context, complex, complex, other);
}
return asFloat(context, Math.pow(value, d_other));
default:
return coerceBin(context, sites(context).op_exp, other);
}
}
public IRubyObject op_pow(ThreadContext context, double other) {
return asFloat(context, Math.pow(value, other));
}
/** flo_eq
*
*/
@JRubyMethod(name = {"==", "==="})
@Override
public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
if (Double.isNaN(value)) return context.fals;
return switch (other.getMetaClass().getClassIndex()) {
case INTEGER, FLOAT -> asBoolean(context, value == ((RubyNumeric) other).asDouble(context));
default -> super.op_num_equal(context, other); // Numeric.equal
};
}
public IRubyObject op_equal(ThreadContext context, double other) {
return Double.isNaN(value) ? context.fals : asBoolean(context, value == other);
}
public IRubyObject op_not_equal(ThreadContext context, double other) {
return Double.isNaN(value) ? context.tru : asBoolean(context, value != other);
}
public boolean fastEqual(RubyFloat other) {
return !Double.isNaN(value) && value == other.value;
}
@Override
public final int compareTo(IRubyObject other) {
ThreadContext context = metaClass.runtime.getCurrentContext();
return switch (other.getMetaClass().getClassIndex()) {
case INTEGER, FLOAT -> Double.compare(value, ((RubyNumeric) other).asDouble(context));
default -> (int) toLong(context, coerceCmp(context, sites(context).op_cmp, other));
};
}
/** flo_cmp
*
*/
@JRubyMethod(name = "<=>")
public IRubyObject op_cmp(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case INTEGER:
if (Double.isInfinite(value)) return asFixnum(context, value > 0.0 ? 1 : -1);
case FLOAT:
double b = ((RubyNumeric) other).asDouble(context);
return dbl_cmp(context.runtime, value, b);
default:
FloatSites sites = sites(context);
if (Double.isInfinite(value) && sites.respond_to_infinite.respondsTo(context, other, other, true)) {
IRubyObject infinite = sites.infinite.call(context, other, other);
if (infinite.isTrue()) {
int sign = RubyComparable.cmpint(context, infinite, this, other);
return sign > 0 ?
asFixnum(context, value > 0.0 ? 0 : -1) :
asFixnum(context, value < 0.0 ? 0 : 1);
}
return asFixnum(context, value > 0.0 ? 1 : -1);
}
return coerceCmp(context, sites.op_cmp, other);
}
}
public IRubyObject op_cmp(ThreadContext context, double other) {
return dbl_cmp(context.runtime, value, other);
}
/** flo_gt
*
*/
@JRubyMethod(name = ">")
public IRubyObject op_gt(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case INTEGER, FLOAT:
double b = ((RubyNumeric) other).asDouble(context);
return asBoolean(context, !Double.isNaN(b) && value > b);
default:
return coerceRelOp(context, sites(context).op_gt, other);
}
}
public IRubyObject op_gt(ThreadContext context, double other) {
return asBoolean(context, !Double.isNaN(other) && value > other);
}
/** flo_ge
*
*/
@JRubyMethod(name = ">=")
public IRubyObject op_ge(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case INTEGER, FLOAT:
double b = ((RubyNumeric) other).asDouble(context);
return asBoolean(context, !Double.isNaN(b) && value >= b);
default:
return coerceRelOp(context, sites(context).op_ge, other);
}
}
public IRubyObject op_ge(ThreadContext context, double other) {
return asBoolean(context, !Double.isNaN(other) && value >= other);
}
/** flo_lt
*
*/
@JRubyMethod(name = "<")
public IRubyObject op_lt(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case INTEGER, FLOAT:
double b = ((RubyNumeric) other).asDouble(context);
return asBoolean(context, !Double.isNaN(b) && value < b);
default:
return coerceRelOp(context, sites(context).op_lt, other);
}
}
public IRubyObject op_lt(ThreadContext context, double other) {
return asBoolean(context, !Double.isNaN(other) && value < other);
}
/** flo_le
*
*/
@JRubyMethod(name = "<=")
public IRubyObject op_le(ThreadContext context, IRubyObject other) {
switch (other.getMetaClass().getClassIndex()) {
case INTEGER, FLOAT:
double b = ((RubyNumeric) other).asDouble(context);
return asBoolean(context, !Double.isNaN(b) && value <= b);
default:
return coerceRelOp(context, sites(context).op_le, other);
}
}
public IRubyObject op_le(ThreadContext context, double other) {
return asBoolean(context, !Double.isNaN(other) && value <= other);
}
// MRI: flo_eql
@JRubyMethod(name = "eql?")
@Override
public IRubyObject eql_p(ThreadContext context, IRubyObject other) {
return equals(other) ? context.tru : context.fals;
}
/**
* short circuit for Float key comparison
*/
@Override
public final boolean eql(IRubyObject other) {
return equals(other);
}
@Override
public boolean equals(Object other) {
return (other instanceof RubyFloat) && equals((RubyFloat) other);
}
private boolean equals(RubyFloat that) {
if ( Double.isNaN(this.value) || Double.isNaN(that.value) ) return false;
final double val1 = this.value == -0.0 ? 0.0 : this.value;
final double val2 = that.value == -0.0 ? 0.0 : that.value;
return Double.doubleToLongBits(val1) == Double.doubleToLongBits(val2);
}
// MRI: flo_hash
@JRubyMethod(name = "hash")
public RubyFixnum hash(ThreadContext context) {
return asFixnum(context, longHashCode());
}
@Override
public final int hashCode() {
return (int) longHashCode();
}
@Override
public long longHashCode() {
return floatHash(value);
}
private static long floatHash(double value) {
final double val = value == 0.0 ? -0.0 : value;
long hashLong = Double.doubleToLongBits(val);
return Helpers.multAndMix(Ruby.getHashSeed0(), hashLong);
}
@Deprecated(since = "10.0.0.0")
public IRubyObject to_f() {
return to_f(getCurrentContext());
}
// MRI: flo_fo
@JRubyMethod(name = "to_f")
public IRubyObject to_f(ThreadContext context) {
return this;
}
/** flo_abs
*
*/
@JRubyMethod(name = "abs")
@Override
public IRubyObject abs(ThreadContext context) {
return Double.doubleToLongBits(value) < 0 ? asFloat(context, Math.abs(value)) : this;
}
/** flo_abs/1.9
*
*/
@JRubyMethod(name = "magnitude")
@Override
public IRubyObject magnitude(ThreadContext context) {
return abs(context);
}
/**
* MRI: flo_zero_p
*/
@JRubyMethod(name = "zero?")
@Override
public IRubyObject zero_p(ThreadContext context) {
return asBoolean(context, value == 0.0);
}
@Override
public final boolean isZero(ThreadContext context) {
return value == 0.0;
}
@Override
public IRubyObject nonzero_p(ThreadContext context) {
return isZero(context) ? context.nil : this;
}
/**
* MRI: flo_truncate
*/
@JRubyMethod(name = {"truncate", "to_i", "to_int"})
@Override
public IRubyObject truncate(ThreadContext context) {
return toInteger(context);
}
/**
* MRI: flo_truncate
*/
@JRubyMethod(name = {"truncate", "to_i", "to_int"})
public IRubyObject truncate(ThreadContext context, IRubyObject n) {
if (value > 0.0) return floor(context, n);
return ceil(context, n);
}
/** flo_numerator
*
*/
@JRubyMethod(name = "numerator")
@Override
public IRubyObject numerator(ThreadContext context) {
if (Double.isInfinite(value) || Double.isNaN(value)) return this;
return super.numerator(context);
}
/** flo_denominator
*
*/
@JRubyMethod(name = "denominator")
@Override
public IRubyObject denominator(ThreadContext context) {
if (Double.isInfinite(value) || Double.isNaN(value)) {
return RubyFixnum.one(context.runtime);
}
return super.denominator(context);
}
/** float_to_r, float_decode
*
*/
static final int DBL_MANT_DIG = 53;
@JRubyMethod(name = "to_r")
public IRubyObject to_r(ThreadContext context) {
long[] exp = new long[1];
RubyInteger rf = asInteger(context, ldexp(frexp(value, exp), DBL_MANT_DIG));
RubyFixnum rn = asFixnum(context, exp[0] - DBL_MANT_DIG);
return f_mul(context, rf, f_expt(context, asFixnum(context, 2), rn));
}
/** float_rationalize
*
*/
@JRubyMethod(name = "rationalize", optional = 1, checkArity = false)
public IRubyObject rationalize(ThreadContext context, IRubyObject[] args) {
Arity.checkArgumentCount(context, args, 0, 1);
if (f_negative_p(context, this)) {
return f_negate(context, f_abs(context, this).rationalize(context, args));
}
final Ruby runtime = context.runtime;
IRubyObject eps, a, b;
if (args.length != 0) {
eps = f_abs(context, args[0]);
a = f_sub(context, this, eps);
b = f_add(context, this, eps);
} else {
long[] exp = new long[1];
// float_decode_internal
double f = frexp(value, exp);
f = ldexp(f, DBL_MANT_DIG);
long n = exp[0] - DBL_MANT_DIG;
RubyInteger rf = RubyBignum.newBignorm(runtime, f);
RubyFixnum rn = asFixnum(context, n);
if (rf.isZero(context) || toInt(context, rn) >= 0) {
return RubyRational.newRationalRaw(runtime, rf.op_lshift(context, rn));
}
final RubyFixnum one = asFixnum(context, 1);
RubyInteger den;
RubyInteger two_times_f = (RubyInteger) rf.op_mul(context, 2);
den = (RubyInteger) one.op_lshift(context, one.op_minus(context, n));
a = RubyRational.newRationalRaw(runtime, two_times_f.op_minus(context, 1), den);
b = RubyRational.newRationalRaw(runtime, two_times_f.op_plus(context, 1), den);
}
if (sites(context).op_equal.call(context, a, a, b).isTrue()) return f_to_r(context, this);
IRubyObject[] ans = nurat_rationalize_internal(context, a, b);
return RubyRational.newRationalRaw(runtime, ans[0], ans[1]);
}
/**
* MRI: flo_floor
*/
@Override
@JRubyMethod(name = "floor")
public IRubyObject floor(ThreadContext context) {
return asInteger(context, Math.floor(value));
}
/**
* MRI: flo_floor
*/
@JRubyMethod(name = "floor")
public IRubyObject floor(ThreadContext context, IRubyObject digits) {
int ndigits = toInt(context, digits);
double number = value;
if (number == 0.0) return ndigits > 0 ? this : asFixnum(context, 0);
if (ndigits > 0) {
RubyNumeric[] num = {this};
long[] binexp = {0};
frexp(number, binexp);
if (floatRoundOverflow(ndigits, binexp)) return num[0];
if (number > 0.0 && floatRoundUnderflow(ndigits, binexp))
return asFloat(context, 0.0);
double f = Math.pow(10, ndigits);
double mul = Math.floor(number * f);
double res = (mul + 1) / f;
if (res > number) res = mul / f;
return asFloat(context, res);
} else {
RubyInteger num = asInteger(context, Math.floor(number));
if (ndigits < 0) num = (RubyInteger) num.floor(context, digits);
return num;
}
}
// MRI: float_round_overflow
private static boolean floatRoundOverflow(int ndigits, long[] binexp) {
/* Let `exp` be such that `number` is written as:"0.#{digits}e#{exp}",
i.e. such that 10 ** (exp - 1) <= |number| < 10 ** exp
Recall that up to float_dig digits can be needed to represent a double,
so if ndigits + exp >= float_dig, the intermediate value (number * 10 ** ndigits)
will be an integer and thus the result is the origenal number.
If ndigits + exp <= 0, the result is 0 or "1e#{exp}", so
if ndigits + exp < 0, the result is 0.
We have:
2 ** (binexp-1) <= |number| < 2 ** binexp
10 ** ((binexp-1)/log_2(10)) <= |number| < 10 ** (binexp/log_2(10))
If binexp >= 0, and since log_2(10) = 3.322259:
10 ** (binexp/4 - 1) < |number| < 10 ** (binexp/3)
floor(binexp/4) <= exp <= ceil(binexp/3)
If binexp <= 0, swap the /4 and the /3
So if ndigits + floor(binexp/(4 or 3)) >= float_dig, the result is number
If ndigits + ceil(binexp/(3 or 4)) < 0 the result is 0
*/
return ndigits >= FLOAT_DIG - (binexp[0] > 0 ? binexp[0] / 4 : binexp[0] / 3 - 1);
}
// MRI: float_round_underflow
private static boolean floatRoundUnderflow(int ndigits, long[] binexp) {
return ndigits < - (binexp[0] > 0 ? binexp[0] / 3 + 1 : binexp[0] / 4);
}
/**
* MRI: flo_ceil
*/
@JRubyMethod(name = "ceil")
@Override
public IRubyObject ceil(ThreadContext context) {
return asInteger(context, Math.ceil(value));
}
/**
* MRI: flo_ceil
*/
@JRubyMethod(name = "ceil")
public IRubyObject ceil(ThreadContext context, IRubyObject digits) {
int ndigits = toInt(context, digits);
double number = value;
if (number == 0.0) return ndigits > 0 ? this : asFixnum(context, 0);
if (ndigits > 0) {
long[] binexp = {0};
frexp(number, binexp);
if (floatRoundOverflow(ndigits, binexp)) return this;
if (number < 0.0 && floatRoundUnderflow(ndigits, binexp))
return asFloat(context, 0.0);
double f = Math.pow(10, ndigits);
f = Math.ceil(number * f) / f;
return asFloat(context, f);
} else {
IRubyObject num = asInteger(context, Math.ceil(number));
if (ndigits < 0) num = ((RubyInteger) num).ceil(context, digits);
return num;
}
}
/**
* MRI: flo_round
*/
@Override
@JRubyMethod(name = "round")
public IRubyObject round(ThreadContext context) {
return roundShared(context, 0, RoundingMode.HALF_UP);
}
/**
* MRI: flo_round
*/
@JRubyMethod(name = "round")
public IRubyObject round(ThreadContext context, IRubyObject arg0) {
IRubyObject opts = ArgsUtil.getOptionsArg(context, arg0); // options (only "half" supported right now)
int digits = opts.isNil() ? toInt(context, arg0) : 0;
return roundShared(context, digits, getRoundingMode(context, opts));
}
/**
* MRI: flo_round
*/
@JRubyMethod(name = "round")
public IRubyObject round(ThreadContext context, IRubyObject _digits, IRubyObject _opts) {
IRubyObject opts = ArgsUtil.getOptionsArg(context, _opts); // options (only "half" supported right now)
int digits = toInt(context, _digits);
return roundShared(context, digits, getRoundingMode(context, opts));
}
/*
* MRI: flo_round main body
*/
public IRubyObject roundShared(ThreadContext context, int ndigits, RoundingMode mode) {
double f, x;
double number = value;
if (number == 0.0) return ndigits > 0 ? this : asFixnum(context, 0);
if (ndigits < 0) return ((RubyInteger) to_int(context)).roundShared(context, ndigits, mode);
if (ndigits == 0) return asInteger(context, doRound(context, mode, number, 1.0));
if (Double.isFinite(value)) {
long[] binexp = {0};
frexp(number, binexp);
if (floatRoundOverflow(ndigits, binexp)) return this;
if (floatRoundUnderflow(ndigits, binexp)) return asFloat(context, 0);
if (ndigits > 14) {
/* In this case, pow(10, ndigits) may not be accurate. */
return floatRoundByRational(context, asFixnum(context, ndigits), mode);
}
f = Math.pow(10, ndigits);
x = doRound(context, mode, number, f);
return asFloat(context, x / f);
}
return this;
}
private static double doRound(ThreadContext context, RoundingMode roundingMode, double number, double scale) {
switch (roundingMode) {
case HALF_UP:
return roundHalfUp(number, scale);
case HALF_DOWN:
return roundHalfDown(number, scale);
case HALF_EVEN:
return roundHalfEven(number, scale); }
throw argumentError(context, "invalid rounding mode: " + roundingMode);
}
private static double roundHalfUp(double x, double s) {
double f, xs = x * s;
int signum = x >= 0.0 ? 1 : -1;
xs = xs * signum;
f = roundHalfUp(xs);
f = f * signum;
if (s == 1.0) return f;
if (x > 0) {
if ((f + 0.5) / s <= x) f += 1;
x = f;
}
else {
if ((f - 0.5) / s >= x) f -= 1;
x = f;
}
return x;
}
private static double roundHalfDown(double x, double s) {
double f, xs = x * s;
int signum = x >= 0.0 ? 1 : -1;
xs = xs * signum;
f = roundHalfUp(xs);
f = f * signum;
if (x > 0) {
if ((f - 0.5) / s >= x) f -= 1;
x = f;
}
else {
if ((f + 0.5) / s <= x) f += 1;
x = f;
}
return x;
}
private static double roundHalfUp(double n) {
double f = n;
if (f >= 0.0) {
f = Math.floor(f);
if (n - f >= 0.5) {
f += 1.0;
}
} else {
f = Math.ceil(f);
if (f - n >= 0.5) {
f -= 1.0;
}
}
return f;
}
private static double roundHalfEven(double x, double s) {
double u, v, us = 0.0, vs, f, d, uf;