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=" = ")

运行结果:

python、C++对数,三角函数求值_第1张图片

 

C++:

#include 
#include 

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、C++对数,三角函数求值_第2张图片

 

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

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

 

你可能感兴趣的:(python3,C++)