-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloatingPointComparison.cpp
44 lines (40 loc) · 1.56 KB
/
FloatingPointComparison.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//Purpose: A function to compare floating point numbers. Numbers in computers
//are internally represented in binary, and there are some floating point
//numbers (such as 0.1) that do not have an exact representation in binary.
//If there is no exact representation in binary, then the value is rounded to
//the nearest representation in binary, which results in some floating point
//numbers being slightly imprecise. This imprecision results in some comparisons
//(such as testing for equality) to not work as expected. Here we implement a
//function that compares two floating point numbers by using an epsilon value
//(error margin) that takes into account floating point imprecision.
#include <iostream>
#include <algorithm>
#include <limits>
#include <math.h>
using namespace std;
#define EPSILON 0.00001
bool nearly_equal(float a, float b){
float abs_a = fabs(a);
float abs_b = fabs(b);
float diff = fabs(a - b);
if(a == b){
return true;
}
//a or b is zero or both are extremely close to zero
//other implementations use float min normal instead of float min
else if(a == 0 || b == 0 || diff < std::numeric_limits<float>::min()){
return diff < (EPSILON * std::numeric_limits<float>::min());
}
//use relative error
else{
return (diff / std::min((abs_a + abs_b), std::numeric_limits<float>::max())) < EPSILON;
}
}
int main(){
cout << nearly_equal(0, 0) << endl;
cout << nearly_equal(0, 0.00001) << endl;
float a = 0.2 + 0.1;
float b = 0.15 + 0.15;
cout << nearly_equal(a, b) << endl;
return 0;
}