Simple Math

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <conio>
#include <math>
int main()
{
	double x=0.25;
	cout<<"x^(1/2) is "<<pow(x,(1/2))<<endl;   		//answer should be 0.5 (but it gave "1")
   cout<<"cos(x) is "<<cos(x)<<endl;               //the answer is in radian, how to make it in Degree?
   cout<<"sin(x) is "<<sin(3)<<endl;               //the answer is in radian, how to make it in Degree?
   cout<<"x^7 is "<<pow(x,7)<<endl;
   cout<<"((x^3)+(x^7))/x is "<<(pow(x,3)+pow(x,7))/x<<endl;
   cout<<"sqrt|7-9| is "<<sqrt(7-9)<<endl;         //how to make it sqrt|7-9|<-Modulus?

   getch();
   return 0;
}


Hi, can anyone tell me how to correct the error about? I've put the question inside the comment...TQ...
line 7. 1/2 = 0 due to integer division. Write 1.0/2 or just 0.5
lines 8, 9. radian*180/3.1415 = degree
line 12. There is a function abs (or fabs?)
Use 1.0/2.

Degrees = Radians * (180/pi). Write a ToDegrees function that takes a radian value and returns it in degrees.

Not sure what you want from the last one, but the modulus operator is %.
Last edited on
Thanks...
@hamsterman
for the line 7, is that I can use 1.0/2, 1/2.0 and 0.5?
for lines 8 and 9, is there any function in C++ like "deg"? instead of doing math like radian*180/3.1415 ?
for line 12, I tried both abs and fabs, they works fine, is that "fabs" is legal too?
cout<<"sqrt|7-9| is "<<sqrt(fabs(7-9))<<endl;

TQ...

@iHutch105
1. how to use ToDegrees function?
2. actually 7-9=-2 but I want to take the magnitude of it only which is "2", is that I can use % also?

TQ...
For 7 you could use 1.0/2 or 1/2.0 or 0.5 or 1./2 or 1/(float)2 or float(1)/2 or many similar variations. The important thing is to say that one of the numbers is not an integer.
There is no deg. Like iHutch105 said, you can write one. Here: double deg(double rad) { return rad*108/3.1415; }
I wrote a "?" because I remembered using abs, but in a reference only saw fabs. I'm sure they're both fine.
% can't be used for absolute value. It only gives the remainder.
Typo in your function, Hamsterman. *180 :-)
Thanks hamesterman and iHutch105

@hamsterman
double deg(double rad) { return rad*180/3.1415; }

why there is deg inside? I thought u mean cannot have deg right? Or I misunderstoond, pls help...Thanks...
Last edited on
Inside what? I only said that there wasn't a function that could convert radians to degrees in the standard library. Any kind of operation can be put into a function for your convenience.
Thanks hamsterman...

I mean Inside this double deg(double rad) { return rad*180/3.1415; }

is that it's a fuction created ourself? I still not study about function yet...that's why don't know what;s that code means...sorry...
Yes, that's a function definition.
Topic archived. No new replies allowed.