Project

General

Profile

Feature #10474 ยป combine_some_macros_into_one_at_math.c.patch

gogotanaka (Kazuki Tanaka), 11/04/2014 05:46 AM

View differences:

math.c
25 25
VALUE rb_mMath;
26 26
VALUE rb_eMathDomainError;
27 27

  
28
#define Need_Float(x) do {if (!RB_TYPE_P(x, T_FLOAT)) {(x) = rb_to_float(x);}} while(0)
29
#define Need_Float2(x,y) do {\
30
    Need_Float(x);\
31
    Need_Float(y);\
32
} while (0)
28
#define Get_RFloat_Value(x) RFLOAT_VALUE(RB_TYPE_P(x, T_FLOAT) ? x : rb_to_float(x))
33 29

  
34 30
#define domain_error(msg) \
35 31
    rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
......
69 65
# define M_PI 3.14159265358979323846
70 66
#endif
71 67
    double dx, dy;
72
    Need_Float2(y, x);
73
    dx = RFLOAT_VALUE(x);
74
    dy = RFLOAT_VALUE(y);
68
    dx = Get_RFloat_Value(x);
69
    dy = Get_RFloat_Value(y);
75 70
    if (dx == 0.0 && dy == 0.0) {
76 71
	if (!signbit(dx))
77 72
	    return DBL2NUM(dy);
......
114 109
static VALUE
115 110
math_cos(VALUE obj, VALUE x)
116 111
{
117
    Need_Float(x);
118
    return DBL2NUM(cos(RFLOAT_VALUE(x)));
112
    return DBL2NUM(cos(Get_RFloat_Value(x)));
119 113
}
120 114

  
121 115
/*
......
136 130
static VALUE
137 131
math_sin(VALUE obj, VALUE x)
138 132
{
139
    Need_Float(x);
140
    return DBL2NUM(sin(RFLOAT_VALUE(x)));
133
    return DBL2NUM(sin(Get_RFloat_Value(x)));
141 134
}
142 135

  
143 136

  
......
158 151
static VALUE
159 152
math_tan(VALUE obj, VALUE x)
160 153
{
161
    Need_Float(x);
162
    return DBL2NUM(tan(RFLOAT_VALUE(x)));
154
    return DBL2NUM(tan(Get_RFloat_Value(x)));
163 155
}
164 156

  
165 157
/*
......
181 173
{
182 174
    double d0, d;
183 175

  
184
    Need_Float(x);
185
    d0 = RFLOAT_VALUE(x);
176
    d0 = Get_RFloat_Value(x);
186 177
    /* check for domain error */
187 178
    if (d0 < -1.0 || 1.0 < d0) domain_error("acos");
188 179
    d = acos(d0);
......
207 198
{
208 199
    double d0, d;
209 200

  
210
    Need_Float(x);
211
    d0 = RFLOAT_VALUE(x);
201
    d0 = Get_RFloat_Value(x);
212 202
    /* check for domain error */
213 203
    if (d0 < -1.0 || 1.0 < d0) domain_error("asin");
214 204
    d = asin(d0);
......
231 221
static VALUE
232 222
math_atan(VALUE obj, VALUE x)
233 223
{
234
    Need_Float(x);
235
    return DBL2NUM(atan(RFLOAT_VALUE(x)));
224
    return DBL2NUM(atan(Get_RFloat_Value(x)));
236 225
}
237 226

  
238 227
#ifndef HAVE_COSH
......
260 249
static VALUE
261 250
math_cosh(VALUE obj, VALUE x)
262 251
{
263
    Need_Float(x);
264
    return DBL2NUM(cosh(RFLOAT_VALUE(x)));
252
    return DBL2NUM(cosh(Get_RFloat_Value(x)));
265 253
}
266 254

  
267 255
#ifndef HAVE_SINH
......
289 277
static VALUE
290 278
math_sinh(VALUE obj, VALUE x)
291 279
{
292
    Need_Float(x);
293
    return DBL2NUM(sinh(RFLOAT_VALUE(x)));
280
    return DBL2NUM(sinh(Get_RFloat_Value(x)));
294 281
}
295 282

  
296 283
#ifndef HAVE_TANH
......
318 305
static VALUE
319 306
math_tanh(VALUE obj, VALUE x)
320 307
{
321
    Need_Float(x);
322
    return DBL2NUM(tanh(RFLOAT_VALUE(x)));
308
    return DBL2NUM(tanh(Get_RFloat_Value(x)));
323 309
}
324 310

  
325 311
/*
......
341 327
{
342 328
    double d0, d;
343 329

  
344
    Need_Float(x);
345
    d0 = RFLOAT_VALUE(x);
330
    d0 = Get_RFloat_Value(x);
346 331
    /* check for domain error */
347 332
    if (d0 < 1.0) domain_error("acosh");
348 333
    d = acosh(d0);
......
366 351
static VALUE
367 352
math_asinh(VALUE obj, VALUE x)
368 353
{
369
    Need_Float(x);
370
    return DBL2NUM(asinh(RFLOAT_VALUE(x)));
354
    return DBL2NUM(asinh(Get_RFloat_Value(x)));
371 355
}
372 356

  
373 357
/*
......
389 373
{
390 374
    double d0, d;
391 375

  
392
    Need_Float(x);
393
    d0 = RFLOAT_VALUE(x);
376
    d0 = Get_RFloat_Value(x);
394 377
    /* check for domain error */
395 378
    if (d0 <  -1.0 || +1.0 <  d0) domain_error("atanh");
396 379
    /* check for pole error */
......
419 402
static VALUE
420 403
math_exp(VALUE obj, VALUE x)
421 404
{
422
    Need_Float(x);
423
    return DBL2NUM(exp(RFLOAT_VALUE(x)));
405
    return DBL2NUM(exp(Get_RFloat_Value(x)));
424 406
}
425 407

  
426 408
#if defined __CYGWIN__
......
484 466
	numbits = 0;
485 467
    }
486 468

  
487
    Need_Float(x);
488
    d0 = RFLOAT_VALUE(x);
469
    d0 = Get_RFloat_Value(x);
489 470
    /* check for domain error */
490 471
    if (d0 < 0.0) domain_error("log");
491 472
    /* check for pole error */
......
540 521
	numbits = 0;
541 522
    }
