forked from mruby/mruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric.c
More file actions
2274 lines (2071 loc) · 53.2 KB
/
numeric.c
File metadata and controls
2274 lines (2071 loc) · 53.2 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
/*
** numeric.c - Numeric, Integer, Float class
**
** See Copyright Notice in mruby.h
*/
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/numeric.h>
#include <mruby/string.h>
#include <mruby/class.h>
#include <mruby/internal.h>
#include <mruby/presym.h>
#include <string.h>
#ifndef MRB_NO_FLOAT
#ifdef MRB_USE_FLOAT32
#define trunc(f) truncf(f)
#define fmod(x,y) fmodf(x,y)
#else
#endif
#endif
mrb_noreturn void
mrb_int_overflow(mrb_state *mrb, const char *reason)
{
mrb_raisef(mrb, E_RANGE_ERROR, "integer overflow in %s", reason);
}
mrb_noreturn void
mrb_int_zerodiv(mrb_state *mrb)
{
mrb_raise(mrb, E_ZERODIV_ERROR, "divided by 0");
}
static mrb_noreturn void
mrb_int_noconv(mrb_state *mrb, mrb_value y)
{
mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %Y into Integer", y);
}
mrb_value
mrb_int_pow(mrb_state *mrb, mrb_value x, mrb_value y)
{
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
#ifndef MRB_NO_FLOAT
if (mrb_float_p(y)) {
return mrb_float_value(mrb, pow(mrb_bint_as_float(mrb, x), mrb_float(y)));
}
#endif
return mrb_bint_pow(mrb, x, y);
}
#endif
mrb_int base = mrb_integer(x);
mrb_int result = 1;
mrb_int exp;
#ifndef MRB_NO_FLOAT
if (mrb_float_p(y)) {
return mrb_float_value(mrb, pow((double)base, mrb_float(y)));
}
else if (mrb_integer_p(y)) {
exp = mrb_integer(y);
}
else
#endif
{
mrb_get_args(mrb, "i", &exp);
}
if (exp < 0) {
#ifndef MRB_NO_FLOAT
return mrb_float_value(mrb, pow((double)base, (double)exp));
#else
mrb_int_overflow(mrb, "negative power");
#endif
}
for (;;) {
if (exp & 1) {
if (mrb_int_mul_overflow(result, base, &result)) {
#ifdef MRB_USE_BIGINT
return mrb_bint_pow(mrb, mrb_bint_new_int(mrb, mrb_integer(x)), y);
#else
mrb_int_overflow(mrb, "power");
#endif
}
}
exp >>= 1;
if (exp == 0) break;
if (mrb_int_mul_overflow(base, base, &base)) {
#ifdef MRB_USE_BIGINT
return mrb_bint_pow(mrb, mrb_bint_new_int(mrb, mrb_integer(x)), y);
#else
mrb_int_overflow(mrb, "power");
#endif
}
}
return mrb_int_value(mrb, result);
}
/*
* call-seq:
*
* num ** other -> num
*
* Raises <code>num</code> the <code>other</code> power.
*
* 2.0**3 #=> 8.0
*/
static mrb_value
int_pow(mrb_state *mrb, mrb_value x)
{
return mrb_int_pow(mrb, x, mrb_get_arg1(mrb));
}
mrb_int
mrb_div_int(mrb_int x, mrb_int y)
{
mrb_int div = x / y;
if ((x ^ y) < 0 && x != div * y) {
div -= 1;
}
return div;
}
mrb_value
mrb_div_int_value(mrb_state *mrb, mrb_int x, mrb_int y)
{
if (y == 0) {
mrb_int_zerodiv(mrb);
}
else if(x == MRB_INT_MIN && y == -1) {
#ifdef MRB_USE_BIGINT
return mrb_bint_mul_ii(mrb, x, y);
#else
mrb_int_overflow(mrb, "division");
#endif
}
return mrb_int_value(mrb, mrb_div_int(x, y));
}
/* 15.2.8.3.4 */
/* 15.2.9.3.4 */
/*
* call-seq:
* int / other -> num
*
* Performs division: the class of the resulting object depends on
* the class of <code>num</code> and on the magnitude of the
* result.
*/
static mrb_value
int_div(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_div(mrb, x, y);
}
#endif
mrb_int a = mrb_integer(x);
if (mrb_integer_p(y)) {
return mrb_div_int_value(mrb, a, mrb_integer(y));
}
switch (mrb_type(y)) {
#ifdef MRB_USE_BIGINT
case MRB_TT_BIGINT:
return mrb_bint_div(mrb, mrb_bint_new_int(mrb, a), y);
#endif
#ifdef MRB_USE_RATIONAL
case MRB_TT_RATIONAL:
return mrb_rational_div(mrb, mrb_rational_new(mrb, a, 1), y);
#endif
#ifdef MRB_USE_COMPLEX
case MRB_TT_COMPLEX:
x = mrb_complex_new(mrb, (mrb_float)a, 0);
return mrb_complex_div(mrb, x, y);
#endif
#ifndef MRB_NO_FLOAT
case MRB_TT_FLOAT:
return mrb_float_value(mrb, mrb_div_float((mrb_float)a, mrb_as_float(mrb, y)));
#endif
default:
mrb_int_noconv(mrb, y);
}
}
/* 15.2.9.3.19(x) */
/*
* call-seq:
* num.quo(numeric) -> real
*
* Returns most exact division.
*/
/*
* call-seq:
* int.div(other) -> int
*
* Performs division: resulting integer.
*/
static mrb_value
int_idiv(mrb_state *mrb, mrb_value x)
{
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bint_div(mrb, x, mrb_get_arg1(mrb));
}
#endif
mrb_int y;
mrb_get_args(mrb, "i", &y);
return mrb_div_int_value(mrb, mrb_integer(x), y);
}
static mrb_value
int_quo(mrb_state *mrb, mrb_value x)
{
#ifndef MRB_USE_RATIONAL
#ifdef MRB_NO_FLOAT
return int_idiv(mrb, x);
#else
mrb_float y;
mrb_get_args(mrb, "f", &y);
if (y == 0) {
mrb_int_zerodiv(mrb);
}
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_float_value(mrb, mrb_bint_as_float(mrb, x) / y);
}
#endif
return mrb_float_value(mrb, mrb_integer(x) / y);
#endif
#else
mrb_int a = mrb_integer(x);
mrb_value y = mrb_get_arg1(mrb);
if (mrb_integer_p(y) && mrb_class_defined_id(mrb, MRB_SYM(Rational))) {
return mrb_rational_new(mrb, a, mrb_integer(y));
}
switch (mrb_type(y)) {
case MRB_TT_RATIONAL:
x = mrb_rational_new(mrb, a, 1);
return mrb_rational_div(mrb, x, y);
default:
#ifndef MRB_NO_FLOAT
return mrb_float_value(mrb, mrb_div_float((mrb_float)a, mrb_as_float(mrb, y)));
#else
mrb_int_noconv(mrb, y);
break;
#endif
}
#endif
}
static mrb_value
coerce_step_counter(mrb_state *mrb, mrb_value self)
{
mrb_value num, step;
mrb_get_args(mrb, "oo", &num, &step);
#ifndef MRB_NO_FLOAT
mrb->c->ci->mid = 0;
if (mrb_float_p(num) || mrb_float_p(step)) {
return mrb_ensure_float_type(mrb, self);
}
#endif
return self;
}
#ifndef MRB_NO_FLOAT
/********************************************************************
*
* Document-class: Float
*
* <code>Float</code> objects represent inexact real numbers using
* the native architecture's double-precision floating-point
* representation.
*/
static mrb_value
flo_pow(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
mrb_float d = pow(mrb_as_float(mrb, x), mrb_as_float(mrb, y));
return mrb_float_value(mrb, d);
}
static mrb_value
flo_idiv(mrb_state *mrb, mrb_value xv)
{
mrb_int y;
mrb_get_args(mrb, "i", &y);
return mrb_div_int_value(mrb, (mrb_int)mrb_float(xv), y);
}
mrb_float
mrb_div_float(mrb_float x, mrb_float y)
{
if (y != 0.0) {
return x / y;
}
else if (x == 0.0) {
return NAN;
}
else {
return x * (signbit(y) ? -1.0 : 1.0) * INFINITY;
}
}
static mrb_value
flo_div(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
mrb_float a = mrb_float(x);
switch(mrb_type(y)) {
#ifdef MRB_USE_COMPLEX
case MRB_TT_COMPLEX:
return mrb_complex_div(mrb, mrb_complex_new(mrb, a, 0), y);
#endif
case MRB_TT_FLOAT:
a = mrb_div_float(a, mrb_float(y));
return mrb_float_value(mrb, a);
default:
a = mrb_div_float(a, mrb_as_float(mrb, y));
return mrb_float_value(mrb, a);
}
return mrb_float_value(mrb, a);
}
/* the argument `fmt` is no longer used; you can pass `NULL` */
mrb_value
mrb_float_to_str(mrb_state *mrb, mrb_value flo, const char *fmt)
{
char buf[25];
#ifdef MRB_USE_FLOAT32
const int prec = 7;
#else
const int prec = 15;
#endif
mrb_format_float(mrb_float(flo), buf, sizeof(buf), 'g', prec, '\0');
for (char *p = buf; *p; p++) {
if (*p == '.') goto exit;
if (*p == 'e') {
memmove(p+2, p, strlen(p)+1);
memcpy(p, ".0", 2);
goto exit;
}
}
strcat(buf, ".0");
exit:
return mrb_str_new_cstr(mrb, buf);
}
/* 15.2.9.3.16(x) */
/*
* call-seq:
* flt.to_s -> string
* flt.inspect -> string
*
* Returns a string containing a representation of self. As well as a
* fixed or exponential form of the number, the call may return
* "<code>NaN</code>", "<code>Infinity</code>", and
* "<code>-Infinity</code>".
*
* 3.0.to_s #=> 3.0
* 3.25.to_s #=> 3.25
*/
static mrb_value
flo_to_s(mrb_state *mrb, mrb_value flt)
{
mrb_float f = mrb_float(flt);
mrb_value str;
if (isinf(f)) {
str = f < 0 ? mrb_str_new_lit(mrb, "-Infinity")
: mrb_str_new_lit(mrb, "Infinity");
}
else if (isnan(f)) {
str = mrb_str_new_lit(mrb, "NaN");
}
else {
str = mrb_float_to_str(mrb, flt, NULL);
}
RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
return str;
}
/* 15.2.9.3.1 */
/*
* call-seq:
* float + other -> float
*
* Returns a new float which is the sum of <code>float</code>
* and <code>other</code>.
*/
static mrb_value
flo_add(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
mrb_float a = mrb_float(x);
switch (mrb_type(y)) {
case MRB_TT_FLOAT:
return mrb_float_value(mrb, a + mrb_float(y));
#if defined(MRB_USE_COMPLEX)
case MRB_TT_COMPLEX:
return mrb_complex_add(mrb, y, x);
#endif
default:
return mrb_float_value(mrb, a + mrb_as_float(mrb, y));
}
}
/* 15.2.9.3.2 */
/*
* call-seq:
* float - other -> float
*
* Returns a new float which is the difference of <code>float</code>
* and <code>other</code>.
*/
static mrb_value
flo_sub(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
mrb_float a = mrb_float(x);
switch (mrb_type(y)) {
case MRB_TT_FLOAT:
return mrb_float_value(mrb, a - mrb_float(y));
#if defined(MRB_USE_COMPLEX)
case MRB_TT_COMPLEX:
return mrb_complex_sub(mrb, mrb_complex_new(mrb, a, 0), y);
#endif
default:
return mrb_float_value(mrb, a - mrb_as_float(mrb, y));
}
}
/* 15.2.9.3.3 */
/*
* call-seq:
* float * other -> float
*
* Returns a new float which is the product of <code>float</code>
* and <code>other</code>.
*/
static mrb_value
flo_mul(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
mrb_float a = mrb_float(x);
switch (mrb_type(y)) {
case MRB_TT_FLOAT:
return mrb_float_value(mrb, a * mrb_float(y));
#if defined(MRB_USE_COMPLEX)
case MRB_TT_COMPLEX:
return mrb_complex_mul(mrb, y, x);
#endif
default:
return mrb_float_value(mrb, a * mrb_as_float(mrb, y));
}
}
static void
flodivmod(mrb_state *mrb, double x, double y, mrb_float *divp, mrb_float *modp)
{
double div, mod;
if (isnan(y)) {
/* y is NaN so all results are NaN */
div = mod = y;
goto exit;
}
if (y == 0.0) {
mrb_int_zerodiv(mrb);
}
if (isinf(y) && !isinf(x)) {
mod = x;
}
else {
mod = fmod(x, y);
}
if (isinf(x) && !isinf(y)) {
div = x;
}
else {
div = (x - mod) / y;
if (modp && divp) div = round(div);
}
if (div == 0) div = 0.0;
if (mod == 0) mod = 0.0;
if (y*mod < 0) {
mod += y;
div -= 1.0;
}
exit:
if (modp) *modp = mod;
if (divp) *divp = div;
}
/* 15.2.9.3.5 */
/*
* call-seq:
* flt % other -> float
* flt.modulo(other) -> float
*
* Return the modulo after division of <code>flt</code> by <code>other</code>.
*
* 6543.21.modulo(137) #=> 104.21
* 6543.21.modulo(137.24) #=> 92.9299999999996
*/
static mrb_value
flo_mod(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
mrb_float mod;
flodivmod(mrb, mrb_float(x), mrb_as_float(mrb, y), 0, &mod);
return mrb_float_value(mrb, mod);
}
#endif
/* 15.2.8.3.16 */
/*
* call-seq:
* num.eql?(numeric) -> true or false
*
* Returns <code>true</code> if <i>num</i> and <i>numeric</i> are the
* same type and have equal values.
*
* 1 == 1.0 #=> true
* 1.eql?(1.0) #=> false
* (1.0).eql?(1.0) #=> true
*/
static mrb_value
num_eql(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
#ifdef MRB_USE_BIGINT
if (mrb_bigint_p(x)) {
return mrb_bool_value(mrb_bint_cmp(mrb, x, y) == 0);
}
#endif
#ifndef MRB_NO_FLOAT
if (mrb_float_p(x)) {
if (!mrb_float_p(y)) return mrb_false_value();
return mrb_bool_value(mrb_float(x) == mrb_float(y));
}
#endif
if (mrb_integer_p(x)) {
if (!mrb_integer_p(y)) return mrb_false_value();
return mrb_bool_value(mrb_integer(x) == mrb_integer(y));
}
return mrb_bool_value(mrb_equal(mrb, x, y));
}
#ifndef MRB_NO_FLOAT
/* 15.2.9.3.7 */
/*
* call-seq:
* flt == obj -> true or false
*
* Returns <code>true</code> only if <i>obj</i> has the same value
* as <i>flt</i>. Contrast this with <code>Float#eql?</code>, which
* requires <i>obj</i> to be a <code>Float</code>.
*
* 1.0 == 1 #=> true
*
*/
static mrb_value
flo_eq(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
switch (mrb_type(y)) {
case MRB_TT_INTEGER:
return mrb_bool_value(mrb_float(x) == (mrb_float)mrb_integer(y));
case MRB_TT_FLOAT:
return mrb_bool_value(mrb_float(x) == mrb_float(y));
#ifdef MRB_USE_RATIONAL
case MRB_TT_RATIONAL:
return mrb_bool_value(mrb_float(x) == mrb_as_float(mrb, y));
#endif
#ifdef MRB_USE_COMPLEX
case MRB_TT_COMPLEX:
return mrb_bool_value(mrb_equal(mrb, y, x));
#endif
default:
return mrb_false_value();
}
}
static int64_t
value_int64(mrb_state *mrb, mrb_value x)
{
switch (mrb_type(x)) {
case MRB_TT_INTEGER:
return (int64_t)mrb_integer(x);
case MRB_TT_FLOAT:
{
double f = mrb_float(x);
if ((mrb_float)INT64_MAX >= f && f >= (mrb_float)INT64_MIN)
return (int64_t)f;
}
default:
mrb_raise(mrb, E_TYPE_ERROR, "cannot convert to Integer");
break;
}
/* not reached */
return 0;
}
static mrb_value
int64_value(mrb_state *mrb, int64_t v)
{
if (!TYPED_FIXABLE(v,int64_t)) {
mrb_int_overflow(mrb, "bit operation");
}
return mrb_fixnum_value((mrb_int)v);
}
static mrb_value
flo_rev(mrb_state *mrb, mrb_value x)
{
int64_t v1 = value_int64(mrb, x);
return int64_value(mrb, ~v1);
}
static mrb_value
flo_and(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
int64_t v1, v2;
v1 = value_int64(mrb, x);
v2 = value_int64(mrb, y);
return int64_value(mrb, v1 & v2);
}
static mrb_value
flo_or(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
int64_t v1, v2;
v1 = value_int64(mrb, x);
v2 = value_int64(mrb, y);
return int64_value(mrb, v1 | v2);
}
static mrb_value
flo_xor(mrb_state *mrb, mrb_value x)
{
mrb_value y = mrb_get_arg1(mrb);
int64_t v1, v2;
v1 = value_int64(mrb, x);
v2 = value_int64(mrb, y);
return int64_value(mrb, v1 ^ v2);
}
static mrb_value
flo_shift(mrb_state *mrb, mrb_value x, mrb_int width)
{
mrb_float val;
if (width == 0) {
return x;
}
val = mrb_float(x);
if (width < -MRB_INT_BIT/2) {
if (val < 0) return mrb_fixnum_value(-1);
return mrb_fixnum_value(0);
}
if (width < 0) {
while (width++) {
val /= 2;
if (val < 1.0) {
val = 0;
break;
}
}
#if defined(_ISOC99_SOURCE)
val = trunc(val);
#else
if (val > 0){
val = floor(val);
} else {
val = ceil(val);
}
#endif
if (val == 0 && mrb_float(x) < 0) {
return mrb_fixnum_value(-1);
}
}
else {
while (width--) {
val *= 2;
}
}
if (FIXABLE_FLOAT(val))
return mrb_int_value(mrb, (mrb_int)val);
return mrb_float_value(mrb, val);
}
static mrb_value
flo_rshift(mrb_state *mrb, mrb_value x)
{
mrb_int width;
mrb_get_args(mrb, "i", &width);
if (width == MRB_INT_MIN) return flo_shift(mrb, x, -MRB_INT_BIT);
return flo_shift(mrb, x, -width);
}
static mrb_value
flo_lshift(mrb_state *mrb, mrb_value x)
{
mrb_int width;
mrb_get_args(mrb, "i", &width);
return flo_shift(mrb, x, width);
}
/* 15.2.9.3.13 */
/*
* call-seq:
* flt.to_f -> self
*
* As <code>flt</code> is already a float, returns +self+.
*/
static mrb_value
flo_to_f(mrb_state *mrb, mrb_value num)
{
return num;
}
/* 15.2.9.3.11 */
/*
* call-seq:
* flt.infinite? -> nil, -1, +1
*
* Returns <code>nil</code>, -1, or +1 depending on whether <i>flt</i>
* is finite, -infinity, or +infinity.
*
* (0.0).infinite? #=> nil
* (-1.0/0.0).infinite? #=> -1
* (+1.0/0.0).infinite? #=> 1
*/
static mrb_value
flo_infinite_p(mrb_state *mrb, mrb_value num)
{
mrb_float value = mrb_float(num);
if (isinf(value)) {
return mrb_fixnum_value(value < 0 ? -1 : 1);
}
return mrb_nil_value();
}
/* 15.2.9.3.9 */
/*
* call-seq:
* flt.finite? -> true or false
*
* Returns <code>true</code> if <i>flt</i> is a valid IEEE floating
* point number (it is not infinite, and <code>nan?</code> is
* <code>false</code>).
*
*/
static mrb_value
flo_finite_p(mrb_state *mrb, mrb_value num)
{
return mrb_bool_value(isfinite(mrb_float(num)));
}
/*
* Document-class: FloatDomainError
*
* Raised when attempting to convert special float values
* (in particular infinite or NaN)
* to numerical classes which don't support them.
*
* Float::INFINITY.to_i
*
* <em>raises the exception:</em>
*
* FloatDomainError: Infinity
*/
/* ------------------------------------------------------------------------*/
void
mrb_check_num_exact(mrb_state *mrb, mrb_float num)
{
if (isinf(num)) {
mrb_raise(mrb, E_FLOATDOMAIN_ERROR, num < 0 ? "-Infinity" : "Infinity");
}
if (isnan(num)) {
mrb_raise(mrb, E_FLOATDOMAIN_ERROR, "NaN");
}
}
static mrb_value
flo_rounding_int(mrb_state *mrb, mrb_float f)
{
if (!FIXABLE_FLOAT(f)) {
#ifdef MRB_USE_BIGINT
return mrb_bint_new_float(mrb, f);
#else
mrb_int_overflow(mrb, "rounding");
#endif
}
return mrb_int_value(mrb, (mrb_int)f);
}
static mrb_value
flo_rounding(mrb_state *mrb, mrb_value num, double (*func)(double))
{
mrb_float f = mrb_float(num);
mrb_int ndigits = 0;
#ifdef MRB_USE_FLOAT32
const int fprec = 7;
#else
const int fprec = 15;
#endif
mrb_get_args(mrb, "|i", &ndigits);
if (f == 0.0) {
return ndigits > 0 ? mrb_float_value(mrb, f) : mrb_fixnum_value(0);
}
if (ndigits > 0) {
if (ndigits > fprec) return num;
mrb_float d = pow(10, (double)ndigits);
f = func(f * d) / d;
mrb_check_num_exact(mrb, f);
return mrb_float_value(mrb, f);
}
if (ndigits < 0) {
mrb_float d = pow(10, -(double)ndigits);
f = func(f / d) * d;
}
else { /* ndigits == 0 */
f = func(f);
}
mrb_check_num_exact(mrb, f);
return flo_rounding_int(mrb, f);
}
/* 15.2.9.3.10 */
/*
* call-seq:
* float.floor([ndigits]) -> integer or float
*
* Returns the largest number less than or equal to +float+ with
* a precision of +ndigits+ decimal digits (default: 0).
*
* When the precision is negative, the returned value is an integer
* with at least <code>ndigits.abs</code> trailing zeros.
*
* Returns a floating point number when +ndigits+ is positive,
* otherwise returns an integer.
*
* 1.2.floor #=> 1
* 2.0.floor #=> 2
* (-1.2).floor #=> -2
* (-2.0).floor #=> -2
*
* 1.234567.floor(2) #=> 1.23
* 1.234567.floor(3) #=> 1.234
* 1.234567.floor(4) #=> 1.2345
* 1.234567.floor(5) #=> 1.23456
*
* 34567.89.floor(-5) #=> 0
* 34567.89.floor(-4) #=> 30000
* 34567.89.floor(-3) #=> 34000
* 34567.89.floor(-2) #=> 34500
* 34567.89.floor(-1) #=> 34560
* 34567.89.floor(0) #=> 34567
* 34567.89.floor(1) #=> 34567.8
* 34567.89.floor(2) #=> 34567.89
* 34567.89.floor(3) #=> 34567.89
*
* Note that the limited precision of floating point arithmetic
* might lead to surprising results:
*
* (0.3 / 0.1).floor #=> 2 (!)
*/
static mrb_value
flo_floor(mrb_state *mrb, mrb_value num)
{
return flo_rounding(mrb, num, floor);
}
/* 15.2.9.3.8 */
/*
* call-seq:
* float.ceil([ndigits]) -> integer or float
*
* Returns the smallest number greater than or equal to +float+ with
* a precision of +ndigits+ decimal digits (default: 0).
*
* When the precision is negative, the returned value is an integer
* with at least <code>ndigits.abs</code> trailing zeros.
*
* Returns a floating point number when +ndigits+ is positive,
* otherwise returns an integer.
*
* 1.2.ceil #=> 2
* 2.0.ceil #=> 2
* (-1.2).ceil #=> -1
* (-2.0).ceil #=> -2
*
* 1.234567.ceil(2) #=> 1.24
* 1.234567.ceil(3) #=> 1.235
* 1.234567.ceil(4) #=> 1.2346
* 1.234567.ceil(5) #=> 1.23457
*
* 34567.89.ceil(-5) #=> 100000
* 34567.89.ceil(-4) #=> 40000
* 34567.89.ceil(-3) #=> 35000
* 34567.89.ceil(-2) #=> 34600
* 34567.89.ceil(-1) #=> 34570
* 34567.89.ceil(0) #=> 34568
* 34567.89.ceil(1) #=> 34567.9
* 34567.89.ceil(2) #=> 34567.89
* 34567.89.ceil(3) #=> 34567.89
*
* Note that the limited precision of floating point arithmetic
* might lead to surprising results:
*
* (2.1 / 0.7).ceil #=> 4 (!)
*/
static mrb_value
flo_ceil(mrb_state *mrb, mrb_value num)
{
return flo_rounding(mrb, num, ceil);
}
/* 15.2.9.3.12 */
/*
* call-seq:
* flt.round([ndigits]) -> integer or float
*
* Rounds <i>flt</i> to a given precision in decimal digits (default 0 digits).
* Precision may be negative. Returns a floating-point number when ndigits
* is more than zero.
*
* 1.4.round #=> 1
* 1.5.round #=> 2
* 1.6.round #=> 2
* (-1.5).round #=> -2
*
* 1.234567.round(2) #=> 1.23
* 1.234567.round(3) #=> 1.235
* 1.234567.round(4) #=> 1.2346
* 1.234567.round(5) #=> 1.23457
*
* 34567.89.round(-5) #=> 0
* 34567.89.round(-4) #=> 30000
* 34567.89.round(-3) #=> 35000
* 34567.89.round(-2) #=> 34600
* 34567.89.round(-1) #=> 34570
* 34567.89.round(0) #=> 34568
* 34567.89.round(1) #=> 34567.9
* 34567.89.round(2) #=> 34567.89
* 34567.89.round(3) #=> 34567.89
*
*/
static mrb_value
flo_round(mrb_state *mrb, mrb_value num)
{
double number, f;
mrb_int ndigits = 0;
mrb_int i;
mrb_get_args(mrb, "|i", &ndigits);
number = mrb_float(num);