为什么isnan,isinf和isfinite返回int而不是bool?

问题描述 投票:2回答:2

isnan返回一个int ... nonzero value if arg is NaN, ​0​ otherwise。为什么不回归布尔?这不是一些传统的东西,它是C ++ 11的新功能。

c++ floating-point c++11 nan infinite
2个回答
5
投票

这是传统的东西,这些都可以在<cmath>中找到,这是将<math.h>放入std命名空间的C标准库。 C没有原生的bool类型

虽然这些都是C ++ 11的添加,但它们实际上是在C99中添加到<math.h>中的东西。


1. For those about to complain, C99's bool is actually a macro. There is a _Bool, but I guess the C99 authors chose not to use it for isnan et al. in order to retain consistency with other stuff in <math.h>.

0
投票

为了澄清,isnan,isinf作为宏在C99中作为int表达式添加。因此,对于C兼容性,将它们添加为返回int的函数是有意义的。

http://www.cplusplus.com/reference/cmath/isnan/

C99 C ++ 11

在C中,这是作为返回int值的宏实现的。 x的类型应为float,double或long double

© www.soinside.com 2019 - 2024. All rights reserved.