Division by complex in clang++ versus g++

前端 未结 4 1684
无人及你
无人及你 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:33

    Update:

    Quoting from the standard:

    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].

    Quoting from http://lists.freebsd.org/pipermail/freebsd-numerics/2014-March/000549.html:

    It appears that clang developers have chosen the naive complex division algorithm.

    ...

    I did a bit of grepping. Could it be that the division algorithm is contained in the file src/contrib/llvm/tools/clang/lib/CodeGen/CGExprComplex.cpp inside the function ComplexExprEmitter::EmitBinDiv ?

    If you look at the code, it certainly looks like it is generating code to perform complex division, and it definitely looks like they are using the naive algorithm.

    Assuming that indeed the clang uses naive complex division the expression 1.0 / c evaluates according to the naive implementation of complex division to the following expression enter image description here,

    enter image description here

    1.e-324 is out of the double range. This results according to the standard to undefined behaviour.

    Also making a search in the LLVM/Clang bug list, it appears that there are quite some issues concerning complex division.

    As such your case is a bug and you should report it.

    For anyone who is interested on how robust complex division is implemented take a look at

    1. http://ideone.com/bqFk8j and
    2. A Robust Complex Division in Scilab.

提交回复
热议问题