Checking loop validity without using if statement - c++

My lab professor wants us to use a while statement to have a parabola curving downward (gravity equation), but not going negative without an if statement in c++11. The code below works fine except for the fact that one negative value is still used. Is there a way to remove this last x/y value?
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const double gravAccel = 9.82;
double initHeight;
double itemAngle;
double itemSpeed;
double horzCoord = 0;
double vertCoord;
cout <<"What is the initial height of the projectile?";
cin >> initHeight;
cout <<"What angle is the projectile launched at?";
cin >> itemAngle;
cout <<"What is the projectile's speed?";
cin >> itemSpeed;
while (vertCoord >= 0)
{
vertCoord = initHeight + horzCoord*tan(itemAngle) - ((gravAccel*pow(horzCoord,2))/(2*(pow(itemSpeed*cos(itemAngle),2))));
cout << horzCoord << "meters, " << vertCoord << "meters" << endl;
horzCoord++;
}
return 0; }

use a while statement to have a parabola curving downward (gravity equation), but not going negative without an if statement in c++11
Not really elegant, but you can embed the vertCord assignment in the while() test
while ( (vertCoord = initHeight + horzCoord*tan(itemAngle)
- ((gravAccel*pow(horzCoord,2))/(2*(pow(itemSpeed*cos(itemAngle),2))))) >= 0 )
{
cout << horzCoord << "meters, " << vertCoord << "meters" << endl;
horzCoord++;
}
Off Topic: as far I know, g (gravAccel) isn't 9.82 (m/s^2) but 9.807.

Gravity is approx. 9.81m/(s^2).
It doesn't seem like you are calculating the distance vertically and horizontally correct..
sin/cos/tan in C++ uses radians for the angle parameter.
++ operator on a double is going to increase it by 1.0.
If you want it to never be below zero, why not clamp it to its time? By that I mean calculate the time it takes for the projectile to hit the floor. Then loop for (time - currentTime) with some step while printing the X and Y coordinates of the projectile. This will make sure the vertical coordinate is always > 0.
#include <iostream>
#include <cmath>
using namespace std;
double calculateHorizontalCoordinate(double time, double initial_velocity, double initial_angle)
{
double initial_velocity_x = initial_velocity * cos(initial_angle * M_PI / 180.0);
return initial_velocity_x * time;
}
double calculateVerticalCoordinate(double time, double initial_velocity, double initial_angle)
{
double initial_velocity_y = initial_velocity * sin(initial_angle * M_PI / 180.0);
return (initial_velocity_y * time) - (0.5 * 9.81 * (time * time));
}
double calculateTime(double initial_velocity, double initial_angle)
{
double initial_velocity_y = initial_velocity * sin(initial_angle * M_PI / 180.0);
double time = (2 * initial_velocity_y) / 9.81;
return time;
}
int main() {
double initial_height = 0.0;
double initial_angle = 0.0;
double initial_velocity = 0.0;
double horzCoord = 0.0;
double vertCoord = 0.0;
cout <<"What is the initial height of the projectile?";
cin >> initial_height;
cout <<"What angle is the projectile launched at?";
cin >> initial_angle;
cout <<"What is the projectile's speed?";
cin >> initial_velocity;
double currentTime = 0.0;
double time = calculateTime(initial_velocity, initial_angle);
double step = time / 10.0;
while (time - currentTime >= numeric_limits<double>::min())
{
horzCoord = calculateHorizontalCoordinate(currentTime, initial_velocity, initial_angle);
vertCoord = calculateVerticalCoordinate(currentTime, initial_velocity, initial_angle);
cout << horzCoord << " meters, " << vertCoord << " meters" << endl;
currentTime += step;
}
return 0;
}
Input:
What is the initial height of the projectile? 0
What angle is the projectile launched at? 45
What is the projectile's speed? 25
0 meters, 0 meters
6.37105 meters, 5.73394 meters
12.7421 meters, 10.1937 meters
19.1131 meters, 13.3792 meters
25.4842 meters, 15.2905 meters
31.8552 meters, 15.9276 meters
38.2263 meters, 15.2905 meters
44.5973 meters, 13.3792 meters
50.9684 meters, 10.1937 meters
57.3394 meters, 5.73394 meters
Program ended with exit code: 0

