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

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

    if (x!=x)              ... then x is nan
    if (x>0 && x/x != x/x) ... then x is +inf
    if (x<0 && x/x != x/x) ... then x is -inf
    

    this might also work (but involves call to exp() and testing equality of doubles):

    if (exp(-x)==0.) ... then x is inf
    if (exp(x)==0.)  ... then x is -inf
    

提交回复
热议问题