[d9be488] | 1 | /*
|
---|
| 2 | * Copyright (c) 2014 Martin Decky
|
---|
[992ffa6] | 3 | * Copyright (c) 2015 Jiri Svoboda
|
---|
[d9be488] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[992ffa6] | 30 | #include <stdbool.h>
|
---|
[d9be488] | 31 | #include <stdio.h>
|
---|
| 32 | #include <stdlib.h>
|
---|
[582a0b8] | 33 | #include <stddef.h>
|
---|
[d9be488] | 34 | #include <math.h>
|
---|
| 35 | #include "../tester.h"
|
---|
| 36 |
|
---|
[992ffa6] | 37 | #define OPERANDS 10
|
---|
| 38 | #define PRECISIONF 10000
|
---|
| 39 | #define PRECISION 100000000
|
---|
[d9be488] | 40 |
|
---|
| 41 | static double arguments[OPERANDS] = {
|
---|
| 42 | 3.5, -2.1, 100.0, 50.0, -1024.0, 0.0, 768.3156, 1080.499999, -600.0, 1.0
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | static double results_cos[OPERANDS] = {
|
---|
| 46 | -0.936456687291, -0.504846104600, 0.862318872288, 0.964966028492,
|
---|
| 47 | 0.987353618220, 1.0, -0.194939922623, 0.978471923925, -0.999023478833,
|
---|
| 48 | 0.540302305868
|
---|
| 49 | };
|
---|
| 50 |
|
---|
[b69786e] | 51 | static double results_sin[OPERANDS] = {
|
---|
| 52 | -0.350783227690, -0.863209366649, -0.506365641110, -0.262374853704,
|
---|
| 53 | 0.158533380044, 0.0, 0.980815184715, -0.206379975025, -0.044182448332,
|
---|
| 54 | 0.841470984808
|
---|
| 55 | };
|
---|
| 56 |
|
---|
| 57 | static double results_trunc[OPERANDS] = {
|
---|
| 58 | 3.0, -2.0, 100.0, 50.0, -1024.0, 0.0, 768.0, 1080.0, -600.0, 1.0
|
---|
| 59 | };
|
---|
| 60 |
|
---|
[992ffa6] | 61 | static bool cmp_float(float a, float b)
|
---|
| 62 | {
|
---|
| 63 | float r;
|
---|
| 64 |
|
---|
| 65 | /* XXX Need fabsf() */
|
---|
| 66 | if (b < 1.0 / PRECISIONF && b > -1.0 / PRECISIONF)
|
---|
| 67 | r = a;
|
---|
| 68 | else
|
---|
| 69 | r = a / b - 1.0;
|
---|
| 70 |
|
---|
| 71 | /* XXX Need fabsf() */
|
---|
| 72 | if (r < 0.0)
|
---|
| 73 | r = -r;
|
---|
| 74 |
|
---|
| 75 | return r < 1.0 / PRECISIONF;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static bool cmp_double(double a, double b)
|
---|
| 79 | {
|
---|
| 80 | double r;
|
---|
| 81 |
|
---|
| 82 | /* XXX Need fabs() */
|
---|
| 83 | if (b < 1.0 / PRECISION && b > -1.0 / PRECISION)
|
---|
| 84 | r = a;
|
---|
| 85 | else
|
---|
| 86 | r = a / b - 1.0;
|
---|
| 87 |
|
---|
| 88 | /* XXX Need fabs() */
|
---|
| 89 | if (r < 0.0)
|
---|
| 90 | r = -r;
|
---|
| 91 |
|
---|
| 92 | return r < 1.0 / PRECISION;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[d9be488] | 95 | const char *test_float2(void)
|
---|
| 96 | {
|
---|
[7a9ef81] | 97 | bool fail = false;
|
---|
[0b5d37a] | 98 |
|
---|
[d9be488] | 99 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
[ba8eecf] | 100 | double res = cos(arguments[i]);
|
---|
| 101 |
|
---|
| 102 | if (!cmp_double(res, results_cos[i])) {
|
---|
| 103 | TPRINTF("Double precision cos failed "
|
---|
| 104 | "(%lf != %lf, arg %u)\n", res, results_cos[i], i);
|
---|
[7a9ef81] | 105 | fail = true;
|
---|
[d9be488] | 106 | }
|
---|
| 107 | }
|
---|
[ba8eecf] | 108 |
|
---|
[d9be488] | 109 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
[ba8eecf] | 110 | float res = cosf(arguments[i]);
|
---|
| 111 |
|
---|
| 112 | if (!cmp_float(res, results_cos[i])) {
|
---|
| 113 | TPRINTF("Single precision cos failed "
|
---|
| 114 | "(%f != %lf, arg %u)\n", res, results_cos[i], i);
|
---|
[7a9ef81] | 115 | fail = true;
|
---|
[d9be488] | 116 | }
|
---|
| 117 | }
|
---|
[ba8eecf] | 118 |
|
---|
[802a8c8] | 119 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
[ba8eecf] | 120 | double res = sin(arguments[i]);
|
---|
| 121 |
|
---|
| 122 | if (!cmp_double(res, results_sin[i])) {
|
---|
| 123 | TPRINTF("Double precision sin failed "
|
---|
| 124 | "(%lf != %lf, arg %u)\n", res, results_sin[i], i);
|
---|
[7a9ef81] | 125 | fail = true;
|
---|
[d9be488] | 126 | }
|
---|
| 127 | }
|
---|
[ba8eecf] | 128 |
|
---|
| 129 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
| 130 | float res = sinf(arguments[i]);
|
---|
| 131 |
|
---|
| 132 | if (!cmp_float(res, results_sin[i])) {
|
---|
| 133 | TPRINTF("Single precision sin failed "
|
---|
| 134 | "(%f != %lf, arg %u)\n", res, results_sin[i], i);
|
---|
| 135 | fail = true;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
[850fd32] | 138 |
|
---|
[ba8eecf] | 139 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
| 140 | double res = trunc(arguments[i]);
|
---|
| 141 |
|
---|
| 142 | if (!cmp_double(res, results_trunc[i])) {
|
---|
| 143 | TPRINTF("Double precision trunc failed "
|
---|
| 144 | "(%lf != %lf, arg %u)\n", res, results_trunc[i], i);
|
---|
| 145 | fail = true;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
| 150 | float res = truncf(arguments[i]);
|
---|
| 151 |
|
---|
| 152 | if (!cmp_float(res, results_trunc[i])) {
|
---|
[b69786e] | 153 | TPRINTF("Single precision trunc failed "
|
---|
[ba8eecf] | 154 | "(%f != %lf, arg %u)\n", res, results_trunc[i], i);
|
---|
| 155 | fail = true;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[7a9ef81] | 159 | if (fail)
|
---|
| 160 | return "Floating point imprecision";
|
---|
[ba8eecf] | 161 |
|
---|
[d9be488] | 162 | return NULL;
|
---|
| 163 | }
|
---|