数字在c中删除小数位的错误

时间:2015-02-13 05:33:48

标签: c console line rounding

我在初学者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 = X n-2 / 2 +(3 * x n-1

我插入n-2的数字是2,n-3是3

1 个答案:

答案 0 :(得分:0)

你的get_input()需要返回一个值。 return number;