《quantitative finance with cpp》阅读笔记21---极坐标转换成直角坐标
作者:yunjinqi   类别:    日期:2024-01-01 14:13:59    阅读:258 次   消耗积分:0 分    

Write a function polarToCartesian which takes four parameters: r, theta, x, and y. x and y should be passed by pointers. The function should populate the values pointed to by x and y with the r cos(θ) and r sin(θ). Write a test for this function. In the C language, one does not have references, so passing by pointer is the C equivalent of passing by reference.

void polarToCartesian(double r, double theta,
	double* x, double* y) {
	*x = r*cos(theta);
	*y = r*sin(theta);
}

void testPolarToCartesian() {
	double r = 2;
	double theta = atan(1);
	double x;
	double y;
	polarToCartesian(r, theta, &x, &y);
	ASSERT_APPROX_EQUAL(x, sqrt(2), 0.0001);
	ASSERT_APPROX_EQUAL(y, sqrt(2), 0.0001);
}


版权所有,转载本站文章请注明出处:云子量化, http://www.woniunote.com/article/386
上一篇:《quantitative finance with cpp》阅读笔记20---计算一系列点离原点的平均距离
下一篇:《quantitative finance with cpp》阅读笔记22---查询字符串出现的次数