Created
October 5, 2022 09:26
-
-
Save mebjas/46a8b170907037b92320c9f395dc4402 to your computer and use it in GitHub Desktop.
Code to test the problem above about 10 million times.
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 <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