Home | History | Annotate | Download | only in tests
      1 
      2 /* Program to check that the FP stuff underlying these common FP
      3    functions isn't too badly broken.  Carefully kludged to print the
      4    same answers on different platforms (even when run natively). */
      5 
      6 #include <stdio.h>
      7 #include <math.h>
      8 
      9 int main ( void )
     10 {
     11    double d;
     12    float f;
     13    int i;
     14 
     15    const double tinyD = 0.0000000001;
     16    const double tinyF = 0.0001;
     17 
     18    /* -------------------- any arg -------------------- */
     19 
     20    d = -2.0;
     21    for (i = 0; i < 41; i++) {
     22       printf("floorD(%+20.13e) = %+20.13e\n", d, floor(d));
     23       d += 0.1-tinyD;
     24    }
     25    f = -2.0;
     26    for (i = 0; i < 41; i++) {
     27       printf("floorF(%+20.4e) = %+20.4e\n", (double)f, (double)floorf(f));
     28       f += 0.1-tinyF;
     29    }
     30 
     31 
     32    d = -2.0;
     33    for (i = 0; i < 41; i++) {
     34       printf(" ceilD(%+20.13e) = %+20.13e\n", d, ceil(d));
     35       d += 0.1-tinyD;
     36    }
     37    f = -2.0;
     38    for (i = 0; i < 41; i++) {
     39       printf(" ceilF(%+20.4e) = %+20.4e\n", (double)f, (double)ceilf(f));
     40       f += 0.1-tinyF;
     41    }
     42 
     43 
     44    d = -2.0;
     45    for (i = 0; i < 41; i++) {
     46       printf("  sinD(%+20.13e) = %+20.13e\n", d, sin(d));
     47       d += 0.1-tinyD;
     48    }
     49    f = -2.0;
     50    for (i = 0; i < 41; i++) {
     51       printf("  sinF(%+20.4e) = %+20.4e\n", (double)f, (double)sinf(f));
     52       f += 0.1-tinyF;
     53    }
     54 
     55 
     56    d = -2.0;
     57    for (i = 0; i < 41; i++) {
     58       printf("  cosD(%+20.13e) = %+20.13e\n", d, cos(d));
     59       d += 0.1-tinyD;
     60    }
     61    f = -2.0;
     62    for (i = 0; i < 41; i++) {
     63       printf("  cosF(%+20.4e) = %+20.4e\n", (double)f, (double)cosf(f));
     64       f += 0.1-tinyF;
     65    }
     66 
     67 
     68    d = -2.0;
     69    for (i = 0; i < 41; i++) {
     70       printf("  tanD(%+20.13e) = %+20.13e\n", d, tan(d));
     71       d += 0.1-tinyD;
     72    }
     73    f = -2.0;
     74    for (i = 0; i < 41; i++) {
     75       printf("  tanF(%+20.4e) = %+20.4e\n", (double)f, (double)tanf(f));
     76       f += 0.1-tinyF;
     77    }
     78 
     79 
     80    d = -2.0;
     81    for (i = 0; i < 41; i++) {
     82       printf("  expD(%+20.13e) = %+20.13e\n", d, exp(d));
     83       d += 0.1-tinyD;
     84    }
     85    f = -2.0;
     86    for (i = 0; i < 41; i++) {
     87       printf("  expF(%+20.4e) = %+20.4e\n", (double)f, (double)expf(f));
     88       f += 0.1-tinyF;
     89    }
     90 
     91    /* -------------------- >= 0 arg -------------------- */
     92 
     93    d = 0.0;
     94    for (i = 0; i < 21; i++) {
     95       printf(" sqrtD(%+20.13e) = %+20.13e\n", d, sqrt(d));
     96       d += 0.1-tinyD;
     97    }
     98    f = 0.0;
     99    for (i = 0; i < 21; i++) {
    100       printf(" sqrtF(%+20.4e) = %+20.4e\n", (double)f, (double)sqrtf(f));
    101       f += 0.1-tinyF;
    102    }
    103 
    104 
    105    d = 0.0;
    106    for (i = 0; i < 21; i++) {
    107       printf("  logD(%+20.13e) = %+20.13e\n", d, log(d));
    108       d += 0.1-tinyD;
    109    }
    110    f = 0.0;
    111    for (i = 0; i < 21; i++) {
    112       printf("  logF(%+20.4e) = %+20.4e\n", (double)f, (double)logf(f));
    113       f += 0.1-tinyF;
    114    }
    115 
    116 
    117    d = 0.0;
    118    for (i = 0; i < 21; i++) {
    119       printf("log10D(%+20.13e) = %+20.13e\n", d, log10(d));
    120       d += 0.1-tinyD;
    121    }
    122    f = 0.0;
    123    for (i = 0; i < 21; i++) {
    124       printf("log10F(%+20.4e) = %+20.4e\n", (double)f, (double)log10f(f));
    125       f += 0.1-tinyF;
    126    }
    127 
    128    /* -------------------- -1 .. +1 arg -------------------- */
    129 
    130    d = -1.0;
    131    for (i = 0; i < 21; i++) {
    132       printf(" asinD(%+20.13e) = %+20.13e\n", d, asin(d));
    133       d += 0.1-tinyD;
    134    }
    135    f = -1.0;
    136    for (i = 0; i < 21; i++) {
    137       printf(" asinF(%+20.4e) = %+20.4e\n", (double)f, (double)asinf(f));
    138       f += 0.1-tinyF;
    139    }
    140 
    141    /* acos(double) seems very prone to accuracy loss near the end of
    142       the range (arg --> +1.0).  Hence is different from the rest to
    143       stop it getting so close to 1.0. */
    144    d = -1.0;
    145    for (i = 0; i < 21; i++) {
    146       printf(" acosD(%+20.13e) = %+20.10e\n", d, acos(d));
    147       d += 0.1 - 1000.0*tinyD;
    148    }
    149    f = -1.0;
    150    for (i = 0; i < 21; i++) {
    151       printf(" acosF(%+20.4e) = %+20.4e\n", (double)f, (double)acosf(f));
    152       f += 0.1-tinyF;
    153    }
    154 
    155 
    156    d = -1.0;
    157    for (i = 0; i < 21; i++) {
    158       printf(" atanD(%+20.13e) = %+20.13e\n", d, atan(d));
    159       d += 0.1-tinyD;
    160    }
    161    f = -1.0;
    162    for (i = 0; i < 21; i++) {
    163       printf(" atanF(%+20.4e) = %+20.4e\n", (double)f, (double)atanf(f));
    164       f += 0.1-tinyF;
    165    }
    166 
    167 
    168    d = -1.0;
    169    for (i = 0; i < 21; i++) {
    170       printf("atan2D(%+20.13e) = %+20.13e\n", d, atan2(d, 1.0));
    171       d += 0.1-tinyD;
    172    }
    173    f = -1.0;
    174    for (i = 0; i < 21; i++) {
    175       printf("atan2F(%+20.4e) = %+20.4e\n", (double)f, (double)atan2f(f,1.0));
    176       f += 0.1-tinyF;
    177    }
    178 
    179    return 0;
    180 }
    181