Cameron Hall
Cameron Hall

Reputation: 91

c++ sin not return correct values

I am trying to complete a program that converts degrees to radians and vice versa. After that, users can find sin, cosine, asin and acos. The program now keeps returning incorrect answers. I have looked through a few previous questions but none of them have helped. The incorrect answer is usually like sin(90)=.84654648

// File Name: program6.cpp
// Author: Cameron Hall
// Student ID: t997f229
// Assignment Number: 6
// Description: This program reads some text entered by the user, then in functions that return sine and cosin
// Last Changed: October 15, 2015

#include <iostream>
#include <cmath>

using namespace std;
int degrees, radians;
const double PI= 3.14159265359; //Declare PI
double x;           // x for radians
int y;              // for arc-sin and arc-cosinc
// Returns x degrees converted to radians

double degrees_to_radians(double y) {
    double radians= (x * PI)/180;
    return radians;
}

// Returns x radians converted to degrees

double radians_to_degrees(double x) {
    degrees= (y * 180)/PI;
    return degrees;
}

int main(void) {
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);


int choice;     // For input needed to plug into function 
while (choice != -1) {
cout << "Trigonometric function calculator: " << endl;
cout << "1. Sine" << endl;
cout << "2. Cosine" << endl;
cout << "3. Arc-sine" << endl;
cout << "4. Arc-cosine" << endl;
cout << "5. Quit" << endl;
cout << "Enter the number of your choice from the menu: " << endl;
cin >> choice; 
switch (choice) {

    case 1: cout << "\n To find sin(x), enter a value for x: ";
        cin >> x;
        radians= degrees_to_radians(x);
        cout << "sin(" << x << ") =" << sin(radians) << endl;       
        break;
        case 2: cout << "\n To find cosine(x) where x is an angle in degrees, enter a value for x: " << endl;
        cin >> x;
        radians= degrees_to_radians(x);
        cout << "cosine(" << x << ") =" << cos(radians) << endl;
        break;
    case 3: cout << "\n To find asin(y) where y is an angle in degrees, enter a value for y: " << endl;
        cin >> y;
        degrees= radians_to_degrees(y);
        cout << "asin(" << y << ") =" << sin(radians) << endl;
        break;
    case 4: cout << "\n To find acos(y), enter a value for y: " << endl;
        cin >> y;
        degrees= radians_to_degrees(y);
        cout << "acos(" << y << ") =" << acos(radians) << endl;
        break;
    case 5: return (0);
}
}
return 0;
}

Upvotes: 2

Views: 3522

Answers (3)

Cameron Hall
Cameron Hall

Reputation: 91

Thank you for the help everybody. I moved x and y into the local scope and used radians in my sin() and cos() functions. For the last two, I called the asin and acos before converting.

// File Name: program6.cpp
// Author: Cameron Hall
// Student ID: t997f229
// Assignment Number: 6
// Description: This program reads some text entered by the user, then in functions that return sine and cosin
// Last Changed: October 15, 2015

#include <iostream>
#include <cmath>

using namespace std;
const double PI= 3.14159265359; //Declare PI
// Returns x degrees converted to radians
double degrees, radians;
double degrees_to_radians(double x) {
    double radians= (x * PI)/180;
    return radians;
}

// Returns x radians converted to degrees

double radians_to_degrees(double radians) {
    degrees= (radians * 180)/PI;
    return degrees;
}

int main(void) {
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
double x, y;

int choice;     // For input needed to plug into function 
do {
cout << "Trigonometric function calculator: " << endl;
cout << "1. Sine" << endl;
cout << "2. Cosine" << endl;
cout << "3. Arc-sine" << endl;
cout << "4. Arc-cosine" << endl;
cout << "5. Quit" << endl;
cout << "Enter the number of your choice from the menu: " << endl;
cin >> choice; 
switch (choice) {

    case 1: cout << "\n To find sin(x), enter a value for x: ";
        cin >> x;
        radians= degrees_to_radians(x);
        cout << "sin(" << x << ") =" << sin(radians) << endl;       
        break;
        case 2: cout << "\n To find cosine(x) where x is an angle in degrees, enter a value for x: " << endl;
        cin >> x;
        radians= degrees_to_radians(x);
        cout << "cosine(" << x << ") =" << cos(radians) << endl;
        break;
    case 3: cout << "\n To find asin(y), enter a value for y: " << endl;
        cin >> y;
        while (y<-1 || y>1) {
        cout << "Please input a number ranging from -1 to 1" << endl;
        cin >> y;
        }
        radians=asin(y);
        degrees= radians_to_degrees(radians);
        cout << "asin(" << y << ") =" << degrees << endl;
        break;
    case 4: cout << "\n To find acos(y), enter a value for y: " << endl;
        cin >> y;
        while (y<-1 || y>1) {
        cout << "Please input a number ranging from -1 to 1" << endl;
        cin >> y;
        }
        radians=acos(y);
        degrees= radians_to_degrees(radians);
        cout << "acos(" << y << ") =" << degrees << endl;
        break;
    case 5: return (0);
}
} while (choice>0 && choice<6);
return 0;
}

Upvotes: 1

Humam Helfawi
Humam Helfawi

Reputation: 20311

there is small mistake:

double degrees_to_radians(double y) {
    double radians= (y * PI)/180; // y not x
    return radians;
}

// Returns x radians converted to degrees

double radians_to_degrees(double x) {
    degrees= (y * 180)/PI; // y not x
    return degrees;
}

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172628

You need to change the value of your PI constant like this:

const double PI = atan(1) * 4;

You can refer: std::atan

Upvotes: 2

Related Questions