Thread: doubles don't work!!!!!!!

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    2

    doubles don't work!!!!!!!

    I downloaded both the newest (12.I forget) and 10.05 versions of codeblocks on my windows 7 64bit laptop. Whenever I try and do any sort of operation with a double type variable (print, scan, any sort of math), even if it is initialized to something, the result always comes up as 0.00000000. Anyone got any ideas?

    for example

    double i = 9.4;
    printf ("%lf", i);

    prints 0.0000000

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Don't use %lf with printf. Just use %f for both float and double.
    scanf needs %lf for a double, though.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by oogabooga View Post
    Don't use %lf with printf. Just use %f for both float and double.
    scanf needs %lf for a double, though.
    That's correct. It really shouldn't matter though.

    OP, please submit a complete snippet that is compilable and exhibits the problem you're observing.

    E.g.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        double i = 9.4;
        printf ("%lf", i);
        return 0;
    }

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're right of course that it shouldn't matter, but sadly it does on gcc 4.7.1, at least.
    The ell should just be ignored, but instead it gets weird.
    E.g., your code snippet just prints 0.00000000.
    I guess it's a bug.
    EDIT: Just did a quick test. Apparently gcc (4.7.1, at least) interprets a small ell as a capital ell, i.e., if you make i a long double, it prints correctly.
    Last edited by oogabooga; 10-30-2013 at 12:27 AM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by oogabooga View Post
    You're right of course that it shouldn't matter, but sadly it does on gcc 4.7.1, at least.
    The ell should just be ignored, but instead it gets weird.
    E.g., your code snippet just prints 0.00000000.
    I guess it's a bug.
    EDIT: Just did a quick test. Apparently gcc (4.7.1, at least) interprets a small ell as a capital ell, i.e., if you make i a long double, it prints correctly.
    Weird. It's not undefined behaviour... it's defined as having no effect

    .... has no effect on a following a, A, e, E, f, F, g, or G conversion specifiers.
    Must be gcc bug.

    For the snippet I provided, gcc 4.8.2 prints 9.400000

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    2
    I tried all the changes you guys suggested but still no luck. I was wondering, is it possible that my anti-virus might have stopped a part of codeblocks from downloading? I scrolled through the logs and it didn't mention anything. Also I would expect an error from codeblocks if it was missing some of its self.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    No, that's not possible. You're just doing something wrong. Try this:
    Code:
    #include <stdio.h>
     
    int main(void)
    {
        double d = 9.4;
        printf ("%f\n", d);
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array of doubles
    By hencherz in forum C++ Programming
    Replies: 8
    Last Post: 02-26-2012, 10:24 AM
  2. Distinct doubles
    By ArlexBee-871RBO in forum C++ Programming
    Replies: 20
    Last Post: 03-02-2009, 07:02 PM
  3. Using Doubles
    By jay kay in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2005, 01:14 PM
  4. Problem with doubles
    By Asbestos in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2005, 05:47 PM
  5. How can I work with doubles and stil get exact values?
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2001, 02:08 AM