floating point comparison basics
ed
sillymongrel at gmail.com
Tue Dec 3 14:03:48 PST 2013
Hi All,
I'm learning programming and chose D because it is the best :D
But, I've hit floating point numbers and I'm stuck on some of the
basics.
What is the proper way to do floating point comparisons, in
particular I need to check if a value is zero?
For example, given "real x = someCalculatingFunction();" how do I
check if X is zero in a robust way.
if(x == 0.0) {} // <-- Will this work as expected?
I see some code from others in my class doing this (mostly C++):
# if(fabs(x) < 1e-10) {} // <-- I've heard this is bad, is it?
# if(fabs(x) < std::numeric_limits<double>::min()) {}
# if(fabs(x) < std::numeric_limits<double>::epsilon()) {}
# if(!(fabs(x) > 0.0)) {}
# double eps = 1/(1/x);
# if(!(fabs(eps) > 0.0)) {}
So you can see we're all confused!
Thanks,
Ed
PS: I have read this great article and the links it provides:
http://dlang.org/d-floating-point.html
Most of it makes sense but I'm struggling to tie it all together
when it comes time to apply it.
More information about the Digitalmars-d-learn
mailing list