Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a float variable to be infinity in C language ? [duplicate]

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;

like image 859
Ryan Avatar asked May 10 '26 09:05

Ryan


2 Answers

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.

like image 58
AlexD Avatar answered May 13 '26 01:05

AlexD


Macro Infinity is defined under library.

float variable = INFINITY;