c ++ sin不返回正确的值

时间:2015-10-16 04:57:13

标签: c++ trigonometry

我正在尝试完成将度数转换为弧度的程序,反之亦然。之后,用户可以找到罪,余弦,asin和acos。该程序现在不断返回错误的答案。我已经看了几个先前的问题,但没有一个有帮助。错误的答案通常就像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;
}

3 个答案:

答案 0 :(得分:2)

您需要更改PI常量的值,如下所示:

const double PI = atan(1) * 4;

您可以参考:std::atan

答案 1 :(得分:1)

有一个小错误:

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;
}

答案 2 :(得分:1)

感谢大家的帮助。我将x和y移动到局部范围,并在sin()和cos()函数中使用弧度。对于最后两个,我在转换之前调用了asin和acos。

// 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;
}