浮点变量比较失败 [英] Float variable comparison fails

查看:79
本文介绍了浮点变量比较失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的程序似乎将运行一次,但是当我在Turbo C中运行时,输出什么也没有. 有人可以解释吗?

The below program seems to look like it will run for one time but when i run in Turbo C , the output is nothing. Can any one explain this ?

#include<stdio.h>
int main() 
{
    float x=1.1;
    while(x==1.1)
    {
        printf("%f \n",x);
        x=x-0.1;
    }
    return 0;
}

推荐答案

默认情况下,浮点数存储为'double'类型.因此,完成了float和double值的比较.

By default, floating point numbers are stored as type 'double'. So, a comparison on float and double value is done.

我认为,

if(x==1.1f)

应该可以解决问题.

FLT_EPSILON是两个浮点数之间的最小差异,以使它们相同.

And also FLT_EPSILON is the smallest difference between two floating point numbers for them to be same.

if( abs(x-1.1f) <= FLT_EPSILON)

应该工作

这篇关于浮点变量比较失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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