The behaviour of floating point division by zero

后端 未结 7 1744
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 02:39

Consider

#include 
int main()
{
    double a = 1.0 / 0;
    double b = -1.0 / 0;
    double c = 0.0 / 0;
    std::cout << a << b          


        
7条回答
  •  死守一世寂寞
    2020-12-03 02:58

    In [expr]/4 we have

    If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. [ Note: most existing implementations of C++ ignore integer overflows. Treatment of division by zero, forming a remainder using a zero divisor, and all floating point exceptions vary among machines, and is usually adjustable by a library function. —end note ]

    Emphasis mine

    So per the standard this is undefined behavior. It does go on to say that some of these cases are actually handled by the implementation and are configurable. So it won't say it is implementation defined but it does let you know that implementations do define some of this behavior.

提交回复
热议问题