python、C++对数,三角函数求值

python:

import math


print("ln(100)", math.log(100), sep=" = ")
print("log2(256)", math.log(256, 2), sep=" = ", end="\n\n")

print("sin(30°)", math.sin(math.radians(30)), sep=" = ")
print("cos(60°)", math.cos(math.radians(60)), sep=" = ")
print("tan(45°)", math.tan(math.radians(45)), sep=" = ", end="\n\n")

print("asin(0.5)", math.degrees(math.asin(0.5)), sep=" = ")
print("acos(0.5)", math.degrees(math.acos(0.5)), sep=" = ")
print("atan(1)", math.degrees(math.atan(1)), sep=" = ")

运行结果:

 

C++:

#include <iostream>
#include <cmath>

const double PI = acos(-1);

using namespace std;


int main() {
	cout << "ln(100) = " << log(100.0) << endl;
	cout << "log2(256) = " << log(256.0) / log(2.0) << endl << endl;

	cout << "sin(30°) = " << sin(30.0 * PI / 180.0) << endl;
	cout << "cos(60°) = " << cos(60.0 * PI / 180.0) << endl;
	cout << "tan(45°) = " << tan(45.0 * PI / 180.0) << endl << endl;

	cout << "asin(0.5) = " << asin(0.5) / PI * 180.0 << endl;
	cout << "acos(0.5) = " << acos(0.5) / PI * 180.0 << endl;
	cout << "atan(1) = " << atan(1) / PI * 180.0 << endl;

	return 0;
}

运行结果:

 

可以看到,python对于 sin(30°) cos(60°) asin(0.5) acos(0.5) 的求值并不准确。

如何用python调用C++函数?

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值