Skip to content

Instantly share code, notes, and snippets.

@mebjas
Created October 5, 2022 09:26
Show Gist options
  • Select an option

  • Save mebjas/46a8b170907037b92320c9f395dc4402 to your computer and use it in GitHub Desktop.

Select an option

Save mebjas/46a8b170907037b92320c9f395dc4402 to your computer and use it in GitHub Desktop.
Code to test the problem above about 10 million times.
#include <iostream>
#include <math.h>
#include <float.h>
using namespace std;
inline bool isNearlyEqual(float val_a, float val_b) {
return fabs(val_a - val_b) < FLT_EPSILON;
}
int main() {
int ITERATIONS = 10000000;
int counter = 0;
for (int i = 0; i < ITERATIONS; ++i) {
float a = 0.151f + 0.151f;
float b = 0.101f + 0.201f;
bool d = isNearlyEqual(a, b);
if (d) {
counter++;
}
}
std::cout << "Counter = " << counter << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment