News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

Possible GCC Bug?

Started by Szabadember, October 14, 2007, 04:26:26 PM

Previous topic - Next topic

Szabadember

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:


#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 a = (float)pow(10,i); instead of a = pow(10,i);, output will be as expected. (1,10,100,1000,10000,...)

If i use 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)

Biplab

Quote from: Szabadember on October 14, 2007, 04:26:26 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.

Szabadember


#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) .

MortenMacFly

Quote from: Szabadember on October 14, 2007, 05:00:54 PM
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: [url="https://www.codeblocks.org/docs/main_codeblocks_en.html"]https://www.codeblocks.org/docs/main_codeblocks_en.html[/url]
C::B FAQ: [url="https://wiki.codeblocks.org/index.php?title=FAQ"]https://wiki.codeblocks.org/index.php?title=FAQ[/url]

thomas

Quote from: Szabadember on October 14, 2007, 05:00:54 PMWhat about that code, why isn't that working?
It's working fine, btw. It works exactly like it should be.

Quoteand/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."