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

  • Home
  • SEARCH
  • 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 9236451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:17:45+00:00 2026-06-18T07:17:45+00:00

The following code realizes the same computation by either using an Eigen vector as

  • 0

The following code realizes the same computation by either using an Eigen vector as a container only or a simple C-array. It produces a closed but not bit-to-bit equivalent result.

The final mathematical operation is x * alpha + y * beta.

#include <Eigen/Eigen>

int main()
{
  Eigen::VectorXd x(2);
  double* y = new double[2];
  long long int a = 4603016991731078785;
  double ga = *(double*)(&a);
  long long int b = -4617595986472363966;
  double gb = *(double*)(&b);
  long long int x0 = 451;
  long long int x1 = -9223372036854775100;
  x[0] = *(double*)(&x0);
  y[0] = *(double*)(&x0);
  x[1] = *(double*)(&x1);
  y[1] = *(double*)(&x1);
  double r = ga*x[0] + gb*x[1];
  double s = ga*y[0] + gb*y[1];
}

Why is it so?

Results differ when using MSVC and gcc (64-bits OS).

  • 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-06-18T07:17:46+00:00Added an answer on June 18, 2026 at 7:17 am

    This is probably because one computation is done completely within the FPU (floating-point unit) with 80 bits of precision, while the other computation uses partially 64 bits of precision (the size of a double). This can also be demonstrated without using Eigen. Look at the following program:

    int main()
    {
      // Load ga, gb, y[0], y[1] as in original program
      double* y = new double[2];
      long long int a = 4603016991731078785;
      double ga = *(double*)(&a);
      long long int b = -4617595986472363966;
      double gb = *(double*)(&b);
      long long int x0 = 451;
      long long int x1 = -9223372036854775100;
      y[0] = *(double*)(&x0);
      y[1] = *(double*)(&x1);
    
      // Compute s as in original program
      double s = ga*y[0] + gb*y[1];
    
      // Same computation, but in steps
      double r1 = ga*y[0];
      double r2 = gb*y[1];
      double r = r1+r2;
    }
    

    If you compile this without optimization, you will see that r and s have different values (at least, I saw that on my machine). Looking at the assembly code, in the first computation, the values of ga, y[0], gb and y[1] are loaded in the FPU, then the calculation ga * y[0] + gb * y[1] is done, and then the result is stored in memory. The FPU does all computations with 80 bits, but when the result is stored in memory the number is rounded so that it fits within the 64 bits of a double variable.

    The second computation proceeds differently. First, ga and y[0] are loaded in the FPU, multiplied, and then rounded to a 64-bits number and stored in memory. Then, gb and y[1] are loaded in the FPU, multiplied, and then rounded to a 64-bits number and stored in memory. Finally, r1 and r2 are loaded in the FPU, added, rounded to a 64-bits number and stored in memory. This time, the computer rounds intermediate results, and this leads to the difference.

    For this computation, rounding has a fairly large effect because you are working with denormal numbers.

    Now, here comes the bit where I am not so certain (and if this was your question, I apologize): what does this have to do with the original program, where x is an Eigen container? Here the computation goes as follows: a function from Eigen is called to get x[0], then ga and the result from that function is loaded into the FPU, multiplied, and stored in a temporary memory location (64 bits, so this is rounded). Then gb and x[1] are loaded into the FPU, multiplied, added to the intermediate result stored in a temporary memory location, and finally stored in x. So in the computation of r in the original program, the result of ga*x[0] is rounded to 64 bits. Perhaps the reason for this is that the floating point stack is not preserved across function calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code produces a nested array as a result for keys containing three items:
Following code is a pretty simple and complete JQuery Dialog. Everything works. Problem is
I've the following code and I want form_invalid method to return the same page
I have been using the following code (with jQuery v1.4.2) to set the 'selected'
I am using wxWidgets 2.9.2, when i use following code //ToolBar wxToolBar *mainTool =
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class
Following code gets executed whenever I want to persist any entity. Things seems to
Following code is generated by a for loop. <form action=saveresponse.php method=POST name=mainForm> <input class=cbox_yes

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.