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 7034509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:11:41+00:00 2026-05-28T01:11:41+00:00

I am creating a floating point matrix template class. The class declaration is shown

  • 0

I am creating a floating point matrix template class. The class declaration is shown below only with relevant functions and members.

// columns, rows
template <unsigned int c, unsigned int r>
class Matrix {
 public:
  Matrix(float value);

  float& At(unsigned int x, unsigned int y);
  float const& At(unsigned int x, unsigned int y) const;
  template <unsigned int p> Matrix<p, r> MultipliedBy(Matrix<p, c> const& other);

 private:
  // column-major ordering
  float data_[c][r];
}

The implementations for each of the above functions follow.

template <unsigned int c, unsigned int r>
Matrix<c, r>::Matrix(float value) {
  std::fill(&data_[0][0], &data_[c][r], value);
}

template <unsigned int c, unsigned int r>
float& Matrix<c, r>::At(unsigned int x, unsigned int y) {
  if (x >= c || y >= r) {
    return data_[0][0];
  }

  return data_[x][y];
}

template <unsigned int c, unsigned int r>
float const& Matrix<c, r>::At(unsigned int x, unsigned int y) const {
  if (x >= c || y >= r) {
    return data_[0][0];
  }

  return data_[x][y];
}

template <unsigned int c, unsigned int r>
template <unsigned int p>
Matrix<p, r> Matrix<c, r>::MultipliedBy(Matrix<p, c> const& other) {
  Matrix<p, r> result(0.0f);

  for (unsigned int x = 0; x < c; x++) {
    for (unsigned int y = 0; y < r; y++) {
      for (unsigned int z = 0; z < p; z++) {
        result.At(z, y) += At(x, y) * other.At(z, x);
      }
    }
  }

  return result;
}

Now, a few lines of test code.

Matrix<4, 4> m1;

// m1 set to
//
//  1   2   3   4
//  5   6   7   8
//  9   10  11  12
//  13  14  15  16

Matrix<1, 4> m2;

// m2 set to
//
//  6
//  3
//  8
//  9

Matrix<1, 4> m3 = m1.MultipliedBy(m2);

Here’s where things get weird. When compiled (using g++) with no optimization (-O0):

// m3 contains
//  0
//  0
//  0
//  0

With any optimization (-O1, -O2, or -O3):

// m3 contains
//  210
//  236
//  262
//  288

Note that even with the optimization, the answer is incorrect (verified with an external calculator). So I narrowed it down to this call in MultipliedBy:

Matrix<p, r> result(0.0f);

If I instantiate result in any way other becomes invalidated (all data_ values set to 0.0f). Before the allocation/initialization of result, other is still valid (6, 3, 8, 9).

It is worth noting that if I multiply two matrices of the same (square) dimension, I get a completely valid and correct output, regardless of the optimization level.

Anyone have a clue what in the world g++ is pulling here? I’m running g++ (GCC) 4.6.1 on mingw… might this have something to do with the problem?

  • 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-28T01:11:41+00:00Added an answer on May 28, 2026 at 1:11 am

    &data_[c][r] is perhaps wrong: it’s data_ + (c*r + r) * FS, whereas you perhaps need &data_[c-1][r-1] + FS, which is data_ + ((c-1)*r + (r-1) + 1) * FS, which is data_ + c*r * FS.

    (Here FS == sizeof(float).)

    Your last item is data_[c-1][r-1], so one past last would be data_[c-1][r], not data_[c][r].

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

Sidebar

Related Questions

I'm creating a web application that does some very heavy floating point arithmetic calculations,
What I am aiming for is along the lines of the example at http://net.tutsplus.com/tutorials/html-css-techniques/creating-a-floating-html-menu-using-jquery-and-css/
I'm creating a financial app and it seems my floats in sqlite are floating
I'm in the process of creating a class that stores metadata about a particular
I work with a lot of floating-point images, and would find it extremely useful
wondered if any one knew of a way of creating a floating menu bar
Need some advice on creating a floating menu that behaves very similar to what
Given an arbitrary floating point number, say -0.13 , suppose we have an algorithm
I am creating a website of floating width. The users use screens from full
I'm fiddling w/ dojo (1.4) and am having a problem programmatically creating floating panes

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.