我正在学习 C 语言初学者,当我运行代码时,它会丢失一些小数位。根据我手写的同类型方程,它应该等于 99.5 有人能告诉我我做错了什么吗?
#include <stdio.h>
//create the number variable
double number = 0;
//function prototypes
double get_input();
double get_next(double one, double two);
void print_result(void);
int main(){
//get all the input needed
double x_two = get_input();
double x_one = get_input();
//calculate third
double x_three = get_next(x_one,x_two);
//calculate fourth
double x_four = get_next(x_three,x_two);
//calulate fith and set to number
number = get_next(x_four,x_three);
//print the result
print_result();
}
double get_input(void){
double number = 0;
//prompt the user for the information needed
printf("Please enter a value > ");
//take the user info and pass it back
scanf("%lf",&number);
return number;
}
double get_next(double minus_one, double minus_two){
double number = (minus_two/2)+(3*minus_one);
return number;
}
void print_result(void){
printf("The result is: %lf",number);
}
方程如下给出 Xn = Xn−2/2 + (3 * xn-1)
我为 n-2 插入的数字是 2,n-3 是 3
最佳答案
您的 get_input() 需要返回一个值。 返回号码;
关于c - c中数字删除小数位的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28493133/