Related

Euler method in c++; Values getting too big too fast

i am trying to solve the equation of motion for a particle with mass m attached to a spring with a spring constant k. Both are set to 1 however.
The algorithm looks like this:
My (attempted) solution, written in c++, looks like this:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <fstream>
// Initialise file to write series of values in
std::ofstream output("Eulermethod.txt");
// Define Euler algorithm
void euler(double x_0, double v_0, double delta, double t_max) {
double x_prev = x_0;
double v_prev = v_0;
double x_new, v_new;
for (double t = 0; t < t_max; t = t + delta) {
x_new = x_prev + t * v_prev;
v_new = v_prev - t * x_prev;
// Writes time, position and velocity into a csv file
output << std::fixed << std::setprecision(3) << t << "," << x_prev << "," << v_prev << std::endl;
x_prev = x_new;
v_prev = v_new;
// Breaks loop if values get to big
if ((x_new != x_new) || (v_new != v_new) || (std::isinf(x_new) == true) || (std::isinf(v_new) == true)) {
break;
}
}
}
int main() {
// Initialize with user input
double x_0, v_0, t_max, delta;
std::cout << "Initial position x0?: ";
std::cin >> x_0;
std::cout << "Intial velocity v0?: ";
std::cin >> v_0;
std::cout << "Up to what time t_max?: ";
std::cin >> t_max;
std::cout << "Step size delta?: ";
std::cin >> delta;
// Runs the function
euler(x_0, v_0, delta, t_max);
}
I know that the solution will grow indefinitely but for smaller values of t it should resemble the analytical solution while growing slowly.
The values i get are blowing out of proportions after ca. 10 iterations and i can not find out why.
When i plot the position as a function of the time i get the plot below, which is obviously wrong.
Your equation implementation is wrong. You are usint t instead of dt. Correct variant:
x_new = x_prev + delta * v_prev;
v_new = v_prev - delta * x_prev;
And a side note if you plan to develop your code further: common approach to implementation of ODE solver is to have a method with signature similar to
Output = solveOde(System, y0, t);
Where System is method that describes the ODE dy/dx = f(x,t), e.g.
std::vector<double> yourSystem(std::vector<double> y, double /*t unused*/)
{
return {y[1], -y[0]};
}
y0 are initial conditions, and t is a time vector (delta is calculated internally). Take a look at boost odeint or more compact and transparent python documentation.

Double-Pendulum Equation Bug

