程序返回inf? [英] Program returning inf?

查看:246
本文介绍了程序返回inf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为错误在于这一行:

pre $ float bmi = weight * 703 / pow(height,2.0); //用户输入重量和高度,并且应该计算bmi。

事情是,我创建了一个模拟程序,其值已经存在,一切都很好。但是,当我使用scanf()让用户把这些相同的值它不起作用。

  #include  
#include< math.h>

int main()
{
float weight = 105,height = 63;
float bmi;
bmi = weight * 703 / pow(height,2.0);
printf(%f \ n,bmi);

重量和高度都是浮动的。当我尝试打印bmi时,我得到inf。

编辑显示输入函数和打印语句。

  printf(Weight?); 
scanf(%f,& weight);

printf(BMI is%.2f.\\\
,bmi);

另一个输入语句与高度替换权重基本相同。



编辑:这里是整个代码

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

int main()
{
float weight,height;
float bmi = weight * 703 / pow(height,2.0);

printf(Height?);
scanf(%f,& height);

printf(Weight?);
scanf(%f,& weight);

printf(您的BMI计算在%.2f.\\\
,bmi);

printf(%f\\\
,height);
printf(%f \ n,weight);




解决方案

您使用权重高度未初始化在:

  float weight,height; 
float bmi = weight * 703 / pow(height,2.0);

这是未定义的行为,这就是为什么你得到 inf ,在我的情况下,我得到 0.00 ,但任何事情都可能发生在UB下。

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

int main(void)
{
float weight,height;
float bmi;

printf(Height?);
scanf(%f,& height);

printf(Weight?);
scanf(%f,& weight);

bmi = weight * 703 / pow(height,2.0);
printf(您的BMI计算在%.2f.\\\
,bmi);

printf(%f\\\
,height);
printf(%f \ n,weight);
返回0;
}


I think the error lies in this line:

float bmi = weight * 703 / pow(height, 2.0); // user inputs weight and height and the bmi should be calculated.

The thing is, I created a sort of mock program with the values already put in and everything was fine. But when I use scanf() to have the user put in those same values it doesn't work.

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

int main ()
{
    float weight = 105, height = 63;
    float bmi;
    bmi = weight * 703 / pow(height, 2.0);
    printf("%f\n", bmi);
}

Both weight and height are floats. When I try to print the bmi I get "inf."

** Edited to show input function and print statement.

 printf("Weight? ");
    scanf("%f", &weight);

printf("BMI is %.2f.\n", bmi);

The other input statement is essentially the same with the word height replacing weight.

edit: Here is the entire code

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

int main ()
{
    float weight, height;
    float bmi = weight * 703 / pow(height, 2.0);

    printf("Height? ");
    scanf("%f", &height);

    printf("Weight? ");
    scanf("%f", &weight);

    printf("Your BMI is calculated at %.2f.\n", bmi);

    printf("%f\n", height);
    printf("%f\n", weight);


}

解决方案

You are using weight and height uninitialized in:

   float weight, height;
   float bmi = weight * 703 / pow(height, 2.0);

This is undefined behavior and thats why you get inf, in my case I get 0.00, but anything can happen under UB.

Change to

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

int main(void)
{
    float weight, height;
    float bmi;

    printf("Height? ");
    scanf("%f", &height);

    printf("Weight? ");
    scanf("%f", &weight);

    bmi = weight * 703 / pow(height, 2.0);
    printf("Your BMI is calculated at %.2f.\n", bmi);

    printf("%f\n", height);
    printf("%f\n", weight);
    return 0;
}

这篇关于程序返回inf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