I need to compute imaginary exponential in C.
As far as I know, there is no complex number library in C. It is possible to get e^x with exp(x) of math.h, but how can I compute the value of e^(-i), where i = sqrt(-1)?
In C99, there is a complex type. Include complex.h; you may need to link with -lm on gcc. Note that Microsoft Visual C does not support complex; if you need to use this compiler, maybe you can sprinkle in some C++ and use the complex template.
I is defined as the imaginary unit, and cexp does exponentiation. Full code example:
#include <complex.h>
#include <stdio.h>
int main() {
complex x = cexp(-I);
printf("%lf + %lfi\n", creal(x), cimag(x));
return 0;
}
See man 7 complex for more information.
Note that exponent of complex number equals:
e^(ix) = cos(x)+i*sin(x)
Then:
e^(-i) = cos(-1)+i*sin(-1)
Using the Euler's Formula you have that e^-i == cos(1) - i*sin(1)
e^-j is just cos(1) - j*sin(1), so you can just generate the real and imaginary parts using real functions.
Just use the cartesian form
if z = m*e^j*(arg);
re(z) = m * cos(arg);
im(z) = m * sin(arg);
Is calling a c++ function a solution for you? The C++ STL has a nice complex-class and boost also has to offer some nice options. Write a function in C++ and declare it "extern C"
extern "C" void myexp(float*, float*);
#include <complex>
using std::complex;
void myexp (float *real, float *img )
{
complex<float> param(*real, *img);
complex<float> result = exp (param);
*real = result.real();
*img = result.imag();
}
Then you can call the function from whatever C-code you rely on ( Ansi-C, C99, ...).
#include <stdio.h>
void myexp(float*, float*);
int main(){
float real = 0.0;
float img = -1.0;
myexp(&real, &img);
printf ("e^-i = %f + i* %f\n", real, img);
return 0;
}
In C++ it can be done directly:
std::exp(std::complex<double>(0, -1));
Related
Why does the following bit of code work in C:
int res = pow(2, 3);
printf("%d\n", res);
while this other doesn't?
int a = 2;
int b = 3;
int res = pow(a, b);
printf("%d\n", res);
Even if I try
double a = 2;
double b = 3;
double res = pow(a, b);
printf("%f\n", res);
I get an
undefined reference to `pow'
What am I doing wrong?
When it works, it's because the calculation was done by the compiler itself (and included in the binary as if you wrote it out)
printf("8\n");
When it doesn't work, is because the pow function is included in the math library and the math library isn't linked with your binary by default.
To get the math library to be linked, if your compiler is gcc, use
gcc ... -lm ...
With other compilers, should be the same :)
but read the documentation
undefined reference to 'pow' sounds like a linker error. You are not linking in the math library, even if you introduce the function pow by including <math.h>.
With gcc, use the -lm command line parameter to link in the math lib.
Use like this
#include <math.h>
#include <stdio.h>
int main(void)
{
for(int i = 1; i < 5; i++)
printf("pow(3.2, %d) = %lf\n", i, pow(3.2, i));
return 0;
}
Output:
pow(3.2, 1) = 3.200000
undefined reference to `pow'
because power to any number must have an integer value as power
pow(x,y)
where, x must be real and y must be a whole number
I have these two equations
and I need to convert them to C code where you inputk and x. The thing is I don't get that advanced levels of math, neither did I learn C in the past :D
Can anyone show me step by step what built-in functions can be used for this and how exactly should the logic behind the app work?
Cheers!
Your formula is wrong. As shown here (along with the proof of the derivation) the correct formula is
You have k and n swapped in your summation. The inputs should then be x and n. The correct code is then:
#include <math.h>
double sum_of_sin(double x, int n) {
if (sin(x/2) == 0.0) {
return 0.0; //prevent division by 0 for x multiple of 2π
}
return sin(n*x/2) * sin((n+1)*x/2) / sin(x/2);
}
You can include the file math.h which has inbuilt functions like cos() and sin().
For instance:
#include <stdio.h>
#include <math.h>
int main ()
{
double res;
ret = cos(45);
printf("The cos of angle 45 is %f",res);
return 0;
}
Hope it helps..
You have an equation that consists of two formulae. It is easy to write the two parts out, but it can be hard to find a solution. The right-hand part of the first formula would be:
float RHvalue;
RHvalue = (sin( ((n+1.0)*x) / 2.0 ) * sin(n * x/2.0) ) / sin(x/2.0);
now, what the values of kx would be, is another matter, though it would be easy to sum them.
I understand that this is a common problem. However I can't find a solid straight answer.
16 ^ 54 = 1.0531229167e+65 (this is the result I want)
When I use pow(16,54), I get:
105312291668557186697918027683670432318895095400549111254310977536.0
Code is as follows:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main(){
double public;
double a = 16;
double b = 54;
public = (pow(a,b));
printf("%.21f\n", public);
}
Code executed with:
gcc main.c -lm
What I'm doing wrong?
What am I doing wrong?
Several things:
Use %.10e format for scientific notation with printf for a printout with ten digits after the dot,
Return an int from your main,
Consider not using public to name a variable, on the chance that your program would need to be ported to C++, where public is a keyword.
Here is how you can fix your program:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(){
double p;
double a = 16;
double b = 54;
p = (pow(a,b));
printf("%.10e\n", p);
return 0;
}
Demo on ideone.
Have you tried:
printf("%e\n", public);
The %e specifier is for scientific notation, as described in the documentation
If you need scientific notation you need to use the %e format specifier:
printf("%e\n", public);
^^
Also, public is a keyword in C++ and so it would be a good idea to avoid that and any other keywords in case this code needs to be portable.
Hi I am new to C programming, I am just trying to replace part of my code with a function call but I don't know how to do it properly, please help.
I just want the line d = ... to be equivalent to the line e = ...
#include <stdio.h>
#include <math.h>
double dist(int i, int j, double v[100][2])
{
return sqrt( pow((v[j][0] - v[i][0]),2) + pow((v[j][1] - v[i][1]), 2) )
}
main()
{
double v[100][2], d, e;
v[1][0] = 0;
v[1][1] = 1;
v[2][0] = 1;
v[2][1] = 1;
d = sqrt( pow((v[1][0] - v[2][0]),2) + pow((v[1][1] - v[2][1]), 2) );
e = dist(1,2,v);
printf("\n%f\n",d);
printf("\n%f\n",e);
}
double dist(int i, int j, double (*v)[2])
{
return sqrt( pow((v[j][0] - v[i][0]),2) + pow((v[j][1] - v[i][1]), 2) );
}
d = dist(0,1,v)
Or dist(1,0,v)
Distance between point 0 and point 1 ... Order does not matter.
EDIT: What I have above is a function CALL, as requested. d= is equivalent to e= ... to write another function is quite the waste of code and more importantly, not a realization of what a function is used for. I stick by my answer.
If you wanted the same thing for different types, you can use a macro (not recommended for this case since a decent compiler will inline the function call to Cato's function) but just for educational purposes
#define dist(i,j,v) sqrt(pow((v[j][0]-v[i][0]),2)+pow((v[j][1]-v[i][1]),2))
Just keep in mind that sqrt returns a double, so if you want float or long double returns, you'll need sqrtf or sqrtl.
The advantage to using macros for mathematical "functions" is that they get expanded out into the code prior to compile such that constants can be evaluated into the computation and can sometimes reduce the entire calculation down to a much simpler computation or sometimes even a constant value.
Mike is correct on the mathematical properties, though precision may cause the 2 values to differ slightly (usually this difference is unwanted).
Ok, i'm writing in c here. Compiling in mingw gcc.
I'm trying to do something really simple. create a vector struct containing 3 floats x,y,z.
then I want to be able to do some math with them.
This is my short test program:
#ifndef _PHYSICS_C_
#define _PHYSICS_C_
#define SUCCESS 0
#define FAILURE 1
typedef struct {
float x;
float y;
float z;
}vector;
int add ( vector* a, vector* b, vector* destination ){
(*destination).x = (float)( ((*a).x) + ((*b).x) );
(*destination).y = (float)( ((*a).y) + ((*b).y) );
(*destination).z = (float)( ((*a).z) + ((*b).z) );
return SUCCESS;
}
int main(int argc, char** argv){
printf("creating vectors\n\n");
vector a = {1.0f,5.0f,3.0f};
vector b = {2.0f,3.0f,6.0f};
vector destination;
printf("adding vectors\n\n");
if(add(&a, &b, &destination) == SUCCESS){
printf("result: (%d, %d, %d)\n\n",destination.x,destination.y,destination.z);
} else {
printf("the program failed somehow...\n\n");
}
printf("Press any key to continue...\n");
getchar();
return SUCCESS;
}
#endif
When I compile and run it, it should return (3, 8, 9) the sum of vectors a and b.
instead it returns (0, 1074266112, 0)...
I can't figure out what is wrong.
for some reason I think that I must somehow be writing over memory I'm not supposed to.
x,y,z are floats but you are trying to print them as integers.
try:
printf("result: (%f, %f, %f)\n\n",destination.x,destination.y,destination.z);
check man printf or your documentation to see all of the specifiers for printf.
%d expects int. Use %f or %g for floats/doubles.
Identifiers starting with an underscore followed by an uppercase letter are reserved; don't use them in your own code.
Include guards (#ifndef _PHYSICS_C_ ...) are for header files, not for .c files.
printf requires #include <stdio.h>.
You're returning the value SUCCESS from main(). That's ok, since SUCCESS happens to be 0, but it would be clearer to use either EXIT_SUCCESS (declared in <stdlib.h> or just return 0;.
You add function always returns SUCCESS. It might as well be a void function (and the test for its value in main is not useful). Unless you anticipate adding error checking later on.
The casts in your add function are unnecessary; the expression is already of type float. And (*foo).bar is better written as foo->bar. For example, the first assignment can be simplified to destination->x = a->x + y->x;.
The real problem (which has already been pointed out) is that you're using a "%d" format for values of type float.
It's usual to use double rather than float. It has more precision, and modern hardware is often optimized for double-precision operations.
If you enable warnings in your compiler, it will probably tell you about some of these problems.
You are printing float with format specifier %d which is intended for signed int. Use %f or %g or %e instead .
Also, why don't you do:
destination->x = a->x + b->x;
Its much easier on the eyes. (although not a problem).