Thread: Having a problem with dividing long doubles...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    4

    Having a problem with dividing long doubles...

    Hi,

    I'm afraid I'm a novice in c programming, and no matter what I do I cannot get a division to work:

    Code:
            //END RANGE INPUT
            long double End;
            printf("\nPlease enter the start of the range (Lower Bound):\n\n");
            scanf("%Lf",&End);
            //START RANGE INPUT
            long double Start;
            printf("\nPlease enter the end of the range (Upper Bound):\n\n");
            scanf("%Lf",&Start);
            //PLOT INTERVAL INPUT
            long double Interval;
            printf("\nPlease enter the interval between each plot:\n\n");
            scanf("%Lf",&Interval);
            //RANGE N INPUT
            int N;
            printf("\nPlease enter a value for N:\n\n");
            scanf("%d",&N);
            //RANGE SIZE CREATOR
            long double SizeL;
            SizeL = ((Start-End)/Interval);
            printf("%Lf",Start);
            printf("%Lf",End);
            printf("%Lf",Interval);
            printf("%Lf",SizeL);
    No matter what I input for the values of 'Start', 'End' and 'Interval', the value of 'SizeL' always seems to be -2.

    I've got no idea how to fix this, but any and all help would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Turn up the warnings on your compiler in the first instance. long double (and the scanf())/printf() %LF format specifier) is not supported by all compilers (particularly older compilers). If you're using a compiler that doesn't support those features (or supports them in a manner that is broken) then you'll need to get a compiler/library that does.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    4
    I'm pretty sure it isn't a problem with the %Lf not being supported- all the other variables printf with correct values, without any issues .

    Edit: It seems when the long doubles are inputted on their own, they can then be printed without difficulty. But when any long double which has a value from an equation using other long doubles, it always gives -2 as the printf...
    Last edited by Danni111111; 02-07-2014 at 08:29 AM.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    It would help to step through the code with a source level debugger to actually confirm if the variables are getting proper values. If scanf and printf with %L treat it as a reference to a double instead of a long double, then printf would correctly display the values input by scanf, even though the long double variables had regular double variables stored in them. Once you did math with the messed up variables, you would get an error, although I'm not sure why it would always show -2 as a result (could be related to the range of values input).

    You could also change the variables to be regular doubles and test that with %Lf in scanf and printf, if that works, then %Lf is apparently the format specifier for doubles instead of long doubles. If there isn't an alternative format specifier for long doubles, you may have to input and output doubles, then convert to and from long doubles in your code. You could also create custom functions to convert a string to a long double and back.

    Link to a question / answer with someone else having similar problems:

    c - printf and long double - Stack Overflow
    Last edited by rcgldr; 02-07-2014 at 10:57 AM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @Danni111111:
    You don't mention which compiler you're using, the version, what architecture you're on, etc. This seems to be a fairly compiler specific issue. It works for me with the following: gcc version 4.6.3, running Ubuntu, on Intel x84-64.

    Note, giving us the exact input you used may help too. It could be that just certain values don't work right.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Try %llf instead of %Lf everywhere.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Danni111111 View Post
    I'm pretty sure it isn't a problem with the %Lf not being supported- all the other variables printf with correct values, without any issues.
    Being able to print an int correctly with the %d format has no relationship to being able to being able to print a float with %f, let alone the ability to print a long long double with %Lf. A C89 compiler, for example, is guaranteed to support the first two, but not the last.

    Different code, within the implementation of the printf() family of functions themselves is involved.

    As others have said, you need to identify your compiler, compiler version, operating system, operating system version inputs you are providing, and outputs being received.

    AND you need to specify whether that code you have supplied is complete. If there is other code involved that you have deemed irrelevant, it may be a malfunction in that code which is compromising behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    When I'm faced with a problem like this I like to rule stuff out. I would have written a simple test case such as

    Code:
    #include <stdio.h>
     
    int main(void)
    {
        long double SizeL, Start, End, Interval;
        
        Start = 100.0;
        End = 200.0;
        Interval = 3.0;
        
        SizeL = ((Start-End)/Interval);
        
        printf("%Lf\n",SizeL);
        
        return 0;
    }
    And see if that produces the expected output. If it does, then you've narrowed the bug down to how you're getting the numbers. If it doesn't then you've isolated the bug.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Kinda confusing asking for start of range and storing value into End and then asking for end of range and storing into Start... but, whatever.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem while printing unsigned long long int
    By Dedalus in forum C Programming
    Replies: 3
    Last Post: 03-08-2012, 04:44 AM
  2. dividing Doubles
    By yukapuka in forum C++ Programming
    Replies: 9
    Last Post: 06-13-2008, 05:39 PM
  3. help with dividing doubles into integers
    By Amyaayaa in forum C++ Programming
    Replies: 4
    Last Post: 02-21-2008, 04:57 PM
  4. Limits of long doubles
    By Xanth in forum C++ Programming
    Replies: 3
    Last Post: 04-04-2005, 06:01 AM
  5. Please help - dividing doubles
    By hunterdude in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2004, 12:06 AM