Thread: Problem with printing float precision values.

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    Problem with printing float precision values.

    Hi,

    It seems simple problem for programmers but for me its complicated.

    I am using CodeVision compiler. I am writing code in c language. I am trying to print float value with up to 10 digits precision. I have tried using "%.10f" but it is printing only six precision digits, even with "%.f". i know "%.f" will print six precision digits.
    Code:
    float c, xc=50800.287F, w, f=125000.0F;
    w=2*3.14*f;
    c=1/w*xc;  /which is equal to =1/39878000000=2.507e-11.
    I want to print that "c" value. I have tried to print using the fallowing statement.
    Code:
    printf("capacitance value is : %.12f", c);
    But from the above statement i getting value like "0.000000".

    Please suggest how can i print that C value.

    I tried to use "double variable but my CodeVision is not supporting double variables. help me please.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The code bellow is going to print a value with 12 digits precision
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       double c, xc=50800.287F, w, f=125000.0F;
       w=2*3.14*f;
       c=1/w*xc;
    
       printf("capacitance value is : %.12f\n", c);
    
       return 0;
    }
    Are you sure that double is not supported?I mean it is a primitive type of C.... What error do you get?

    EDIT : Ok, i think you have a logical error.
    What you want is
    Code:
    c=1/(w*xc);
    but you have
    Code:
    c=1/w*xc;
    The problem is that in your code the code will be executed like this : (1/w)*xc

    Mind also that you need at least 11 digits precision otherwise you will see only zeros.

    And with a second thought i found out that float is ok.So i tried it...
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       float c, xc=50800.287, w, f=125000.0;
       w=2*3.14*f;
       c=1/(w*xc);
    
       printf("capacitance value is : %.12f\n", c);
    
       return 0;
    }
    and the output was
    Code:
    Macintosh-c8bcc88e5669-9:~ usi$ ./px
    capacitance value is : 0.000000000025
    Macintosh-c8bcc88e5669-9:~ usi$
    Hope this helps
    Last edited by std10093; 11-22-2012 at 03:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing float number with incremented precision
    By flanderso in forum C Programming
    Replies: 7
    Last Post: 09-07-2012, 12:24 AM
  2. Replies: 2
    Last Post: 06-29-2012, 05:11 AM
  3. float accumulator precision?
    By chunlee in forum C Programming
    Replies: 8
    Last Post: 11-12-2004, 02:25 AM
  4. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  5. setting a float to 4 digit decimal precision
    By river-wind in forum C Programming
    Replies: 8
    Last Post: 01-21-2002, 05:03 PM

Tags for this Thread