Can someone point towards (or show) some good general floating point comparison functions in C# for comparing floating point values? I want to implement functions for IsEqual, IsGreater an IsLess. I also only really care about doubles not floats.
Can someone point towards (or show) some good general floating point comparison functions in
Share
Writing a useful general-purpose floating point
IsEqualis very, very hard, if not outright impossible. Your current code will fail badly fora==0. How the method should behave for such cases is really a matter of definition, and arguably the code would best be tailored for the specific domain use case.For this kind of thing, you really, really need a good test suite. That’s how I did it for The Floating-Point Guide, this is what I came up with in the end (Java code, should be easy enough to translate):
You can also find the test suite on the site.
Appendix: Same code in c# for doubles (as asked in questions)