Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6155513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:26:56+00:00 2026-05-23T20:26:56+00:00

I currently have some code where I have to normalize a vector of doubles

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:26:57+00:00Added an answer on May 23, 2026 at 8:26 pm
    #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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have some code that will produce a crash dump when my application
I currently have some code that pulls down a list of users in a
I've been trying to implement unit testing and currently have some code that does
Currently I have some code that is doing an SFTP via expect/tcl. Its something
We currently have some c# code that runs and imports data from a number
I currently have some code that delibratly throws an exception if the user sends
I currently have some code that cycles through a text file looking for a
We currently have some php code that allows an end user to update records
I currently have some VBA code that runs through and modifies all my calendar
Currently I have some code to replace strings in a file that looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.