03-floating-point_exceptions.c 632 Bytes
Newer Older
Xavier Besseron's avatar
PS 2021  
Xavier Besseron committed
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
26
27
28
29
30
31
32
#include <stdio.h>
#include <math.h>

// Notes:
//
//  Add -lm on the compilation command line to link with the math library
//  Add -Ddivbyzero -Dinvalidop or -Doverflow to compile only the relevant part


int main(int argc, char** argv)
{

#ifdef divbyzero
  // Division by zero error
  double a = 1.0 / 0.0;
  printf("Division by zero:   1.0 / 0.0 = %e\n", a);
#endif

#ifdef invalidop
  // Invalid operation
  double b = sqrt(-1.0);
  printf("Invalid operation:  sqrt(-1.0) = %e\n", b);
#endif

#ifdef overflow
  // Overflow
  double c = exp( 1e30 );
  printf("Overflow:           exp( 1e30 ) = %e\n", c);
#endif

  return 0;
}