I have been making a double-pendulum simulator in C++ using Raylib and I have finished everything other than correctly implementing the equations. When doing so, however, no matter what I try, I can't seem to figure out why my implementation of the equations is not working. Most likely I am missing something fundamental but it's so hectic as is that it's difficult for me to understand. How would I implement these equations successfully? For reference:
Angular acceleration for pendulum 1 is angularA1
Angular acceleration for pendulum 2 is angularA2
I split the numerators in the equations into parts because it's simply too hard to keep track of it on one line. The problem is in main.cpp and the file generator is completely separate from the pendulum.
pendulum.h:
#ifndef DOUBLE_PENDULUM_SIM_PENDULUM_H
#define DOUBLE_PENDULUM_SIM_PENDULUM_H
class pendulum {
public:
//Pendulum length
float pLength{};
//Pendulum mass
float pMass{};
//Pendulum's x component
float x{};
//Pendulum's y component
float y{};
//Pendulum's angular displacement relative to its origin
float pAngle{};
public:
void setAngularV(float angV);
void setAngularA(float angA);
[[nodiscard]] float getX() const ;
[[nodiscard]] float getY() const ;
[[nodiscard]] float getAngle() const ;
[[nodiscard]] float getMass() const;
[[nodiscard]] float getLength() const;
void setX(float length, float angle);
void setY(float length, float angle);
void setLength(float length);
void setMass(float mass);
void setAngle(float angle);
//Default constructor
pendulum();
//Easier way to initialize pendulum
pendulum(float length, float mass, float angle) : pLength(length), pMass(mass), pAngle(angle) {}
//De-constructor to make sure pendulum objects are destroyed
~pendulum();
};
#endif //DOUBLE_PENDULUM_SIM_PENDULUM_H
pendulum.cpp:
#include "includes/pendulum.h"
#include <cmath>
#include <iostream>
#include <raylib.h>
void pendulum::setX(float length, float angle) {
x = length * sin(angle);
}
void pendulum::setY(float length, float angle) {
y = length * cos(angle);
}
float pendulum::getX() const {
return x;
}
float pendulum::getY() const {
return y;
}
void pendulum::setLength(float length) {
pLength = length;
}
void pendulum::setMass(float mass) {
pMass = mass;
}
float pendulum::getAngle() const {
return pAngle;
}
pendulum::~pendulum() {
std::cout << "\nPendulum destroyed" << std::endl;
}
void pendulum::setAngle(float angle) {
pAngle = angle * DEG2RAD;
}
float pendulum::getMass() const {
return pMass;
}
float pendulum::getLength() const {
return pLength;
}
//Default constructor
pendulum::pendulum() = default;
main.cpp:
#include <iostream>
#include <fstream>
#include "raylib.h"
#include "includes/pendulum.h"
#include "includes/GenerateFile.h"
#include <cmath>
void testPendulum();
GenerateFile generator;
pendulum pen1;
pendulum pen2;
//The universal constant for gravity, for these purposes however, it can be any value as long as it works
const float g = 1;
int main() {
//Prompt to make the initial values themselves
float uLength1, uLength2, uMass1, uMass2, uAngle1, uAngle2;
//Recommendation: 100 200 for length, 20 25 for mass, 45 0 for angle
try {
std::cout << "Please choose the length of each pendulum, starting with Pendulum 1, then Pendulum 2. Each value provided can be up to 7 decimal digits, " << "\n" << "length MUST BE greater than 50 and less than 200" << "\n";
std::cin >> uLength1 >> uLength2;
std::cout << "Please choose the mass of each pendulum, starting with Pendulum 1, then Pendulum 2. Each value provided can be up to 7 decimal digits, " << "\n" << "mass MUST BE greater than 20 and less than 100" << "\n";
std::cin >> uMass1 >> uMass2;
std::cout << "Please choose the starting angle of each pendulum, starting with Pendulum 1, then Pendulum 2. Each value provided can be up to 7 decimal digits" << "\n";
std::cin >> uAngle1 >> uAngle2;
} catch (const std::exception & e) {
std::cout << e.what();
}
//Init angular acceleration and angular velocity for pendulums
float angularV1 = 0;
float angularV2 = 0;
//Pendulum 1 settings
pen1.setMass(uMass1);
pen1.setLength(uLength1);
pen1.setAngle(uAngle1);
pen1.setX(pen1.pLength,pen1.getAngle());
pen1.setY(pen1.pLength, pen1.getAngle());
std::cout << "X coord: " << pen1.getX() << " Y coord: " << pen1.getY() << std::endl;
//Pendulum 2 settings
pen2.setMass(uMass2);
pen2.setLength(uLength2);
pen2.setAngle(uAngle2); //Can only set this once and cant anywhere else, why?
pen2.setX( pen2.pLength,pen2.getAngle());
pen2.setY( pen2.pLength,pen2.getAngle());
pen2.x = pen1.getX() + pen2.getX();
pen2.y = pen1.getY() + pen2.getY();
std::cout << "X coord: " << pen2.getX() << " Y coord: " << pen2.getY() << std::endl;
//Window settings
const double screenWidth = 1440;
const double screenHeight = 1080;
Vector2 origin{(float) screenWidth/2,(float) screenHeight/3};
InitWindow((int) screenWidth, (int) screenHeight, "Double-Pendulum-Sim");
int frameCounter = 0;
SetTargetFPS(60);
//Set coords for pendulums
float px1 = pen1.getX() + origin.x;
float py1 = pen1.getY() + origin.y;
float px2 = pen2.getX() + origin.x;
float py2 = pen2.getY() + origin.y;
//Load & start pathing
RenderTexture2D target = LoadRenderTexture((int) screenWidth, (int) screenHeight);
BeginTextureMode(target);
ClearBackground(RAYWHITE);
EndTextureMode();
/****Write data to a file*****/
//Init new Values.txt file
generator.file.open("Values.txt");
if(!generator.file) {
perror("Error finding or opening file");
} else {
std::cout << "File opened successfully" << std::endl;
}
generator.file << "Angle 1 | X_1 | Y_1 | Angle 2 | X_2 | Y_2 | Frame # " << std::endl;
//Main while loop
while (!WindowShouldClose()) {
Vector2 rod1{px1,py1};
Vector2 rod2 {px2, py2};
//Implement angular acceleration for first pendulum equations:
float num1 = -g * (2 * pen1.getMass() + pen2.getMass()) * sin(pen1.getAngle());
float num2 = -pen2.getMass() * g * sin(pen1.getAngle() - 2 * pen2.getAngle());
float num3 = -2 * sin(pen1.getAngle() - pen2.getAngle()) * pen2.getMass();
float num4 = pow(angularV2, 2) * pen2.getLength() + pow(angularV1,2) * pen1.getLength() * cos(pen1.getAngle() - pen2.getAngle());
float den1 = pen1.getLength() * (2*pen1.getMass() + pen2.getMass() - pen2.getMass() * cos(2*pen1.getAngle() - 2 * pen2.getAngle()));
float angularA1 = (num1 + num2 + num3*num4) / den1;
//Angular acceleration for second pendulum:
num1 = 2 * sin(pen1.getAngle() - pen2.getAngle());
num2 = (pow(angularV1,2.0) * pen1.getLength() * (pen1.getMass() + pen2.getMass()));
num3 = g * (pen1.getMass() + pen2.getMass()) * cos(pen1.getAngle());
num4 = pow(angularV2,2.0) * pen2.getLength() * pen2.getMass() * cos(pen1.getAngle() - pen2.getAngle());
den1 = pen2.getLength() * (2*pen1.getMass() + pen2.getMass() - pen2.getMass() * cos(2*pen1.getAngle() - 2*pen2.getAngle()));
float angularA2 = (num1 * (num2 + num3 + num4)) / den1;
/**------------------Update------------------*/
frameCounter++;
uAngle1 += angularV1;
angularV1 += angularA1;
pen1.setAngle(uAngle1); //Can only set this once and cant anywhere else, why?
pen1.setX(pen1.pLength,pen1.getAngle());
pen1.setY(pen1.pLength, pen1.getAngle());
px1 = pen1.getX() + origin.x;
py1 = pen1.getY() + origin.y;
uAngle2 += angularV2;
angularV2 += angularA2;
pen2.setAngle(uAngle2); //Can only set this once and cant anywhere else, why?
pen2.setX( pen2.pLength,pen2.getAngle());
pen2.setY( pen2.pLength,pen2.getAngle());
pen2.x = pen1.getX() + pen2.getX();
pen2.y = pen1.getY() + pen2.getY();
px2 = pen2.getX() + origin.x;
py2 = pen2.getY() + origin.y;
//Write data to file by first getting the values of the pendulums:
generator.getData(std::to_string(frameCounter),std::to_string(uAngle1),std::to_string(px1),std::to_string(px2),std::to_string(uAngle2),std::to_string(px2),std::to_string(py2));
/**---------------------------------Draw-Pendulums & Path---------------------------------- */
BeginDrawing();
BeginTextureMode(target);
DrawCircleV(rod2, 2.0f, RED);
// DrawPixelV(rod2, RED);
EndTextureMode();
DrawTextureRec(target.texture, (Rectangle){0,0, (float) target.texture.width, (float) -target.texture.height}, (Vector2){0,0}, WHITE);
ClearBackground(RAYWHITE);
DrawFPS(100, 100);
DrawLineEx(origin, rod1, 5.0f, BLACK);
DrawCircle( px1,py1,pen1.pMass,BLACK);
DrawLineEx(rod1, rod2, 5.0f, BLACK);
DrawCircle(px2,py2,pen2.pMass,BLACK);
std::cout << "Frame #: " << frameCounter << std::endl;
std::cout << "Pendulum 1 Angle: " << uAngle1 << std::endl;
std::cout << "Pendulum 2 Angle: " << uAngle2 << std::endl;
std::cout << "Pendulum 1 X & Y: " << pen1.getX() << " " << pen1.getY() << std::endl;
std::cout << "Pendulum 2 X & Y: " << pen2.getX() << " " << pen2.getY() << std::endl;
EndDrawing();
}
CloseWindow();
generator.file.close();
return 0;
}
//Test function
void testPendulum() {
try {
pen1.setMass(20.0f);
pen1.setLength(150.0f);
pen1.setAngle(0.0f);
pen1.setX(pen1.pLength,pen1.getAngle());
pen1.setY(pen1.pLength, pen1.getAngle());
std::cout << "X coord: " << pen1.getX() << " Y coord: " << pen1.getY() << std::endl;
pen2.setMass(50.0f);
pen2.setLength(150.0f);
pen2.setAngle(0.0f);
pen2.setX( pen2.pLength,pen2.getAngle());
pen2.setY( pen2.pLength,pen2.getAngle());
pen2.x = pen1.getX() + pen2.getX();
pen2.y = pen1.getY() + pen2.getY();
std::cout << "X coord: " << pen2.getX() << " Y coord: " << pen2.getY() << std::endl;
} catch (const std::exception & e) {
std::cout << e.what();
}
}
These are the Double Pendulum Equations I'm trying to use:
Be careful, you've made no attempt to use sane units in your code. You've mismatched degrees and radians in at least one spot too by doing uAngle1 += angularV1. uAngle1 is in degrees, but the angularV1 should likely be produced in radians per second.
That brings in the missing aspect of time in your numerical integration.
Numerical integration typically works by assuming velocity is linear under short time intervals.
A typical numerical integration inner loop calculation looks like this:
acceleration = netForce / mass;
velocity += acceleration * timestep;
position += velocity * timestep;
Whereas you just do:
velocity += acceleration;
position += velocity;
...which is like using a timestep of 1.
The timestep must be small. For simulations that make gradual changes, it may be sufficient to just use the frame interval (e.g.: use a timestep of 1/60th of a second for a 60 fps simulation). Some simulations will actually subdivide the frame interval into a number of simulations steps (e.g. you could perform 100 updates per frame, each with a very small timestep of ((1/60) / 100).
Whatever happens, you should reconcile your units. Make sure your gravitational constant makes sense given the scale of your world. g = 1 is likely much too large. (#Gene made an excellent comment to this effect.) Also make sure your timesteps are reasonable. An implicit timestep may be interpreted as a time interval of 1 second (instead of 1 frame) and your simulation may be running an additional 60 times faster than you expect. All this leads to large values and numerical instability in the integration calculation, which depends on small changes.
You're currently using a distance unit of pixels, but perhaps it makes more sense to use a unit of meters and then scale your objects to fit the screen with a sane camera and viewport calculation.
You're currently using a time unit of frames, but perhaps it makes more sense to use a unit of seconds and make use of either the known frames-per-second rate of your simulation, or keep track of the actual elapsed time with a clock so that the simulation doesn't stutter if frames are dropped.
You're mismatching degrees and radians. It makes sense to use degrees when interacting with the user and radians in your physics calculations, but be careful not to mix them up during your calculations.

C++ Console - New lat/lon coordinate using azimuth and range [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I would like assistance understanding this code I found during a search. I have been stuck for 2 weeks trying to get it and it's holding my project back. I am honestly trying to learn as I go and not just be a script kiddie about it, but this is much more complicated than the rest of my project even though I am trying. (I just learned about auto today while trying to understand this code, for example.)
I am working on a weather app and I know the lat/lon of the radar site. I need the lat/lon of a feature the radar has detected based off of the azimuth/range that the radar tells me (example, 271 degrees and 7 nautical miles). I need to understand how I can use this code below to convert the azimuth/range to a new lat lon coordinate. I don't require the other functions, just to be able to put my variables (starting coords, azimuth and range) and get a result. The code below looks to do much more than that, and it is confusing me.
I see the following code near the end :
auto coordinate = CoordinateToCoordinate(latitude1, longitude1, angle, meters);
... Which looks to be the part I would need out of this. I see how it's calculating it but once I dig deeper I just get myself confused. I have tried hacking at the code so much that I gave up and don't even have any examples.
I would like to be able to set my variables manually (example cin>>) and have the lat and lon output into a variable that I can save to a text file. I am able to do everything myself (ingesting the starting variables and writing the result to a text file) except the actual conversion itself.
How could I get started with this using the code below?
My example variables are :
Original Latitude = 29.4214
Original Longitude = -98.0142
Azimuth from Origin = 271 degrees
Range from Origin = 6 nautical miles (I can convert to meters if needed,
in this case it's 11112 meters)
The actual unedited code is below and a copy at this link. If I get help with this I won't just copy/paste and I will come back with the completed code after I make it. I really am wanting to understand as I go, so I can be better with these advanced topics and not be constrained in the future. Code below :
#include<iostream>
#include<iomanip>
#include<cmath>
// Source: // http://w...content-available-to-author-only...o.uk/scripts/latlong.html
static const double PI = 3.14159265358979323846, earthDiameterMeters = 6371.0 * 2 * 1000;
double degreeToRadian (const double degree) { return (degree * PI / 180); };
double radianToDegree (const double radian) { return (radian * 180 / PI); };
double CoordinatesToAngle (double latitude1,
const double longitude1,
double latitude2,
const double longitude2)
{
const auto longitudeDifference = degreeToRadian(longitude2 - longitude1);
latitude1 = degreeToRadian(latitude1);
latitude2 = degreeToRadian(latitude2);
using namespace std;
const auto x = (cos(latitude1) * sin(latitude2)) -
(sin(latitude1) * cos(latitude2) * cos(longitudeDifference));
const auto y = sin(longitudeDifference) * cos(latitude2);
const auto degree = radianToDegree(atan2(y, x));
return (degree >= 0)? degree : (degree + 360);
}
double CoordinatesToMeters (double latitude1,
double longitude1,
double latitude2,
double longitude2)
{
latitude1 = degreeToRadian(latitude1);
longitude1 = degreeToRadian(longitude1);
latitude2 = degreeToRadian(latitude2);
longitude2 = degreeToRadian(longitude2);
using namespace std;
auto x = sin((latitude2 - latitude1) / 2), y = sin((longitude2 - longitude1) / 2);
#if 1
return earthDiameterMeters * asin(sqrt((x * x) + (cos(latitude1) * cos(latitude2) * y * y)));
#else
auto value = (x * x) + (cos(latitude1) * cos(latitude2) * y * y);
return earthDiameterMeters * atan2(sqrt(value), sqrt(1 - value));
#endif
}
std::pair<double,double> CoordinateToCoordinate (double latitude,
double longitude,
double angle,
double meters)
{
latitude = degreeToRadian(latitude);
longitude = degreeToRadian(longitude);
angle = degreeToRadian(angle);
meters *= 2 / earthDiameterMeters;
using namespace std;
pair<double,double> coordinate;
coordinate.first = asin((sin(latitude) * cos(meters))
+ (cos(latitude) * sin(meters) * cos(angle)));
coordinate.second = longitude + atan2((sin(angle) * sin(meters) * cos(latitude)),
cos(meters) - (sin(latitude) * sin(coordinate.first)));
coordinate.first = radianToDegree(coordinate.first);
coordinate.second = radianToDegree(coordinate.second);
return coordinate;
}
int main ()
{
using namespace std;
const auto latitude1 = 12.968460, longitude1 = 77.641308,
latitude2 = 12.967862, longitude2 = 77.653130;
cout << std::setprecision(10);
cout << "(" << latitude1 << "," << longitude1 << ") --- "
"(" << latitude2 << "," << longitude2 << ")\n";
auto angle = CoordinatesToAngle(latitude1, longitude1, latitude2, longitude2);
cout << "Angle = " << angle << endl;
auto meters = CoordinatesToMeters(latitude1, longitude1, latitude2, longitude2);
cout << "Meters = " << meters << endl;
auto coordinate = CoordinateToCoordinate(latitude1, longitude1, angle, meters);
cout << "Destination = (" << coordinate.first << "," << coordinate.second << ")\n";
}
I think you just want something like this:
#include <iostream>
std::pair<double,double> CoordinateToCoordinate (double latitude,
double longitude,
double angle,
double meters)
{
...
...
}
using namespace std;
int main() {
double lat, lon, angle, dist;
cout << "Enter lat:"; cin >> lat;
cout << "Enter lon:"; cin >> lon;
cout << "Enter angle:"; cin >> angle;
cout << "Enter dist:"; cin >> dist;
auto coordinate = CoordinateToCoordinate(lat, lon, angle, dist);
cout << "Destination = (" << coordinate.first << "," << coordinate.second << ")\n";
}

atanf gives me wrong answers

I was doing Exercise of Chapter 3 (Functions) from a Book Called C++ Modules for Gaming.
It is this one question I am not able to Do is to find atanf(4/2) of (2,4) which according to the book and my calculator should give back '63.42' degrees.
Instead it gives me 1.107 degrees.
Here is my code:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
void tani(float a,float b) //Finds the Tan inverse
{
float res;
res = atanf(b / a);
cout << res << endl;
}
int main()
{
cout << "Enter The Points X and Y: " << endl;
float x, y;
cin >> x >> y; //Input
tani(x,y); //calling Function
}
atanf, and the other trigonometric functions in c++ return results in radians. 1.107 radians are 63.426428 degress, so your code is correct.
You can convert radians to degrees by multiplying by 180 and dividing by Pi (the M_PI constant provided by <cmath>):
cout << res * 180.0 / M_PI << endl;
It is giving you correct answer in radians.Simply Convert it to Degree!
void tani(float a, float b) //Finds the Tan inverse
{
float res;
res = atanf(b/ a);
cout << res *(180 / 3.14) << endl;
}

C++ program to build a Trigonometric Calculator

I am novice at programming in C++. I want to write a program using while loop which displays the trigonometric table for sin, cos and Tan. It takes angles in degrees with a difference of 5 and displays the result. This it what I tried,
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int num;
cout<< "Angle Sin Cos Tan"<<endl;
cout<< "..........................."<<endl;
num=0;
while (num<=360)
{
cout <<setw(3)<<num<<" "
<<setw(3)<<setprecision(3)<<sin(num)<<" "
<<setw(3)<<setprecision(3)<<cos(num)<<" "
<<setw(5)<<setprecision(3)<<tan(num)<<endl;
num=num+5;
}
}
Unfortunately, I could not change radians into degrees in while loop and the display does not look promising even for radians. How can I resolve it ?
To convert degrees to radiant you have to multiply by pi and to divide by 180.0:
#define M_PI 3.14159265358979323846
int num = 0;
while (num<=360)
{
double numRad = num * M_PI/180.0;
std::cout <<std::setw(3)<<num<<" "
<<std::setprecision(3)<<std::fixed
<<std::setw(6)<< std::sin( numRad ) <<" "
<<std::setw(6)<< std::cos( numRad ) <<" ";
if ( num != 90 && num != 270 )
std::cout<<std::setw(6)<< std::tan( numRad ) <<std::endl;
else
std::cout<< "infinitely" <<std::endl;
num=num+5;
}
To use constant M_PI see How to use the PI constant in C++
To convert degrees to radians, use numRad = M_PI / 180.0 where M_PI should be a constant that holds the value od Pi. If you do not have such a constant defined in a header file, just define it yourself, like #define PI 3.14159265
The functions sin, cos and tan always require arguments in radians.