c++ - 为什么标准同时提供了 is_integer 和 is_exact?

标签 c++ floating-point integer fixed-point numeric-limits

std::numeric_limits 提供了 2 个互斥的常量:

  • is_integer : true 对于所有整数算术类型 T

  • is_exact : true 对于所有使用精确表示的算术类型 T

是否存在非精确整数类型的可能性?在这里允许做什么?

在我知道我是否正在处理精确数字的所有模板中,我使用了 is_integer,我现在是否还需要添加对 is_exact 的检查?

最佳答案

来自 is_exact cppreference 页面:

Notes

While all fundamental types T for which std::numeric_limits<T>::is_exact==true are integer types, a library may define exact types that aren't integers, e.g. a rational arithmetics type representing fractions.


而且,正如@Holt 所提到的,该标准也对其进行了描述:

21.3.4.1 numeric_limits members [numeric.limits.members]

static constexpr bool is_exact;

true if the type uses an exact representation. All integer types are exact, but not all exact types are integer. For example, rational and fixed-exponent representations are exact but not integer.

关于c++ - 为什么标准同时提供了 is_integer 和 is_exact?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48227296/

相关文章:

c++ - 我如何使用 glCreateBuffers() 而不是 glGenBuffers()?

c++ - 以编程方式为 ListView 项目构建 UI

c++ - 如何将 Windows C++ 程序的输出重定向到另一个可执行文件?

c - 为什么这个浮点循环在 1,000,000 处终止?

PHP 从字符串中获取整数

python - PyImport_ImportModule,可以从内存中加载模块吗?

javascript - 在Javascript中创建两个数字之间的整数数组

mysql - 浮点类型比较

cocoa - 如何将整数传递给模型类

java - 如何判断一个double变量是否有整型值?