C语言如何初始化一个float变量为无穷大?

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

我想用C语言初始化一个变量为正无穷大或负无穷大,怎么办?以下失败:

浮动 plus_inf = 0.0/0.0;

你可以试试

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

按照标准:

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.

0.0/0.0nan(不是数字),作为 INFINITY(c89 及更早版本)的替代方法,请尝试:

float plus_inf = 1.0/0.0;

宏无限在库下定义。

浮动变量 = INFINITY;