如何在C ++中准确地比较float类型的两个变量,一个是在声明时初始化而另一个是通过程序计算的? [英] What can I do to compare two variables of type float accurately in C++, one being initialized at the time of declaration and the other being computed through the program?

查看:138
本文介绍了如何在C ++中准确地比较float类型的两个变量,一个是在声明时初始化而另一个是通过程序计算的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望你好。

当我比较两个值为0.5或0.25的变量时,程序显示所需的输出但是当我比较两个值为0.333333或0.666667的变量时,程序没有显示所需的输出。你能告诉我哪里出错了吗?请帮我写一些正确的代码。我是一名新手,向您学习将是一次很棒的经历。感谢您的关注。这是带有一些输出的代码:



Hope your well.
When I am comparing two variables having value either 0.5 or 0.25, the program is showing the desired output but when I am comparing two variables having value either 0.333333 or 0.666667, the program is not showing the desired output. Will you please tell me where I am going wrong? Please help me in writing the correct piece of code. I am a novice and it will be a great experience to learn from you. Thank you for your concern.Here is the code with some of the outputs:

#include 





输出:



0.333333 0.333333

UNEQUAL



0.666667 0.666667

UNEQUAL < br $>


0.25 0.25

EQUAL



0.5 0.5

EQUAL





问候,

Prateek Senapati



我尝试了什么:



我试图输入不同的小数值,但没有得到预期的结果case。



OUTPUTS:

0.333333 0.333333
UNEQUAL

0.666667 0.666667
UNEQUAL

0.25 0.25
EQUAL

0.5 0.5
EQUAL


Regards,
Prateek Senapati

What I have tried:

I have tried to enter different decimal values but did not get expected results in all the cases.

推荐答案

只考虑浮点表示不能精确表示像1/3这样的数字。浮点数格式使用基数2中的分数表示,即它可以正确地表示诸如1 / 2,1 / 4,1 / 8,1 / 16等的数字,但不能表示1/3或1/5。对于那些它使用最接近的近似值。现在,当您以浮点格式进行计算时,每次计算都会出现越来越大的逼近误差。因此,程序中0.3333 ...作为常量的表示可能与计算结果不完全相同。



解决方案:不要比较相等,但总是允许一个小的近似误差。
Just consider that the floating point representation cannot precisely express numbers like 1/3. The floating point number format uses a fractional representation in base 2, i.e. it can represent numbers like 1/2, 1/4, 1/8, 1/16 etc. correctly, but not 1/3 or 1/5. For those it uses the nearest approximation. Now, when you do computations in floating point format the approximation error gets bigger and bigger with every single computation. Hence, the representation of 0.3333... as constant in your program might not exactly be equal to your computation result.

The solution: Don't compare for equality, but always allow for a small approximation error.


浮点数不能完全代表大多数实数。数字0.25和0.5可以精确表示,但不能表示为0.33和0.66。



这是由内部表示这些数字的来源(参见IEEE浮点 - 维基百科 [ ^ ]。



要检查浮点数是否几乎相等,必须执行特殊检查例如,参见浮点指南 - 比较 [ ^ ]。
Floating point numbers can not represent most real numbers exactly. The numbers 0.25 and 0.5 can be represented exactly but not 0.33 and 0.66.

This is sourced by how these numbers are represented internally (see IEEE Floating Point - Wikipedia[^].

To check if floating points numbers are nearly equal, special checks must be performed. See for example The Floating-Point Guide - Comparison[^].


我们不做你的HomeWork。 />
HomeWork不会在乞求其他人做你的工作时测试你的技能,它将帮助你的老师检查你对你所学课程的理解以及你应用它们时遇到的问题。
如果您失败,将帮助您的老师发现您的弱点并设置补救措施。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请展示您的代码并解释这个问题,我们可能会提供帮助。
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.


这篇关于如何在C ++中准确地比较float类型的两个变量,一个是在声明时初始化而另一个是通过程序计算的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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