testMaths.c 2.12 KB
Newer Older
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
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (C) 2016 Matthieu Schaller (matthieu.schaller@durham.ac.uk)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/

#include "../config.h"

#include "approx_math.h"
#include "vector.h"

#include <math.h>
Matthieu Schaller's avatar
Matthieu Schaller committed
26
#include <stdio.h>
27

28
int main(int argc, char *argv[]) {
29
30

  const int numPoints = 60000;
Matthieu Schaller's avatar
Matthieu Schaller committed
31

32
  for (int i = 0; i < numPoints; ++i) {
Matthieu Schaller's avatar
Matthieu Schaller committed
33
34

    const float x = 0.6f * (i / (float)numPoints) - 0.3f;
35
36
37
38
    const float exp_correct = expf(x);
    const float exp_approx = approx_expf(x);

    const float abs = fabs(exp_correct - exp_approx);
Matthieu Schaller's avatar
Matthieu Schaller committed
39
40
41
    const float rel =
        0.5f * fabs(exp_correct - exp_approx) / fabs(exp_correct + exp_approx);

42
    int error = 0;
43
44
45

    if (abs > 3e-6 && fabsf(x) <= 0.2) {
      printf("Absolute difference too large !\n");
46
      error = 1;
47
    }
48
    if (abs > 3e-7 && fabsf(x) <= 0.1) {
49
      printf("Absolute difference too large !\n");
50
      error = 2;
51
52
53
54
    }

    if (rel > 1e-6 && fabsf(x) <= 0.2) {
      printf("Relative difference too large !\n");
55
      error = 3;
56
    }
57
    if (rel > 3e-7 && fabsf(x) <= 0.1) {
58
      printf("Relative difference too large !\n");
59
      error = 4;
60
61
    }

62
    if (error > 0) {
Peter W. Draper's avatar
Peter W. Draper committed
63
64
      printf("%2d/%d: x= %f exp(x)= %e approx_exp(x)=%e abs=%e rel=%e\n", i,
             error, x, exp_correct, exp_approx, abs, rel);
65
66
67
68
69
70
71
72
      return 1;
    }
  }

  printf("\nAll values are consistent\n");

  return 0;
}