Hey guys,
I am here to ask from guys which have some knowledge in trigonometry of the complex numbers.
I have the following problem with my C program: I need to calculate complex sine and cosine of numbers which are around 200-300 and more. The thing is that sine of a complex number z basically is:
sin(z) = ( exp(i*z) - exp(-i*z) ) / 2*i.
And as you know exponent of 200-300 goes to infinity even for a 128-bit long double number. So I am trying to create a workaround. The first thing that came to my mind is bring the value of the angle down to a number between 0 and PI - since sin/cos is a periodic function the return value is expected to be the same.
What I do is take the real and imaginary part, and for each calculate the remainder of division of this number by 2*PI. But the complex sin/cos of the result number is totally different from that of the original... I am puzzled!
Any hints are very welcome.
Artemiy.
Calling math nerds! ;-)
-
- Posts: 123
- Joined: 07:00, 31 May 2004
- Location: Earth
Re: Calling math nerds! ;-)
By "complex numbers" do you mean where the square root of -1 is involved?
#include complex.h
http://www.gnu.org/software/libc/manual ... ex-Numbers
20.9 Complex Numbers
ISO C99 introduces support for complex numbers in C. This is done with a new type qualifier, complex. It is a keyword if and only if complex.h has been included. There are three complex types, corresponding to the three real types: float complex, double complex, and long double complex.
To construct complex numbers you need a way to indicate the imaginary part of a number. There is no standard notation for an imaginary floating point constant. Instead, complex.h defines two macros that can be used to create complex numbers.
— Macro: const float complex _Complex_I
This macro is a representation of the complex number “0+1i”. Multiplying a real floating-point value by _Complex_I gives a complex number whose value is purely imaginary. You can use this to construct complex constants:
3.0 + 4.0i = 3.0 + 4.0 * _Complex_I
Note that _Complex_I * _Complex_I has the value -1, but the type of that value is complex.
_Complex_I is a bit of a mouthful. complex.h also defines a shorter name for the same constant.
— Macro: const float complex I
This macro has exactly the same value as _Complex_I. Most of the time it is preferable. However, it causes problems if you want to use the identifier I for something else. You can safely write
#include
#undef I
if you need I for your own purposes. (In that case we recommend you also define some other short name for _Complex_I, such as J.)
#include complex.h
http://www.gnu.org/software/libc/manual ... ex-Numbers
20.9 Complex Numbers
ISO C99 introduces support for complex numbers in C. This is done with a new type qualifier, complex. It is a keyword if and only if complex.h has been included. There are three complex types, corresponding to the three real types: float complex, double complex, and long double complex.
To construct complex numbers you need a way to indicate the imaginary part of a number. There is no standard notation for an imaginary floating point constant. Instead, complex.h defines two macros that can be used to create complex numbers.
— Macro: const float complex _Complex_I
This macro is a representation of the complex number “0+1i”. Multiplying a real floating-point value by _Complex_I gives a complex number whose value is purely imaginary. You can use this to construct complex constants:
3.0 + 4.0i = 3.0 + 4.0 * _Complex_I
Note that _Complex_I * _Complex_I has the value -1, but the type of that value is complex.
_Complex_I is a bit of a mouthful. complex.h also defines a shorter name for the same constant.
— Macro: const float complex I
This macro has exactly the same value as _Complex_I. Most of the time it is preferable. However, it causes problems if you want to use the identifier I for something else. You can safely write
#include
#undef I
if you need I for your own purposes. (In that case we recommend you also define some other short name for _Complex_I, such as J.)
Re: Calling math nerds! ;-)
Yes Sami, I am using and gcc, you guessed
Still can't make it work - but in theory complex sin/cos are periodic functions: sin (z) = sin (z+PI)...
Here is the essence of my code:
double complex A, B;
A = 5.0 + 6.0*I;
B = drem(__real__ A,2*M_PI) + drem(__imag__ A,2*M_PI)*I;
and then csin(A) != csin(B)...

Here is the essence of my code:
double complex A, B;
A = 5.0 + 6.0*I;
B = drem(__real__ A,2*M_PI) + drem(__imag__ A,2*M_PI)*I;
and then csin(A) != csin(B)...
Re: Calling math nerds! ;-)
Art,
exp(i y) = cos(y) + i sin(y)
This might let you get rid of that exp term and do the trig fns on real numbers.
Dunno, maybe not so helpful.
-illiac
exp(i y) = cos(y) + i sin(y)
This might let you get rid of that exp term and do the trig fns on real numbers.
Dunno, maybe not so helpful.
-illiac
Re: Calling math nerds! ;-)
Thanks illac! But this is not really applicable to my case, my number is in form a + i*b, and not just i*b so Euler's formula won't work I guess... I'll keep on trying.
Re: Calling math nerds! ;-)
Hi Art,
Ooops! Never mind.
-illiac
Ooops! Never mind.
-illiac
Re: Calling math nerds! ;-)
OK, one more probably lame suggestion
exp(a + ib) = exp(a) * exp(ib)
So let z = a + ib
sin(z) = ( exp(iz) - exp(-iz) ) / 2i.
sin(z) = ( exp(i(a + ib)) - exp(-i(a + ib)) ) / 2i.
sin(z) = ( exp(-b + ai) - exp(b - ai) ) / 2*i.
sin(z) = ( exp(-b)exp(ai) - exp(b)exp(-ai)) ) / 2i
sin(z) = ( exp(-b)(cos(a)+i*sin(a)) - exp(b)(cos(-a)+i*sin(-a)) ) / 2i
and now all the exps and trig functions are real-valued.
I'm not sure this is very helpful because the exps will still overflow...
Are you sure about this equality in your original formulation:
(a+bi)%pi = (a%pi + (b%pi)i) ?
-illiac
exp(a + ib) = exp(a) * exp(ib)
So let z = a + ib
sin(z) = ( exp(iz) - exp(-iz) ) / 2i.
sin(z) = ( exp(i(a + ib)) - exp(-i(a + ib)) ) / 2i.
sin(z) = ( exp(-b + ai) - exp(b - ai) ) / 2*i.
sin(z) = ( exp(-b)exp(ai) - exp(b)exp(-ai)) ) / 2i
sin(z) = ( exp(-b)(cos(a)+i*sin(a)) - exp(b)(cos(-a)+i*sin(-a)) ) / 2i
and now all the exps and trig functions are real-valued.
I'm not sure this is very helpful because the exps will still overflow...
Are you sure about this equality in your original formulation:
(a+bi)%pi = (a%pi + (b%pi)i) ?
-illiac