Check for zero or a denormalized number in c++
I currently have some code where I have to normalize a vector of doubles (divide each element by the sum). When debugging, I see sometimes that the elements in the vector are all 0.0. If I then take the sum of the elements, I get either 0.0 or 4.322644347104e-314#DEN (which I recently found out was a denormalized number). I would like to prevent normalizing the vector for the cases when the sum is either 0.0 or a denormalized number. The only way I could think of handling these two cases is to check if the sum is less than 'epsilon', where epsilon is some small number (but I'm not sure how small to make epsilon).
I have 2 questions:
- What is the best way to take 开发者_如何学Pythonthese cases into account?
- Is the value of the denormalized number machine dependent?
C99 provides fpclassify
to detect denormalized number. It's also provided with C++0x and Boost.Math.
// C++0x
#include <cmath>
using std::fpclassify;
// Boost
//#include <boost/math/special_functions/fpclassify.hpp>
//using boost::math::fpclassify;
if(fpclassify(sum) == FP_SUBNORMAL) {
// ...
}
#include <limits>
#include <cmath>
double epsilon = std::numeric_limits<double>::min();
if (std::abs(sum) < epsilon) {
// Don't divide by sum.
}
else {
// Scale vector components by sum.
}
Addendum
Since you are trying to normalize a vector, I would venture that your sum is the sum of the squares of the vector elements, conceptually
double sum = 0;
for (unsigned int ii = 0; ii < vector_size; ++ii) {
sum += vector[ii]*vector[ii];
}
sum = std::sqrt(sum);
There are three problems with the above.
- If any of those vector components is larger in magnitude than
sqrt(max_double)
you will get infinities. - If any of those vector components is smaller in magnitude than
sqrt(min_double)
you will get underflow. - Even if the numbers are well-behaved (between 2*10-154 and 10154 in magnitude), the above is problematic if the magnitudes vary widely (a factor of 106 will do). You need a more sophisticated hypotenuse function if this is the case.
You can use a flag while you take the sum to ensure that not every element is equal to 0.
精彩评论