exp_calc.c


1
#include <stdio.h>
2
3
char exp_ovf;
4
5
#define ln10_val     2.302585093f
6
7
float exp_calc(float x)
8
{
9
  float eps  = 0.00000000001f;
10
  float elem = 1.0f;
11
  float sum  = 0.0f;
12
  char  nega = 0;
13
  int   i    = 1;
14
15
  exp_ovf= 0;
16
  if (x < 0.0f)
17
  {
18
    nega= 1;
19
    x= -x;
20
  }
21
  do
22
  {
23
    sum += elem;
24
    elem *= x / i;
25
    i++;
26
    if (sum> 1e37) break;
27
  } while (elem >= eps);
28
  if (sum> 1e37) exp_ovf= 1;
29
30
  if (nega)
31
  {
32
    return 1.0f / sum;
33
  }
34
  else return sum;
35
}
36
37
int main(void)
38
{
39
  char   ch;
40
  float  a, vu;
41
  float  z, x;
42
43
  a= 64.5f;                     // Verstaerkung in db
44
  vu= exp_calc((a/20.0f) * ln10_val);
45
  printf("\n\r Verstaerung von a= %.3f db entspricht Vu= %.3f \n\r",a, vu);
46
  z= 2431.5f;
47
  x= exp_calc(z);
48
  printf("\n\r Testergebnis : %.5f \n\r",x);
49
}