542 523

  
543
    Need_Float(x);
544
    d0 = RFLOAT_VALUE(x);
524
    d0 = Get_RFloat_Value(x);
545 525
    /* check for domain error */
546 526
    if (d0 < 0.0) domain_error("log2");
547 527
    /* check for pole error */
......
582 562
	numbits = 0;
583 563
    }
584 564

  
585
    Need_Float(x);
586
    d0 = RFLOAT_VALUE(x);
565
    d0 = Get_RFloat_Value(x);
587 566
    /* check for domain error */
588 567
    if (d0 < 0.0) domain_error("log10");
589 568
    /* check for pole error */
......
625 604
{
626 605
    double d0, d;
627 606

  
628
    Need_Float(x);
629
    d0 = RFLOAT_VALUE(x);
607
    d0 = Get_RFloat_Value(x);
630 608
    /* check for domain error */
631 609
    if (d0 < 0.0) domain_error("sqrt");
632 610
    if (d0 == 0.0) return DBL2NUM(0.0);
......
672 650
static VALUE
673 651
math_cbrt(VALUE obj, VALUE x)
674 652
{
675
    Need_Float(x);
676
    return DBL2NUM(cbrt(RFLOAT_VALUE(x)));
653
    return DBL2NUM(cbrt(Get_RFloat_Value(x)));
677 654
}
678 655

  
679 656
/*
......
693 670
    double d;
694 671
    int exp;
695 672

  
696
    Need_Float(x);
697

  
698
    d = frexp(RFLOAT_VALUE(x), &exp);
673
    d = frexp(Get_RFloat_Value(x), &exp);
699 674
    return rb_assoc_new(DBL2NUM(d), INT2NUM(exp));
700 675
}
701 676

  
......
712 687
static VALUE
713 688
math_ldexp(VALUE obj, VALUE x, VALUE n)
714 689
{
715
    Need_Float(x);
716
    return DBL2NUM(ldexp(RFLOAT_VALUE(x), NUM2INT(n)));
690
    return DBL2NUM(ldexp(Get_RFloat_Value(x), NUM2INT(n)));
717 691
}
718 692

  
719 693
/*
......
729 703
static VALUE
730 704
math_hypot(VALUE obj, VALUE x, VALUE y)
731 705
{
732
    Need_Float2(x, y);
733
    return DBL2NUM(hypot(RFLOAT_VALUE(x), RFLOAT_VALUE(y)));
706
    return DBL2NUM(hypot(Get_RFloat_Value(x), Get_RFloat_Value(y)));
734 707
}
735 708

  
736 709
/*
......
750 723
static VALUE
751 724
math_erf(VALUE obj, VALUE x)
752 725
{
753
    Need_Float(x);
754
    return DBL2NUM(erf(RFLOAT_VALUE(x)));
726
    return DBL2NUM(erf(Get_RFloat_Value(x)));
755 727
}
756 728

  
757 729
/*
......
771 743
static VALUE
772 744
math_erfc(VALUE obj, VALUE x)
773 745
{
774
    Need_Float(x);
775
    return DBL2NUM(erfc(RFLOAT_VALUE(x)));
746
    return DBL2NUM(erfc(Get_RFloat_Value(x)));
776 747
}
777 748

  
778 749
/*
......
848 819
    };
849 820
    double d0, d;
850 821
    double intpart, fracpart;
851
    Need_Float(x);
852
    d0 = RFLOAT_VALUE(x);
822
    d0 = Get_RFloat_Value(x);
853 823
    /* check for domain error */
854 824
    if (isinf(d0) && signbit(d0)) domain_error("gamma");
855 825
    fracpart = modf(d0, &intpart);
......
884 854
    double d0, d;
885 855
    int sign=1;
886 856
    VALUE v;
887
    Need_Float(x);
888
    d0 = RFLOAT_VALUE(x);
857
    d0 = Get_RFloat_Value(x);
889 858
    /* check for domain error */
890 859
    if (isinf(d0)) {
891 860
	if (signbit(d0)) domain_error("lgamma");
892
-