How do you check for infinite and indeterminate values in C++?

前端 未结 6 2104
借酒劲吻你
借酒劲吻你 2020-12-29 20:31

In my programs infinity usually arises when a value is divided by zero. I get indeterminate when I divide zero by zero. How do you check for infinite and indeterminate value

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 21:14

    There's isfinite from C99 or POSIX or something I think.

    One hackish way to do it is to test x-x == 0; if x is infinite or NaN, then x-x is NaN so the comparison fails, while if x is finite, then x-x is 0 and the comparison succeeds. I'd recommend using isfinite, though, or packaging this test into a function/macro called something like isfinite so you can get rid of it all when the time comes.

提交回复
热议问题