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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:04:45+00:00 2026-06-07T19:04:45+00:00

Code [GCC, compiled with -O2 flag] int main() { vector< vector<int> > matrixa(8); int

  • 0

Code [GCC, compiled with -O2 flag]

int main()
{
    vector< vector<int> > matrixa(8);
    int ff = 5;
    int s = pow(ff, matrixa.size());
    int ss = pow(double(ff), int(matrixa.size()));
    vector< vector<int> > comb(s);
    cout << ff << "^" << matrixa.size() << " = " << s << endl;
    cout << ss << endl;
    return 0;
}

Output

5^8 = 390624
390625

I’m wondering why s = 390624 when it should be 390625. If I compile the code without -O2 flag, then s = 390625. Also, the casting for ss seems to correct the problem.

What’s going on?

My OS is Windows 7 Ultimate x64. Not sure about the GCC version, it comes with Code::Blocks 10.05.

  • 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-07T19:04:46+00:00Added an answer on June 7, 2026 at 7:04 pm

    Because floating-point arithmetic is not perfect and when you do

    int s = pow(ff, matrixa.size());
    

    the result of pow is actually something more like 390624.99999 and when you truncate it to int it effectively is flattened down to 390624. If you expect an integer value there (a floating-point with .0 fractional part), you should probably round the result of pow.

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

Sidebar

Related Questions

I was trying to execute this code through gcc compiler: #include <stdio.h> int main()
I have the following code compiled by gcc: #include <iostream> using namespace std; class
I am working on performance optimizations of ARM C/C++ code, compiled with GCC. CPU
Examining the compiled code of the sinf() on our GCC implementation, I see that
I know that c++ code should be compiled and linked by g++, not gcc.
I was thinking about using ccache with gcc compiled code on the team wide
I compiled the following code with gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) under Ubuntu 10.04
I am trying to compile the following program: #include <iostream> int main(){ std::cout <<
gcc optimizes code when I pass it the -O2 flag, but I'm wondering how
I am using the GCC compiler but I want the to code the actually

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.