atanf给了我错误的答案

问题描述 投票:2回答:2

我正在从一本名为C ++ Modules for Gaming的书中练习第3章(功能)。这是我无法做的一个问题是找到(2,4)的atanf(4/2),根据书和我的计算器应该回馈'63 .42'度。

相反,它给了我1.107度。

这是我的代码:

#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

}
c++ visual-studio trigonometry
2个回答
5
投票

atanf中的其他三角函数返回结果radians。 1.107弧度是63.426428度,所以你的代码是正确的。

你可以通过乘以180并除以Pi(由M_PI提供的<cmath>常数)将弧度转换为度数:

cout << res * 180.0 / M_PI << endl;

1
投票

它给你正确的弧度答案。简单地转换为学位!

void tani(float a, float b) //Finds the Tan inverse
{
    float res;
    res = atanf(b/ a);
    cout << res *(180 / 3.14) << endl;
}
© www.soinside.com 2019 - 2024. All rights reserved.