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

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    13

    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
    Last edited by Ian Rust; 06-07-2015 at 01:58 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,015
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    13
    Hmm.... Ok I will check it out. Thank you for the feedback.
    Last edited by Ian Rust; 06-07-2015 at 02:07 PM.

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    13
    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.
    Last edited by Ian Rust; 06-07-2015 at 02:22 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,015
    What is your current code? What is your test input, expected output and actual output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2015
    Posts
    13
    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. #7
    Registered User
    Join Date
    May 2015
    Posts
    13
    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;
    }
    Last edited by Ian Rust; 06-07-2015 at 02:48 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,015
    Quote 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?
    Last edited by laserlight; 06-07-2015 at 02:48 PM. Reason: A more informative final printf
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2015
    Posts
    13
    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;
    }
    Last edited by Ian Rust; 06-07-2015 at 02:55 PM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,015
    Quote 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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2015
    Posts
    13
    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.

    ...
    Last edited by Ian Rust; 06-07-2015 at 03:05 PM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,015
    Quote 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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2015
    Posts
    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. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,015
    Quote 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.)
    Last edited by laserlight; 06-07-2015 at 03:18 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    May 2015
    Posts
    13
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generate unsigned long long type random numbers
    By patishi in forum C Programming
    Replies: 27
    Last Post: 09-11-2013, 09:03 PM
  2. number bigger than long long double
    By suryak in forum C Programming
    Replies: 9
    Last Post: 08-18-2011, 02:02 PM
  3. Replies: 1
    Last Post: 04-23-2011, 08:40 PM
  4. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  5. STLport with MingW - Long Long error on project build
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 08-21-2006, 08:55 AM