c++ - MSVC++ 中的无限

标签 c++ visual-c++ floating-point infinity

我正在使用 MSVC++,我想在我的代码中使用特殊值 INFINITY。

在 MSVC++ 中用于无穷大的字节模式或常量是什么?

为什么 1.0f/0.0f 的值看起来是 0?

#include <stdio.h>
#include <limits.h>

int main()
{
  float zero = 0.0f ;
  float inf = 1.0f/zero ;

  printf( "%f\n", inf ) ; // 1.#INF00
  printf( "%x\n", inf ) ; // why is this 0?

  printf( "%f\n", zero ) ; // 0.000000
  printf( "%x\n", zero ) ; // 0

}

最佳答案

使用numeric_limits :

#include <limits>

float maxFloat = std::numeric_limits<float>::infinity();

关于c++ - MSVC++ 中的无限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2538339/

相关文章:

c++ - 可以对内置类型使用 C 样式转换吗?

C++ 带指针的冒泡排序函数

c++ - Visual C++ 优化选项 - 如何改进代码输出?

c++ - lambda 可以有 "C"链接吗?

python - C 类型 float、double 和 long 的 Python 等价物是什么?

c++ - 这种与浮点文字零的比较有效吗?

c++ - 类和继承

c++ - 是否可以使模板函数成为模板特化类的友元?

python - 如何在python中提取float的十进制值

C++ 从文件中读取数字