3

I want to initialize a variable in C language to be the plus infinity or minus infinity, how can I do that ? The below fails:

float plus_inf = 0.0/0.0;

Ryan
  • 800
  • 1
  • 13
  • 27

3 Answers3

9

You can try

#include <math.h>
....
float x = INFINITY;

According to the standard:

The macro

INFINITY

expands to a constant expression of type float representing positive or unsigned infinity, if available; else to a positive constant of type float that overflows at translation time.

Community
  • 1
  • 1
AlexD
  • 32,156
  • 3
  • 71
  • 65
2

0.0/0.0 is a nan (not a number), as an alternative to INFINITY (c89 and earlier) try:

float plus_inf = 1.0/0.0;
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
2

Macro Infinity is defined under library.

float variable = INFINITY;