6

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:

  1. What is the best way to take these cases into account?
  2. Is the value of the denormalized number machine dependent?
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
A-A
  • 663
  • 1
  • 13
  • 17
  • I thought normalizing means ==> `"divide by average of sum"` – iammilind Jul 18 '11 at 13:24
  • 1
    Tons of things you could call "normalize". For instance you might want to divide by the pth root of the sum of the pth powers of the elements... – Kerrek SB Jul 18 '11 at 13:28
  • 1
    @iammilind: Normalizing typically means "divide by the square root of the sum of the squares". Or by some other Lp norm. – David Hammen Jul 18 '11 at 13:40
  • @Kerrek: an Lp-norm would actually be the pth root of the sum of the pth powers of _the absolute values_ of the elements. Its only the same for even p. In particular, if it's the L₁ you're going for, you need the sum of the absolute values. But the more common and unsually more useful norm (because it's invariant under orthogonal transformations) is indeed the L₂, where you need the square root of the sum of the squares. – leftaroundabout Jul 19 '11 at 10:10
  • @leftaroundabout: Yeah, you're right of course -- I think I just got lazy typing :-) I guess you _always_ need the absolute value if you want to allow complex numbers... – Kerrek SB Jul 19 '11 at 10:12

3 Answers3

11

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) {
    // ...
}
Luc Danton
  • 34,649
  • 6
  • 70
  • 114
10
#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.

  1. If any of those vector components is larger in magnitude than sqrt(max_double) you will get infinities.
  2. If any of those vector components is smaller in magnitude than sqrt(min_double) you will get underflow.
  3. 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.
etarion
  • 16,935
  • 4
  • 43
  • 66
David Hammen
  • 32,454
  • 9
  • 60
  • 108
-1

You can use a flag while you take the sum to ensure that not every element is equal to 0.

ascanio
  • 1,506
  • 1
  • 9
  • 18