C ++ 11 rounding error using pow () and std :: complex methods

The following

#include <iostream>
#include <complex>

int main()
{
  std::complex<double> i (0,1);
  std::complex<double> comp =pow(i, 2  );
  std::cout<<comp<<std::endl;
  return 0;
}

gives the expected result (-1.0) without C ++ 11. However, compiling with C ++ 11 gives a very annoying (-1.1.22461e-016).

What to do and what is best?

Of course, this can be fixed manually with floor coverings, etc., but I would appreciate the correct way to solve the problem.

SYSTEM: Win8.1 using Desktop Qt 5.1.1 (Qt Creator) with MinGW 4.8 32 bits. Using C ++ 11, adding the flag QMAKE_CXXFLAGS + = -std = C ++ 11 to the .pro file.

+4
source share
2 answers

++ 11 pow(std::complex). GCC , int unsigned int.

( std::complex</*Promoted*/> pow(const std::complex<T> &, const U &)) pow(i, 2) . #ifdef C++11, ( ) ( pow(double,double) std::polar).

+5

, . - .

1.22461e-016

+0,0000000000000000122461

, . ( 2 .

, 0,1, .

, , : 1. 2. .

:

comp = i * i ;

, .

Pow (x, y) is going to do

exp (log (x) * y) 

F.

0

Source: https://habr.com/ru/post/1536159/


All Articles