std::pow with integer parameters, comparing to an integer type

后端 未结 4 1371
天命终不由人
天命终不由人 2020-12-11 20:59

According to http://en.cppreference.com/w/cpp/numeric/math/pow , when std::pow is used with integer parameters, the result is promoted to a double.

4条回答
  •  执笔经年
    2020-12-11 21:47

    As several answers have pointed out even though for small numbers the result should be exactly representable there are some low quality implementations.

    For the case where you are using const expressions such as:

    std::pow(2, 10)
    

    many compilers will use builtin functions for example both gcc and clang will use builtin functions which will probably use something like a lookup table or a simpler formula for these trivial cases. We can see for the above case using godbolt that gcc computes the value at compile time:

    movl    $1024, %esi     
    

    These results are more likely to be correct as the question C: i got different results with pow(10,2) and pow(10,j), j=2; demonstrates.

提交回复
热议问题