列写三角函数表(C语言实现)

本文介绍了使用C语言实现三角函数表的方法,包括cos、sin和tan函数。借助ceil函数,程序能根据输入的起始值、终止值和步长,生成并打印出相应的三角函数值。
摘要由CSDN通过智能技术生成

下面是用函数指针实现的三角函数表,其中cos函数、sin函数和tan函数都已经在<math.h>中定义,函数tabulate中使用的函数ceil也属于<math.h>,当给定double型实参x时,函数ceil会返回大于或等于x的最小整数。当给定三角函数的起始值,终止值和步长以后,程序会打印出相对应的三角函数值。

#include <math.h>
#include <stdio.h>

// 传递函数指针f时,tabulate函数显示函数f的值
void tabulate(double (*f)(double), double first, double last, double incr);

int main(void)
{
    double initial, final, increment;

  
C语言中,可以使用数学函数<math.h>中的sin、cos、tan和ctan函数来计算正弦、余弦、正切和余切。但如果想要自己实现这些函数,可以使用泰勒级数公式来进行计算。下面是一个示例代码: ```c #include <stdio.h> #include <math.h> double my_sin(double x) { double result = 0.0; double term = x; double sign = 1.0; int i; for (i = 1; i <= 10; i++) { result += term; term *= (-1.0 * x * x) / ((2 * i) * (2 * i + 1)); sign *= -1.0; } return result; } double my_cos(double x) { double result = 0.0; double term = 1.0; double sign = 1.0; int i; for (i = 0; i <= 10; i++) { result += term; term *= (-1.0 * x * x) / ((2 * i + 2) * (2 * i + 1)); sign *= -1.0; } return result; } double my_tan(double x) { return my_sin(x) / my_cos(x); } double my_ctan(double x) { return my_cos(x) / my_sin(x); } int main() { double angle = 45.0; double radian = angle * M_PI / 180.0; double sin_value = my_sin(radian); double cos_value = my_cos(radian); double tan_value = my_tan(radian); double ctan_value = my_ctan(radian); printf("sin(%f) = %f\n", angle, sin_value); printf("cos(%f) = %f\n", angle, cos_value); printf("tan(%f) = %f\n", angle, tan_value); printf("ctan(%f) = %f\n", angle, ctan_value); return 0; } ``` 在上面的代码中,我们分别定义了my_sin、my_cos、my_tan和my_ctan函数来计算正弦、余弦、正切和余切值。在函数中,我们使用了泰勒级数公式来进行计算。最后,我们使用printf函数将结果打印到屏幕上。值得注意的是,由于泰勒级数公式的精度受到项数的影响,因此在实际使用中,需要根据实际情况来选择合适的项数。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值