Re: How to test a 'float' or 'double' zero numerically?
On Sep 13, 6:23 pm, Paavo Helde <nob...@ebi.ee> wrote:
Peng Yu <PengYu...@gmail.com> kirjutas:
Suppose T is 'float' or 'double'.
T x;
x < 10 * std::numeric_limits<T>::epsilon();
I can use the above comparison to test if 'x' is numerically zero. But
Really? What if x is -10000? What if it is equal to
std::numeric_limits <T>::epsilon()?
I'm wondering what should be a good multiplicative constant before
epsilon?
To answer your question literally, then comparing to zero is
easy, just use if(x==0). However, this usually does not give
you much if x is a result of some computation, with this
expression you can pretty much just check whether x has been
assigned literal zero beforehand.
It depends on the computation. There are a lot of contexts
where you get 0.0 exactly, and that's what you want to test for.
There are less contexts where this is true for other values (0.0
is a bit special), but they also exist.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34