c++ - atanf 给我错误的答案

标签 c++ visual-studio trigonometry

我正在做一本名为 C++ 游戏模块的书的第 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

}

最佳答案

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

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

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

关于c++ - atanf 给我错误的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47846078/

相关文章:

c++ - 构造一个临时对象并调用一个返回指针的方法 - 它安全吗?

jQuery 沿正弦波制作动画

c - 如何改进sin/cos lut函数?

c++ - 为什么在 Solaris 10 中,timer_create 会抛出 SIGEV_THREAD 错误?

c++ - 什么类型的转换适合从 unsigned char* 转换为 char*?

visual-studio - 如何在 Visual Studio 2017 中配置 TypeScript 和 Require

visual-studio - 使用 Visual Studio 的跟踪点以十六进制格式打印表达式结果

visual-studio - "Compare with Workspace version"和 "Compare with Latest version"和有什么区别?

c# - 如何找到与给定向量正交的随机向量

c++ - 对类型 'int' 的非常量左值引用无法绑定(bind)到类型 'int' 的临时对象