# Thread: newb question - simple code broke after changing long long int to long double

1. ## newb question - simple code broke after changing long long int to long double

So this is a code which finds the non negative power of a number. It works for integers.

I had a few of the variables as long long ints. I changed them to long doubles, modified the code accordingly (so I thought). Now the most basic calculations such as 7^2 give back outrageously large numbers. Seeing as all I did was replace the integers with floats, I don't know where to start. I know about buffer overflow, but... I don't see how that can be happening. Here is the code, any insight will be appreciated.

Code:
```#include <stdio.h>

int main(void){

int x, power;
long double number, placeholder, total;

// The above were previously long long int, and the code worked.

// The below portion the user inputs a number, and an exponent value.

printf("calculate the exponential value of a number::\n\nenter a number: ");
scanf("%Lf", &number);

printf("enter the value of the exponent (value must not be negative): ");
scanf("%d", &power);

// This loop just checks whether the power is negative, inwhich case it terminates

if (power < 0){
printf("improper exponent value\nprocess terminated");
return 0;
}

// This is just my way of dealing with a 0 power

else if (power == 0){
total = 1;
}

// Here is where the real loop calculations take place. It is just looping to calculate the power
// of a number, uses a placeholder to recalculate and then dumps that out at the end.

else {
placeholder = number;
x = 1;
while (x < power){
placeholder = placeholder * number;
x++;
}
total = placeholder;
}
printf("\ntotal = %.3Lf", total);
return 0;
}```
So it works with int, not with float. Any help is appreciated

2. Originally Posted by Ian Rust
I had a few of the variables as long long ints. I changed them to long doubles, modified the code accordingly (so I thought). Now the most basic calculations such as 7^2 give back outrageously large numbers. Seeing as all I did was replace the integers with floats, I don't know where to start. I know about buffer overflow, but... I don't see how that can be happening. Here is the code, any insight will be appreciated.
7^2 (where ^ denotes exponentiation) works for me. I suggest that you check the return value of the scanf calls, and also check that the value stored matches (at least approximately) what was entered (e.g., by using a debugger or printing the value).

By the way, you need to work on your indentation.

3. Hmm.... Ok I will check it out. Thank you for the feedback.

4. I checked the scan calls by printing them. They are accurate. I am using codeblocks and GCC (I think, or something like that). Also this is in C, not C++. Any other ideas? Anyone else it works or does not work for?

At this point, whatever value I put into the code (above) returns -2.000. 7^2, 8^5, etc. printf indicates that the scans are working correctly. The problem has to be in the final loop, and somehow related to floats. I think, anyway... I do not know.

5. What is your current code? What is your test input, expected output and actual output?

6. Currently the code is unchanged from the one I posted. My input is 7 for the number, 2 for the power. The expected output is 49.000. The actual output for that, and for other inputs such as 6^3, etc. are all -2.000.

What is more strange - previously (when using integer variables) when I input 0 for power, it would return total = 1 (as the 2nd loop states). Now if I put in 0 as power, it returns total as 0.000.

If I use a negative power, the process does terminate as expected.

Power and Number are set correctly.

Somehow total is not being set properly, such as with the statement total = 1, yet printf("%Lf", total) = 0.000

7. Here is the code modified with test statements for the scans and test statements for input of power as 0. When I input power as 0, total comes out as 0.000. This is odd because the statement total = 1 immediately precedes the output statement.

Otherwise, if power is input as a positive number, the output for total = -2.000.

Code:
```#include <stdio.h>

int main(void){

int x, power;
long double number, placeholder, total;

// The above were previously long long int, and the code worked.

// The below portion the user inputs a number, and an exponent value.

printf("calculate the exponential value of a number::\n\nenter a number: ");
scanf("%Lf", &number);

//test statement 1 works
printf("\ntest for first input: %Lf\n", number);

printf("enter the value of the exponent (value must not be negative): ");
scanf("%d", &power);

//test statement 2 works
printf("\ntest for second input: %d", power);

// This loop just checks whether the power is negative, inwhich case it terminates

if (power < 0){
printf("improper exponent value\nprocess terminated");
return 0;
}

// The below is just my way of dealing with a 0 power

else if (power == 0){
total = 1;

// Test statement 3. Total was just set to 1; this should output 1.000. But it outputs 0.000
printf("\n\ntest for when input of power = 0: %Lf\n\n", total);

}

// Here is where the real loop calculations take place. It is just looping to calculate the power
// of a number, uses a placeholder to recalculate and then dumps that out at the end.

else {
placeholder = number;
x = 1;
while (x < power){
placeholder = placeholder * number;
x++;
}
total = placeholder;
}

printf("\ntotal = %.3Lf", total);
return 0;
}```

