Complex number multiplication

i am trying program based on digital signal processing...i wanna create a function which uses complex type to calculate the vale for cos 2*PI*n/8-j sin 2*PI*n/8..where n=0to2 and i wanna use this function to multiply with scalar....i tried by googling it..but i am totally confused....am getting error such that no match call...
Try checking the <cmath> header. Useful functions are provided there for you.
http://cplusplus.com/reference/clibrary/cmath/
i used both <cmath> and <complex>...problem is that operation must define under a function and the function must be multiplied to a real no...i cant do this...anyone pls help me..
The complex numbers in C++ have no problem being multiplied by a scalar, as long as the scalar has the same type: complex<double> can be multiplied by a double, but not by an int or even float.

 ``123456789101112131415161718`` ``````#include #include #include const double pi = std::acos(-1); std::complex f(int n) { return std::complex( std::cos(2*pi*n/8), -std::sin(2*pi*n/8) ); } int main() { std::cout << 1.0 * f(0) << '\n' << 1.1 * f(1) << '\n' << double(10) * f(2) << '\n'; }``````

> complex<double> can be multiplied by a double, but not by an int or even float.

If such a facility is absolutely required:
 ``1234567891011121314151617181920`` ``````#include #include template< typename T, typename SCALAR > inline typename std::enable_if< !std::is_same::value, std::complex >::type operator* ( const std::complex& c, SCALAR n ) { return c * T(n) ; } template< typename T, typename SCALAR > inline typename std::enable_if< !std::is_same::value, std::complex >::type operator* ( SCALAR n, const std::complex& c ) { return T(n) * c ; } // and so on for other arithmetic operators int main() { std::complex c( 1.7, 2.8 ) ; auto a = c * 3 ; auto b = 2.6f * c ; c *= 23 ; }``````
@Cubbi::Thank u very much...i got it....
@JLBorges:how to use this template....
> how to use this template....

`#include <type_traits> ` and then just define the template.

Note: with g++ or clang, you also need a compiler option `--std=c++11` or (in older versions) `--std=c++0x`
there is an output error...in this function cos(2*pi*n/8) when n=2 i must get result as zero...but am getting -1.79821e-006....how to clear this problem...
Topic archived. No new replies allowed.