Thread: Double Precision Problem

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    13

    Question Double Precision Problem

    Hello guys,
    I'm new on C. And I have problems with double precision.

    Whenever I build & run the code, the output is:
    mass of electron is -0.000000
    (I use Code::Blocks)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
        long double me=9.1094e-31;
        printf("mass of electron is %Lf\n",me);
    }
    With my best regards, and thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You need to print out more decimal places. C only uses 6 places after the decimal by default. Try "%.35Lf". That gives you 35 digits after the decimal. Adjust as needed.

    EDIT: Also, check out this article (link). Understanding the limits of floating point precision will be very handy when dealing with scientific calculations like yours, since you are working with such large exponents (whether positive or negative).
    Last edited by anduril462; 10-19-2012 at 03:02 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    Problem was solved. Thank you

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    main()
    {
        double me=9.1094e-31;
        printf("mass of electron is %.40lf\n",me);
    
    
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Check the edit in my previous post, there's a very useful link there.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For extra fun, try "%e" instead and see what happens.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double precision is not correct
    By Ilyas Patanam in forum C Programming
    Replies: 5
    Last Post: 06-24-2011, 01:25 AM
  2. double precision error on Mac OSX
    By cfdprogrammer in forum C Programming
    Replies: 13
    Last Post: 10-01-2009, 03:01 AM
  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

Tags for this Thread