Thread: Double precision is not correct

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    Double precision is not correct

    Code:
    int main()
    {
    double n =.00000006;
    printf("n: %f", n);
    return 0;
    }
    it prints n = .000000

    Any help is appreciated.

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Try %.8f

    By default, %f only prints to 6 decimal places.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    printf(3): formatted output conversion - Linux man page
    Try using the %e or %g conversion formats as well

    Try something like "%.15f" (see the field width and precision qualifiers in the manual).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    thanks %.8f, this seemed to do the trick. how do i find the max value for float. I understand there is a float.h header but I'm a noob and I wasn't able to find anything on google how to print float_max value?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    For the float type, FLT_MAX. For the double type, DBL_MAX. For the long double type (assuming a C99 compiler, possibly an extension with older compilers), LDBL_MAX. Each of those are macros are defined in <float.h>.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    If I want more than 6 decimal places, will I have to specify for each instance or is there a way to change the default?

    Also, is there a way for it to omit the extra zeroes in the end it would make it the output cleaner for me.

    thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double precision error on Mac OSX
    By cfdprogrammer in forum C Programming
    Replies: 13
    Last Post: 10-01-2009, 03:01 AM
  2. numerical precision of a double
    By mc61 in forum C Programming
    Replies: 6
    Last Post: 03-11-2008, 12:23 PM
  3. streams of double and precision
    By zeb in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2008, 06:11 AM
  4. extended vs double precision
    By Lind in forum C Programming
    Replies: 2
    Last Post: 09-19-2007, 02:02 PM
  5. More precision than a double
    By manutd in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2006, 06:49 AM