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

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

问题描述

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

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.

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

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 虽然两个浮点变量的值不同.

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

推荐答案

输出:相同,尽管两个浮点变量的值不同.

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 位并且可以表示大约 232 个不同的值,例如 1.0、1024.0、0.5、0.125.这些值都是以下形式: +/- some_integer*2some_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 在该 float 集中是 not.@Rudy Velthuis.

1.012345679 and 1.012345678 are not in that float set. @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.012345678not 在那个 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

<小时>

可以认为是 2 步舍入.代码 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 具有相同的值.

a and b have the same value.

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

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