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

前端 未结 6 2091
借酒劲吻你
借酒劲吻你 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:16

    Although C++03 doesn't provide C99's isnan and isinf macros, C++11 standardizes them by providing them as functions. If you can use C++11, instead of strict C++03, then these would be cleaner options, by avoiding macros, compiler built-ins and platform-dependant functions.

    C++11's std::isfinite returns true for all values except inf and nan; so !isfinite should check for infinite and indeterminate values in one shot.

提交回复
热议问题