“如果没有”条件不按预期工作 [英] "If else" conditionals not working as expected

查看:48
本文介绍了“如果没有”条件不按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>

int main(){

float x = 9.81 ; 
if( x == 9.81)
{printf("the value of x is 9.81\n");}
else {printf("No its not 9.81 \n");}
}





在此,我们期待如果要执行的语句,而是else语句被执行。

为什么会这样?



In this we expect "if" statement to execute but instead "else" statement get executed.
Why is it so??

推荐答案

这是浮动的常见问题不是他们看起来的样子。 ;-)



原因是内存中有内部表示。输出它。



解决方案是计算差值,接受一个小的。请记住它可能已签名。



it is common issue that float arent exactly what they seem. ;-)

The reason is there internal representation in the memory. Make an output of it.

The solution is to calculate the difference an accept a small one. Remember that it maybe signed.

float x = 9.81f; 
float d = x - 9.81f;

printf("the diff is %f\n", d);

if( fabs(d) < 0.005/*accepted tolerance*/ )
{printf("the value of x is 9.81\n");}
else {printf("No its not 9.81 \n");}
}


KarstenK 已经为您提供了解决方案。我建议你阅读:每个计算机科学家应该知道的关于浮点运算的内容 [ ^ ]。
KarstenK already gave you the solution. I suggest you to read: "What Every Computer Scientist Should Know About Floating-Point Arithmetic"[^].


除了其他答案,我只需要5美分:



使用运营商不是一个好主意==使用浮点操作数。这是对的,我不知道它可能有用的情况。即使浮点值表示零小数部分的数字,也不应该使用它;相反,你可以将值和类型转换为整数类型之一,甚至很少需要。浮点值表示近似值。如果不可能代表全部实数就像计算机这样的有限状态机,即使是单个实数,通常也包含无限量的信息。在有意义的情况下,典型的比较可能如下所示:

Just my 5 cents in addition to other answers:

It's not a good idea to use the operator == with floating-point operands. That's right, I don't know the situations where it can be useful at all. Even with floating-point values representing numbers with zero fractional part, you should not use it; instead you can round a value and typecast to one of the integer types, and even that is rarely needed. Floating-point values represent approximate values. If is impossible to represent "all" real number is such a finite-state machine as computer, even a single real number, generally, contains infinite volume of information. A typical comparison, in case when it makes sense, may look like:
#include <cmath>
double margin = //... some value which is "small enough"
double myValue = //...
double testValue = //...
// let's say, you mean to compare:
// if (myValue == testValue) ... // bad idea
// instead, do this:
if (std::abs(myValue - testValue) < margin) // ...









我提交这篇文章之后,我发现这个代码示例与解决方案1中的代码示例基本相同。 ,我决定不删除我的,也许它可以提供一些额外的澄清。



有时你需要一个不同的标准,相对接近。例如





After I submitted this post, I found that this code sample is essentially the same as in Solution 1. Crediting that, I decided not to remove mine, maybe it may provide some extra clarification.

Sometimes you need a different criterion, "relative closeness". For example

double margin = 0.001; // say, 0.1%
if (std::abs(myValue - testValue) * 2 <
    margin * (myValue + testValue)) // ...



假设比较值是有用的是大值,它们之间的小差异,所以标准是检查相对差异是否足够小。



[END EDIT]



参见: std :: abs( float),std :: fabs - cppreference.com [ ^ ]。



浮点计算的原理已在经典 Donald Knuth的书 计算机编程艺术



-SA


这篇关于“如果没有”条件不按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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