比较C ++中的两个float变量 [英] Compare two float variables in C++

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

问题描述

我已经阅读了多篇有关浮点变量比较的文章,但未能理解这些文章并从中获得所需的知识.所以,我在这里发布这个问题.

I have read multiple articles regarding floating point variables comparison, but failed to understand and get the required knowledge from those articles. So, here I am posting this question.

比较两个float变量的好方法是什么? 下面是代码片段:

What is the good way to compare two float variables? Below is the code snippet:

#define EPSILON_VALUE 0.0000000000000001

bool cmpf(float A, float B)
{
  return (fabs(A - B) < EPSILON_VALUE);
}

int main()
{
  float a = 1.012345679, b = 1.012345678;

  if(cmpf(a, b))cout<<"same"<<endl;
  else cout<<"different"<<endl;

  return 0;
}

输出:same,尽管两个float变量都具有不同的值.

The output: same although both the float variables hold different values.

推荐答案

输出:相同,尽管两个float变量都具有不同的值.

The output: same although both the float variables hold different values.

浮点变量具有不同的值."是没有根据的.

"float variables hold different values." is unfounded.

same是因为即使初始化常数不同,值a,b也相同.

same was printed because values a,b are the same even if the initialization constants differ.

典型的float 是32位,可以表示大约2位 32 不同的值,例如1.0、1024.0、0.5、0.125.这些值的形式均为:+/- some_integer * 2 some_integer

Typical float is 32-bits and can represent about 232 different values such as 1.0, 1024.0, 0.5, 0.125. These values are all of the form: +/- some_integer*2some_integer

1.0123456791.012345678不是 . @Rudy Velthuis .

1.012345 67165374755859375 // `float` member
1.012345 678
1.012345 679
1.012345 790863037109375   // `float` member


double类似,但精度更高-通常为64位.


Similar applies for double, yet with more precision - commonly 64 bits.

1.0123456791.012345678不是 ,因为double设置了这两个

1.012345679 and 1.012345678 are not in that double set either

1.012345 67799999997106397131574340164661407470703125    // `double` member
1.012345 678
1.012345 6780000001931085762407747097313404083251953125  // `double` member
...
1.012345 6789999998317597373898024670779705047607421875  // `double` member
1.012345 679
1.012345 67900000005380434231483377516269683837890625    // `double` member


可以将其视为四舍五入的步骤.代码1.012345679舍入到最接近的double 1.01234567900000005380434231483377516269683837890625.然后,赋值会将double舍入到最接近的float 1.01234567165374755859375.


It can be thought of as 2 steps of rounding. Code 1.012345679 is rounded to the nearest double 1.01234567900000005380434231483377516269683837890625. Then the assignment rounds the double to the nearest float 1.01234567165374755859375.

float a = 1.012345679;
// 'a' has the value of 1.01234567165374755859375

b类似.代码1.012345678被四舍五入为最接近的double 1.01234567799999997106397131574340164661407470703125.然后,赋值会将double舍入到最接近的float 1.01234567165374755859375.

Likewise for b. Code 1.012345678 is rounded to the nearest double 1.01234567799999997106397131574340164661407470703125. Then the assignment rounds the double to the nearest float 1.01234567165374755859375.

flaot b = 1.012345678;
// 'b' has the value of 1.01234567165374755859375

ab具有相同的值.

这篇关于比较C ++中的两个float变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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