sine functions wont compile

Any idea why this won't compile guys. I can't see why it wont work.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cmath>
using namespace std;

#define pi 3.141592654;

double give_val_x(double x){
	double cos(2*x), sin(x*x), val;
	val = cos(2*x) + x*sin(x*x);
	return val;
}

any help will be greatly appreciated.
whoa! you tried to declare doubles called "cos(2*x), sin(x*x), val"
and I imagine yer compiler didn't like it very much.

try this instead:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cmath>
using namespace std;

#define pi 3.141592654

double give_val_x(double x)
{
	double val;
	val = cos(2*x) + x*sin(x*x);
	return val;
}

also I'm assuming (and hoping) that's not the entire code.. "double give_val_x(double x);" will need to be called from main(){}

also not going to guarantee the math to be right but at least that should compile
if you want to get a useful answer you'll need to incorporate "pi" into your equations (or rather, assignments) because sin() and cos() return angles in radians, not degrees... but if you're looking for output in radians it's all good, but then you wouldn't need:
#define pi
Last edited on
cPlusN00b is right but let me point out you don't really need to store the result into var.

You can also:

1
2
3
4
double give_val_x(double x)
{
    return cos(2*x) + x*sin(x*x);
}
of course its not the entire code. that would be silly. no, its just so i can find a value of a function. so variables cant be called in the same line like that? that should help then, let me try that.
p.s. im pretty bad with coding.
ah yeah ^ EssGeEich's code is way cleaner :3

xsemel:
so variables cant be called in the same line like that?

well it's more like "cos(2*x)" and "sin(x*x)" aren't variables at all, they just don't belong in that statement. Your compiler will see nonsense.
Last edited on
yup. :). when i looked up how to call sin or cos everything says use something like
 
double sin(double [random number])

so i thought declaring sin and cos was required. :P thanks for the help amigo
quite welcome
good luck and happy coding
No, you don't need to declare that, because that line you wrote double sin(double [random number]) IS the function declaration (which is into file math.h or cmath).

Good Luck!
Last edited on
I thought declaring sin and cos was required

It is required.

This is how you do it:
#include <cmath>
The functions are already declared for you inside that header file.

One other comment, in C++ it is preferred to use
const double pi=3.141592654;
rather than a #define
Last edited on
oh yeah and just so ya know, xsemel, chervil reminded me of somethin important

if you do use
#define keyword value
you will probably not want a semicolon ";" at the end of the line.
the preprocessor will replace "keyword" with "value" exactly at every instance in the code before it is compiled (hope I'm using right terms here)

so something like this:
1
2
#define pi 3.14;
int pie = pi;

which seems solid will actually look like this to compiler:
int pie = 3.14;;
see the double ";;"? "pi" is replaced by "3.14;"

instead you just do this:
1
2
#define pi 3.14
int pie = pi;

preprocessor commands (#) don't need to end with a ";"
they continue until the end of the line in the source

just so ya know.
but do like he says ^ and just use const
make c++ gods happy
Last edited on
Topic archived. No new replies allowed.