Skip to content

Instantly share code, notes, and snippets.

@jacknlliu
Last active November 1, 2016 10:22
Show Gist options
  • Select an option

  • Save jacknlliu/ef96b46e3cde7ee26a6e8e8a000adf56 to your computer and use it in GitHub Desktop.

Select an option

Save jacknlliu/ef96b46e3cde7ee26a6e8e8a000adf56 to your computer and use it in GitHub Desktop.
Determine whether two float number is equal
#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;
}
#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