c – math.h的问题

c – math.h的问题,第1张

概述我正在编写一个使用math.h库函数sin()和cos()的程序.但是,我注意到我得到了时髦的结果.在搜索并多次检查我的数学后,我决定用这个做一个简单的检查: int main(){ cout << "sin(45.0) = " << sin(45) << endl; cout << "cos(45.0) = " << cos(45) << endl; return 0 我正在编写一个使用math.h库函数sin()和cos()的程序.但是,我注意到我得到了时髦的结果.在搜索并多次检查我的数学后,我决定用这个做一个简单的检查:

int main(){    cout << "sin(45.0) = " << sin(45) << endl;    cout << "cos(45.0) = " << cos(45) << endl;    return 0;}

我得到这个输出:

sin(45) = 0.850904 cos(45) = 0.525322

这些应该是平等的吗? math.h库有什么特别之处吗?难道我做错了什么?

以下是WolframAlpha中的公式:

sin(45)

@L_403_1@

解决方法 您应该在C中使用 cmath,而不是旧的C头.

std::sin()std::cos()都采用浮点值表示radian中的角度.

The GCC version of this file includes a handy constant for π,(在C标准中不存在但是)这将使您更容易将度数转换为弧度:

#include <cmath>#include <iostream>double degrees_to_radian(double deg){    return deg * M_PI / 180.0;}int main(){    std::cout << "sin(45.0) = " << std::sin(degrees_to_radian(45)) << std::endl;    std::cout << "cos(45.0) = " << std::cos(degrees_to_radian(45)) << std::endl;}

See it run!

总结

以上是内存溢出为你收集整理的c – math.h的问题全部内容,希望文章能够帮你解决c – math.h的问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1224015.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存