C and Cpp GCC Compiler Issues
First I actually thought there was something wrong with my programming skills. But after I compiled the same code using Visual Studio, it worked. So here's the code that doesn't seem to be working right with gcc compilers, but they do compile. Note I'm using the Cygwin 3.4.4 version of gcc and 4.5.1 version of gcc from MinGW.
Code:
#include <stdio.h>
#include <math.h>
int main()
{
long double r = 0;
long double p = 0;
printf ("Enter a number: ");
scanf ("%lf", &r);
printf ("Enter its power: ");
scanf ("%lf", &p);
printf ("%lf to the power of %lf = %lf\n", r, p, pow(r, p));
printf ("7 ^ 3 = %lf\n", pow (7.0,3.0));
printf ("4.73 ^ 12 = %lf\n", pow (4.73,12.0));
printf ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54));
return 0;
}
This is the first one. It compiles with GCC but the output doesn't seem to be what I'm expecting. (MinGW gcc (GCC) 4.5.1)
Code:
Enter a number: 5
Enter its power: 3
5.000000 to the power of 0.000000 = 0.000000
7 ^ 3 = 343.000000
4.73 ^ 12 = 125410439.217423
32.01 ^ 1.54 = 208.036691
The part "5.000000 to the power of 0.000000 = 0.000000" doesn't seem to be right. But on the other hand, when I compiled it using Visual Studio, it worked very well.
Code:
Enter a number: 5
Enter its power: 3
5.000000 to the power of 3.000000 = 125.000000
7 ^ 3 = 343.000000
4.73 ^ 12 = 125410439.217423
32.01 ^ 1.54 = 208.036691
I looked for a solution in the internet and I found sscanf. I tried to modified the code, it still compiles with gcc but yielding the same results.
Code:
#include <stdio.h>
#include <math.h>
int main()
{
char line[100];
long double r = 0;
long double p = 0;
printf ("Enter a number: ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%lf",&r);
printf ("Enter its power: ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%lf",&p);
printf ("%lf to the power of %lf = %lf\n", r, p, pow(r, p));
printf ("7 ^ 3 = %lf\n", pow (7.0,3.0));
printf ("4.73 ^ 12 = %lf\n", pow (4.73,12.0));
printf ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54));
return 0;
}
Using the gcc compiler of MinGW this is the result: (MinGW gcc (GCC) 4.5.1)
Code:
Enter a number: 5
Enter its power: 3
5.000000 to the power of 0.000000 = 0.000000
7 ^ 3 = 343.000000
4.73 ^ 12 = 125410439.217423
32.01 ^ 1.54 = 208.036691
The previous yields the same result as before where the part "5.000000 to the power of 0.000000 = 0.000000" is not working as intended. If I use the compiler from Visual Studio 2008 C++, this is the result:
Code:
Enter a number: 5
Enter its power: 3
5.000000 to the power of 3.000000 = 125.000000
7 ^ 3 = 343.000000
4.73 ^ 12 = 125410439.217423
32.01 ^ 1.54 = 208.036691
I made some modifications, but it's still the same.
Quote:
Originally Posted by
grumpy
The format specifier "%lf" is for double, not long double. Use "%Lf" instead.
Similarly, the function for raising long double's to a power is powl() not pow().
If your code worked with Visual Studio, you just got lucky. The mismatching of format specifiers formally gives undefined behaviours (which means anything is allowed to happen).
I edited the code as you suggested but it still yields the same result. But still, thank you very much for seeing my mistake. It should have been %Lf instead of %lf. Anyway, the result is still the same in my MinGW gcc.
Code:
#include <stdio.h>
#include <math.h>
int main()
{
char line[100];
long double r = 0;
long double p = 0;
printf ("Enter a number: ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%Lf",&r);
printf ("Enter its power: ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%Lf",&p);
printf ("%Lf to the power of %Lf = %Lf\n", r, p, pow(r, p));
printf ("7 ^ 3 = %Lf\n", pow (7.0,3.0));
printf ("4.73 ^ 12 = %Lf\n", pow (4.73,12.0));
printf ("32.01 ^ 1.54 = %Lf\n", pow (32.01,1.54));
return 0;
}
Here's the output:
Code:
Enter a number: 5
Enter its power: 3
5.000000 to the power of 0.000000 = 0.000000
7 ^ 3 = 343.000000
4.73 ^ 12 = 125410439.217423
32.01 ^ 1.54 = 208.036691
Regarding the powl() function, i didn't try it seems, as shown in the last three lines of the result, pow is already working great. And I tried to search powl() in Google, I cannot seem to find a documentation about it. Anyway, I believe pow() is already working fine. It's just that my scanf() isn't.
But if I change them all to double. They seem to work fine though.
Code:
#include <stdio.h>
#include <math.h>
int main()
{
double input = 0.0;
double power = 1.0;
printf ("Enter a number: ");
scanf ("%lf", &input);
printf ("Enter its power: ");
scanf ("%lf", &power);
printf ("%.2lf to the power of %.2lf = %.2lf\n", input, power, pow(input, power));
printf ("7 ^ 3 = %lf\n", pow (7,3));
printf ("4.73 ^ 12 = %lf\n", pow (4.73,12));
printf ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54));
return 0;
}
My MinGW gcc compiler does scan and print the correct values.
Code:
Enter a number: 5
Enter its power: 3
5.00 to the power of 3.00 = 125.00
7 ^ 3 = 343.000000
4.73 ^ 12 = 125410439.217423
32.01 ^ 1.54 = 208.036691
But I want to use the long double. :(