我有一个涉及浮点数和整数的函数。当我运行代码时,结果是错误的。当我为 input_voltage 输入 4.3 时,十进制为 3803,十六进制为 EDB。正确答案应该是 3808 和 EE0。谁能告诉我可能会发生什么?
#include <stdio.h>
int digital_encoding(float voltage);
int main()
{
float input_voltage;
int valid, ch;
do{
printf("Please enter the input voltage between 0 and 5 volts: \n");
valid = scanf("%f", &input_voltage);
if(input_voltage <= 0){
printf("Enter a number larger than 0! \n");
valid = 0;
}
if(input_voltage >= 5){
printf("Enter a number less than 5! \n");
valid = 0;
}
}while(valid != 1);
digital_encoding(input_voltage);
}
int digital_encoding(float voltage)
{
int dig_encode;
dig_encode = ((voltage + 5)*(4095/10));
printf("The digital encoding equals %d in decimal, and %X in HEX.\n", dig_encode, dig_encode);
return dig_encode;
}