我正面临一个与while循环有关的问题我这个特殊的问题。 [英] I am facing a problem related to while loop working I this particular question.

查看:86
本文介绍了我正面临一个与while循环有关的问题我这个特殊的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我的尝试:



我的意见我的输出应该是1.1,但它们是NO OUTPUT为什么????????

当我在DEV C ++上执行此代码时......



What I have tried:

I my opinion the output should be 1.1 but their is NO OUTPUT why????????
when i executed this code on DEV C++ ......

推荐答案

这是因为浮点值由于其内部表示而不准确。请参阅以下链接:

浮点指南 - 每个程序员应该知道的关于浮点运算的内容 [ ^ ]

每个计算机科学家应该知道的浮点运算 [ ^ ]



在您的情况下,这是隐藏的,因为您将单精度浮点变量 x 与双精度常量 1.1 。编译器会将您的float变量 x 转换为 double ,然后进行比较。因为 1.1 是其中一个无法准确表示的值,所以比较失败。



在你的在与单个精度常量进行比较时可能会有效(注意 f 后缀):

That is because floating point values are not exact due to their internal representation. See these links:
The Floating-Point Guide - What Every Programmer Should Know About Floating-Point Arithmetic[^]
What Every Computer Scientist Should Know About Floating-Point Arithmetic[^]

In your case this is hidden because you are comparing the single precision floating point variable x with the double precision constant 1.1. The compiler will convert your float variable x to double and then compare it. Because 1.1 is one of those values that can't be represented exactly, the comparison fails.

In your case it might work when comparing with a single precision constant (note the f postfix):
while(x==1.1f)



但你不应该依赖它。相反,您应该使用比较浮点数时所述的epsilon比较 ,2012年版|随机ASCII [ ^ ]。


这很复杂,但在这种情况下它围绕实际的数字。

键入1.1时作为一个数字,那是什么类型的数字?我们知道它不是一个整数 - 因为它有一个小数分量 - 所以它是某种形式的浮点数,但浮点数的大小是什么?这有关系吗?

在C中有两种存储浮点数的方法: float double (嗯......技术上有三个: long double 也可用),差别在于它们占用的空间不大。 float 值通常占用4个字节, double 取8因为它们可以包含更精确的值。



但就编译器而言,1.1是什么?答:这是一个 double ,因为那是最准确的,当你这样做时它会隐式转换为 float 作业。

这有关系吗?是。因为浮点数不是存储为一个点和一个点,所以它们以更复杂的方式存储:单精度浮点格式 - 维基百科 [ ^ ]进入细节,但因为它是所有生物学而不是基数十,1.1不存储为准确数字 - 它是不同的。并且 double 版本与 float 版本不同,因为它可以存储更多的二进制数字。



简单的解决方案是坚持一种格式:

That's complicated, but in this case it revolves around what numbers actually are.
When you type "1.1" as a number, what type of number is that? We know it's not an integer - because it has a fractional component - so it's a floating point number of some form, but what "size" of floating point number is that? Does it matter?
In C there are two ways to store floating point numbers: float and double (well ... technically there are three: long double is also available) and the difference is no much space they take up. float values generally occupy 4 bytes, and double take 8 becuase they can contain more precise values.

But what is 1.1 as far as the compiler is concerned? Answer: it's a double because that's "the most accurate" and it gets an implicit cast down to float when you do the assignment.
Does it matter? Yes. Because floating point numbers aren't stored as "one one and a point one as well", they are stored in a much more complicated way: Single-precision floating-point format - Wikipedia[^] goes into the details, but becuase it's all bionary instead of base ten, 1.1 doesn't store as an "accurate" number - it's different. And the double version isn't the same as the float version because it can store more binary digits.

The simple solution is to stick to one format:
int main()
{
    float x = 1.1;
    while(x == 1.1F)
    {
    printf("%f\n",x);
    x = x - 0.1F;
    } 
    return 0;
}



Or

int main()
{
    double x = 1.1;
    while(x == 1.1)
    {
    printf("%f\n",x);
    x = x - 0.1;
    } 
    return 0;
}

它会做你期望的。


我的预编写者是正确的,但更好的解决方案是编写这样的

a)避免此类浮点问题的代码或

b)使用其他比较或使用常量,例如:

My pre-writers are correct but and the better solution is to write such
a) code that avoids such floating point problems or
b) use some other comparison or work with constants like:
//global constant
const double MY_CONSTANT = 1.1;
int main()
{
    double x = 1.1;
    while(x == MY_CONSTANT)
    {
    printf("%f\n",x);
    x = x - 0.1;
    } 
    return 0;
}

c)为支票使用一些定义的差异

c) use some defined difference for the check

int main()
{
    double x = 1.1;
    // some diff is accepted
    while( abs(x - MY_CONSTANT) < 0.0000001 )
    {
    printf("%f\n",x);
    x = x - 0.1;
    } 
    return 0;
}

但总是使用数据类型为double的更高精度


这篇关于我正面临一个与while循环有关的问题我这个特殊的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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