How would I convert degrees to radians exactly. Rather than showing 0.785398 I want the program to show pi/4.
I am currently using this script to convert degrees into radians.
cout << angle * M_PI / 180.0 << " radians";
You might do
std::cout << angle / 180.0 << " pi radians";
Which will print
0.25 pi radians
You might be interested by how-to-convert-floats-to-human-readable-fractions to convert 0.25 into 1/4.
Related
For example I have a value 0.70, and it is a sin() for 45 degrees. I need a function, that will calculate me an angle from a sin. What function from C++ can help me?
You can use std::asin. This returns a value between [-PI/2,+PI/2], to get degrees you multiply by 180 and divide by PI.
result = std::asin(value) * 180 / PI
PI = 3.1415926535
I'm building a small Physics engine and I'm having trouble converting my Radian value to Degrees using atan, as I need an angle to output in Degrees only.
Firstly, I have an x and y value, and I need to find an angle using atan, so I divide y by x like so:
angleDivide = yN / xN;
Then, before putting this value into tan, I attempt to convert it to Degrees like this:
angleToDegrees = angleDivide * (3.14 / 180);
Then I place angleToDegrees into atan:
angle = atan(angleToDegrees);
But when I'm displaying angle, I'm, still getting radian values.
Please could you tell me what is wrong with my code and how to fix this?
You want to calculate radians=tan(y/x) first.
Then you can convert it to degrees:
radians = atan(y/x)
degrees = radians * (180.0/3.141592653589793238463)
See the reference here for atan:
On a side note, you also have to take into account what quadrant you are in to get the correct answer (since -y/x is the same number as y/-x)
I'm getting some strange behavior using COS() and SIN() libraries in Fortran 77. It picks what number to perform it's operation on like here :
DATA V , THETA , PI / 100 , 45 , 3.1416 /
THETA = THETA * PI/ 180.0
PRINT *, "THETA = " , THETA
VX = V * COS ( THETA )
VY = V * SIN ( THETA )
PRINT *, VX , VY
END
when i check it, i find that
COS(THETA) = COS(45)
not
COS(0.7853)
and the same goes to SIN() too. So why does this happen ?
This is my output :
THETA = 0.785400
70.7105 70.7108
Those are the correct results. You're taking the cos of 45 degrees (converted to radians) and multiplying it by 100, and the cos of 45 degrees (or pi/4 radians) is 0.7071067, and so the expected value to print out is 70.7106.
SIN and COS functions take the arguments in radians. You use the correct formula to convert the input values from degrees to radians, and it's giving you the correct results for those converted values.
I'm curious how you could do the right conversion, and not realize that you did the right thing and got the right answer. Did you take this code from somewhere else?
I am trying to do a simple trigonometric calculation in C++. The following is an example of the problem I am having with this. As far as I know, C++ works in radians, not degrees. So conversion from radians to degrees should be a simple case of multiplying by 180 and dividing by pi. A simple test is tan(45), which should equate 1. The following program produces a value of 92.8063 however...
#include <iostream>
using namespace std;
#include <math.h>
int main(){
double a,b;
a = tan(45);
b = a * 180 / 3.14159265;
cout << b;
return 0;
}
What is wrong?
You're doing it backwards. Don't apply the formula to the output of tan, apply it to the parameter.
Also you'll want to multiply by pi and divide by 180, not vice versa.
The angle is the input to tan. So you want:
a = 45 * 3.141592653589793 / 180.0;
b = tan(a);
cout << b << endl;
You must pass radians to the tan function. Also degrees to radian is wrong.
a = tan(45 * 3.14159265 / 180.);
Tan accepts an angle, and returns a quotient. It is not the other way around. You want
a = tan(45*3.14159265/180); // Now a is equal to 1.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
My code is producing an output of nan.
I have looked around and I'm guessing it is as the equation is complicated from what I gather C++ doesn't recieve complicated equations too well.
But that doesnt seem right.
Here's my code:
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int Backfill;
double SlopeAngleOfWall, AngleOfInternalFriction, AngleOfFrictionSoilAndWall, BackfillSlope, CoefficientOfActivePressure;
cout << "Retaining Wall Calculator \n";
cout << "Enter the slope angle of the wall, this is measured from the horizontal plane, therefor will be 90 degrees if the retaining wall is vertical \n";
cin >> SlopeAngleOfWall;
cout << "Enter the angle of internal friction \n";
cin >> AngleOfInternalFriction;
cout << "Enter the angle of friction between the soil and the wall \n";
cin >> AngleOfFrictionSoilAndWall;
cout << "Enter the angle of the backfill slope \n";
cin >> BackfillSlope;
/* To make sin function work is is typed (angle*pi/180) */
/* To make sin square work is is typesd (pow(sin (angle*pi/180), 2.0) */
/* To add a square root sqrt is used */
CoefficientOfActivePressure = (pow (sin ((SlopeAngleOfWall + AngleOfInternalFriction)*pi/180), 2.0)) / ((pow (sin (SlopeAngleOfWall*pi/180), 2.0) * sin ((SlopeAngleOfWall * AngleOfFrictionSoilAndWall) * pi / 180)) *( 1 + sqrt( ( sin ((AngleOfInternalFriction + AngleOfFrictionSoilAndWall) * pi / 180) * sin ( (AngleOfInternalFriction - BackfillSlope) * pi / 180) / sin ( (SlopeAngleOfWall - AngleOfFrictionSoilAndWall) * pi / 180 ) * sin ( (SlopeAngleOfWall + BackfillSlope) * pi / 180)))));
cout << "The coefficient of active pressure acting on the wall is" << CoefficientOfActivePressure <<"\n"
}
It looks like you are trying to implement Coulomb's Theory of Lateral earth pressure. The formula looks like this:
(From http://www.soilmanagementindia.com)
Assuming that your implementation is correct, the only way to get NaN as result is if the square-root argument is negative.
The bottom line is that the equation is not valid for all possible combinations of input, and for the wrong set of input an output of NaN is to be expected.