#define _CRT_SECURE_NO_WARNINGS 1 //我用的是VS2017不加这句话scanf要换成scanf_s
#include<stdio.h>
#include<math.h>
float f(float x);
float fun(float a, float b, int n, float(*p)(float x));
void print(float r);
int main() {
float a = 0, b = 0, r = 0;
printf("请输入求sin(x)*pow(x,2)-7*pow(x,2)-8定积分的上限a和下限b:");
scanf("%f%f", &a, &b);
r = fun(a, b, 50, f);
print(r);
return 0;
}
float f(float x) {
return (sin(x)*pow(x, 6) - 7 * pow(x, 2) - 8);
}
float fun(float a, float b, int n, float(*p)(float x))
{
float i;
float h = (b - a) / n;
float s = 0;
for (i = a;i < b;i += h) {
s = s + (((*p)(i) + (*p)(i + h))*h) / 2;
}
return s;
}
void print(float r)
{
printf("结果为:%f", r);
}
问题如下:(17): warning C4244: “return”: 从“double”转换到“float”,可能丢失数据
我尝试改了几个地方的“float”但只要改了就一定会报错,不知道应该改哪里?是需要大改吗?