The behaviour of floating point division by zero

后端 未结 7 1739
没有蜡笔的小新
没有蜡笔的小新 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:56

    Division by 0 is undefined behavior.

    From section 5.6 of the C++ standard (C++11):

    The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined. For integral operands the / operator yields the algebraic quotient with any fractional part discarded; if the quotient a/b is representable in the type of the result, (a/b)*b + a%b is equal to a .

    No distinction is made between integer and floating point operands for the / operator. The standard only states that dividing by zero is undefined without regard to the operands.

提交回复
热议问题