8. Originally Posted by Ian Rust
Currently the code is unchanged from the one I posted. My input is 7 for the number, 2 for the power. The expected output is 49. The actual output for that, and for other inputs such as 6^3, etc. are all -2.000.

What is more strange - previously (when using integer variables) when I input 0 for power, it would return total = 1 (as the 2nd loop states). Now if I put in 0 as power, it returns total as 0.000.

If I use a negative power, the process does terminate as expected.

Somehow total is not being set properly, such as with the statement total = 1, yet printf("%Lf", total) = 0.000
Suppose you compile and run this program using 7 as the input for number and 2 as the input for power:
Code:
```#include <stdio.h>

int main(void) {
int x, power;
long double number, placeholder, total = 1.0;

printf("calculate the exponential value of a number::\n\nenter a number: ");
if (scanf("%Lf", &number) != 1) {
fprintf(stderr, "invalid number\nprocess terminated\n");
return 0;
}

printf("enter the value of the exponent (value must not be negative): ");
if (scanf("%d", &power) != 1 || power < 0) {
fprintf(stderr, "improper exponent value\nprocess terminated\n");
return 0;
}

if (power > 0) {
// Here is where the real loop calculations take place. It is just looping to calculate the power
// of a number, uses a placeholder to recalculate and then dumps that out at the end.
placeholder = number;
x = 1;
while (x < power) {
placeholder = placeholder * number;
x++;
}
total = placeholder;
}
printf("\n%.3Lf^%d = %.3Lf\n", number, power, total);
return 0;
}```
What output do you get?

9. To make this much simpler, this code does not work and I do not know why. The output I get for total = -0.000000

Code:
```#include <stdio.h>

int main(void){
long double total;

total = 1;

printf("\ntest output: %Lf", total);
return 0;
}```

10. Originally Posted by Ian Rust
To make this much simpler, this code does not work and I do not know why:
That is an excellent example of a small and simple program that demonstrates the problem

You noted that your compiler is gcc. What is the version number? What options did you pass to your compiler?

11. For your question, the output I got was 7.000^2686712 = 0.000.

I do not know... I don't remember where I downloaded this compiler, how to check the version number... I don't know what 'options passed to the compiler' are. Perhaps I should learn these things.

I watched a tutorial on youtube, followed the instructions, downloaded codeblocks and had to search a GCC compiler from google. I found the filename "GCC", managed to get it connected to codeblocks. I ... hardly know what I did to get the compiler working.

...

12. Originally Posted by Ian Rust
I do not know... I don't remember where I downloaded this compiler, how to check the version number... I don't know what 'options passed to the compiler' are. Perhaps I should learn these things.
What version of Code Blocks are you using? Are you using Windows? If so, did you download the Code Blocks package that was packaged with MinGW? If not, what operating system (including distribution/version number) are you using?

13. codeblocks 13:12, windows 8.0 ... distribution? I don't know. It is what came on the laptop. Probably home? I don't know, most likely I think it is home. I don't even know how to check that. But good guess it is home. The minGW... I am not sure but I remember that name ... Yes I think I did.

14. Originally Posted by Ian Rust
codeblocks 13:12, windows 8, the minGW... I am not sure but I remember that name ... Yes I think I did.
Ah. A quick search of the Web brings up: How to printf long doubles. The most informative comment appears to be from Keith Marshall:
mingwrt-3.15 onwards[*] provides a fully ANSI conformant printf();
however, it isn't used by default, unless you compile with `-ansi',
`-posix' or, as JonY suggests, `-D__USE_MINGW_ANSI_STDIO'.
If so, what you should do is to try adding the -ansi compile option. You can either wade through project settings in Code Blocks, or search the Web for information how to do that. (EDIT: Actually, I suggest that you add the -std=c99 option or even -std=c11 option instead of -ansi since you are technically using a C99 feature, i.e., // comments. -ansi would be equivalent to -std=c90, if I remember correctly.)

15. Okay ... thank you.
Is there any way I could change that simple code to make it function on an older compiler?
Since I probably want my codes compliant with a variety of compilers anyway.