Author Topic: Possible GCC Bug?  (Read 4348 times)

Offline Szabadember

  • Multiple posting newcomer
  • *
  • Posts: 75
  • That's me!
Possible GCC Bug?
« on: October 14, 2007, 04:26:26 pm »
Hi!
I found something really weird! I think it's a bug in mingw gcc 3.4.4 (The compiler that came with the old codeblocks RC2)
Here's an example:

Code
#include <iostream>
#include <math.h>

using namespace std;

int main(){
    int i,a;
    for(i=0;i<10;i++){
        a = pow(10,i);
        printf("%d\n",a);
    }
    return 0;
}


Output will be:
1
10
99
1000
9999
100000
1000000
9999999
99999999
999999999

If i use
Code
a = (float)pow(10,i);
instead of
Code
a = pow(10,i);
, output will be as expected. (1,10,100,1000,10000,...)

If i use
Code
a = pow(10,2);
output will be also as expected. The problem occurs only if i use a variable as an argument.

Can you confirm this?
What is the most recent stable version of MingW?
My other problem is that i don't know how to compile gcc or gdb (of course i want to do this with codeblocks :D)

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Possible GCC Bug?
« Reply #1 on: October 14, 2007, 04:39:35 pm »
Can you confirm this?
What is the most recent stable version of MingW?
My other problem is that i don't know how to compile gcc or gdb (of course i want to do this with codeblocks :D)

If you pass int to pow which expects the arguments in float, you can get such values. Pass correct parameter types to get correct results.

The last stable version is 3.4.5. Though you can download latest 4.2.1 technology preview release.

You can't build GCC or GDB with C::B. I mean it's practically not feasible.
Be a part of the solution, not a part of the problem.

Offline Szabadember

  • Multiple posting newcomer
  • *
  • Posts: 75
  • That's me!
Re: Possible GCC Bug?
« Reply #2 on: October 14, 2007, 05:00:54 pm »
Code
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int a;
    float i;
    for(i=0;i<10;i++){
        a = (int)pow(10.0,i);
        printf("%d\n",a);
    }
    return 0;
}

What about that code, why isn't that working? I used floats for both arguments and converted the return type (which is float i think) to int, output still the same. And why is it working if i typecast the return type to (float) although a is (int) .

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9699
Re: Possible GCC Bug?
« Reply #3 on: October 15, 2007, 09:16:43 am »
What about that code, why isn't that working?
Please, this is *not* a general programming forum. Your question has nothing to do with C::B. Please ask such question in a C / C++ forum and/or read a book how floats are organised in memory and how cast's work.
With regards, Morten.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Possible GCC Bug?
« Reply #4 on: October 15, 2007, 09:53:17 am »
What about that code, why isn't that working?
It's working fine, btw. It works exactly like it should be.

Quote
and/or read a book how floats are organised in memory and how cast's work
What Martin said.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."