Like Tree1Likes
  • 1 Post By bivhitscar

Double precision is not correct

This is a discussion on Double precision is not correct within the C Programming forums, part of the General Programming Boards category; Code: int main() { double n =.00000006; printf("n: %f", n); return 0; } it prints n = .000000 Any help ...

  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.
    Ilyas Patanam likes this.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,454
    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%.

  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, 05: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, 05:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21