Last active
November 1, 2016 10:22
-
-
Save jacknlliu/ef96b46e3cde7ee26a6e8e8a000adf56 to your computer and use it in GitHub Desktop.
Determine whether two float number is equal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "float-equal.hpp" | |
| #include <iostream> | |
| #include <math.h> | |
| using namespace std; | |
| int main(int argc, char const *argv[]) | |
| { | |
| bool ret; | |
| float a = 0.01; | |
| float b = 0.012; | |
| ret = is_float_equal(a,b, 2); | |
| cout<<ret<<endl; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef FLOAT_EQUAL_HPP | |
| #define FLOAT_EQUAL_HPP | |
| #include <stdlib.h> | |
| #include <stdint.h> | |
| #include <math.h> | |
| template <typename T> | |
| bool is_float_equal(T value1, T value2, int acc) | |
| { | |
| // according to |value1 - value2| < epsilon | |
| if (((value1 -value2) < pow(10, -acc)) && ((value1 -value2) > -pow(10, -acc)) ) { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment