## C++浮点数float比较equal，greater和less 原

极客行

``````//Specify the precision to compare two values
bool equal(double v1, double v2, double precision);
bool greater(double v1, double v2, double precision);
bool less(double v1, double v2, double precision);

bool equal(double v1, double v2, double precision)
{
double unprecision = (precision < 0) ? (-1)*precision : precision;
double dt = ((v1 < v2) ? v2 : v1) - ((v1 < v2) ? v1 : v2);
return (dt > unprecision) ? false : true;
}

bool greater(double v1, double v2, double precision)
{
if (equal(v1, v2, precision)) {
return false;
}
else {
return (v1 > v2) ? true : false;
}
}

bool less(double v1, double v2, double precision)
{
if (equal(v1, v2, precision)) {
return false;
}
else {
return (v1 < v2) ? true : false;
}
}

//测试代码
#include <iostream>
#include <functional>
#include "../xPod_Common/common.h"

using namespace std;

void equal_print(double base, double precision, const double* dt, int size,
const char* true_flag, const char* false_flag, function<bool(double,double,double)> func)
{
for (auto i=0; i<size; i++) {
if (func(base, base+*(dt+i), precision)) {
printf("%f %s %f, precision = %f\n", base, true_flag, base+dt[i], precision);
}
else {
printf("%f %s %f, precision = %f\n", base, false_flag, base+dt[i], precision);
}
}
}

void equal_unit_test()
{
//超大数据200000正负10以内
double precision = 10;
double baseVal = 200000;
double dt1[] = {0.0,0.1,-0.1,0.5,-0.5,1,-1,2,-2,5,-5,10,-10,10.1,-10.1,12,-12};
cout << "Test : 200000+10 >= x >= 200000-10" << endl;
equal_print(baseVal, precision, &dt1[0], sizeof(dt1)/sizeof(double), "==", "!=", &xpod_common::equal);
cout << endl << endl;

//高精度数据1正负0.001以内
precision = 0.001;
baseVal = 1.0;
double dt2[] = {0.0,0.0001,-0.0001,0.0005,-0.0005,0.001,-0.001,0.00101,-0.00101,0.0011,-0.0011,0.002,-0.002};
cout << "Test : 1+0.001 >= x >= 1-0.001" << endl;
equal_print(baseVal, precision, &dt2[0], sizeof(dt2)/sizeof(double), "==", "!=", &xpod_common::equal);
cout << endl << endl;

//大于比较
precision = 0.1;
baseVal = 100.0;
double dt3[] = {-1,-0.2,-0.11,-0.10,-0.05,-0.01,0.0,0.01,0.05,0.1,0.5,1,2};
cout << "Test : Greator" << endl;
equal_print(baseVal, precision, &dt3[0], sizeof(dt3)/sizeof(double), ">", "<>", &xpod_common::greater);
cout << endl << endl;

//小于比较
cout << "Test : Less" << endl;
equal_print(baseVal, precision, &dt3[0], sizeof(dt3)/sizeof(double), "<", "<>", &xpod_common::less);
}

int main(int argc, char *argv[])
{
//test equal
equal_unit_test();

return 0;
}``````

### 极客行

2018/08/18
61
0
LeetCode 406. Queue Reconstruction by Height (依据高度进行队列重构)

dby_freedom
2018/12/18
0
0
Priority_Queue(优先队列)

01/22
0
0
CalcStar —— 一个 C++ 的数学表达式计算器

oschina
2014/03/25
1K
2

unaryfunction有一个兄弟是binaryfunction，俩格的区别是一个为了使用一个参数输入的函数对象，另一个适合两个输入参数的函数对象。对于STL不熟悉的人，很难理解为什么好端端的函数对象， 为...

woodo
2014/04/10
87
0

CREATE_17

5
0
CSS盒子模型

CSS盒子模型 组成： content --> padding --> border --> margin 像现实生活中的快递： 物品 --> 填充物 --> 包装盒 --> 盒子与盒子之间的间距 content ：width、height组成的 内容区域 padd......

studywin

7
0

locbytes

8
0
jquery 添加和删除节点

8
0

ZeroBit

10
0