Division by complex in clang++ versus g++

前端 未结 4 1682
无人及你
无人及你 2021-02-19 16:57

When I compile the following code with g++ (4.8.1 or 4.9.0) or clang++ (3.4) I get different outputs.

#include 
#include 

int mai         


        
4条回答
  •  暖寄归人
    2021-02-19 17:18

    The standard says in [complex.numbers] (26.4/3):

    If the result of a function is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.

    There are no specifics on how division should be implemented for complex numbers. Only in [complex.member.ops] it says:

    complex& operator/=(const complex& rhs);
    

    Effects: Divides the complex value rhs into the complex value *this and stores the quotient in *this. Returns: *this.

    and in [complex.ops]:

    template complex operator/(const T& lhs, const complex& rhs);
    

    Returns: complex(lhs) /= rhs.


    As the inverse of 1.e-162 is 1.e+162 and this number is in the range of representable values for a double, the behavior is well defined.

    Thus gcc gets it right and clang has a bug.

提交回复
热议问题