Decimal numbers in C language

Hello
is it possible to have the precision of 20 digits after the comma in C language (for example 10^-20)?If yes, how can I do it?

Replies

  • Jeffrey Arulraj
    Jeffrey Arulraj
    yeah I think they are included in Double and long Double data types

    Even upto 10^-1440 and beyond is representable in C language
  • rukawa
    rukawa
    Thank for your answer. The thing is, I want to display as many digits after the comma as possible. But I've never been able to display more than 6 digits after the comma. Here's an example :
    decimal

    when I calculate 1/238 using the calculator I get the following result : 0,00420168067226890756302521008403
    So what I got from the program is this number but rounded, which effects badly the precision.
    Is there a way to display the full result on the console's screen?
  • rahul69
    rahul69
    Actually first of all use should use %f instead of %e to print if u want to print like calculator prints.
    Now coming to setting the precision.
    As for example u want 20 digits precision u can write:
    printf("%0.20f",x);
    Here 0.20 sets precision upto 20 similarly for exponential form:
    printf("%0.20e",x);
    sets precision in exponential form upto 20.
    Hope it helps😎
  • rukawa
    rukawa
    Thank you rahul69. It was helpful.
    Now when I calculate 1/239 using the float type I don't get the same result as when I calculate it using the double type :

    b

    Plus, the result I get when using a calculator is : 0,00418410041841004184100418410042 .
    The result I got when using the float type is erroneous. The one I got using the double type is more accurate but I was enable to get more than 19 digits after the comma even though I precised in the program that the precision is of 30 digits after the comma. Is there a way to get a result more accurate with a better precision?
  • simplycoder
    simplycoder
    rukawa
    Thank you rahul69. It was helpful.
    Now when I calculate 1/239 using the float type I don't get the same result as when I calculate it using the double type :

    b

    Plus, the result I get when using a calculator is : 0,00418410041841004184100418410042 .
    The result I got when using the float type is erroneous. The one I got using the double type is more accurate but I was enable to get more than 19 digits after the comma even though I precised in the program that the precision is of 30 digits after the comma. Is there a way to get a result more accurate with a better precision?
    Please read on what are floating numbers and how are they stored in memory
    also how to set precision in c
    You would get your doubts cleared.
  • rahul69
    rahul69
    #-Link-Snipped-#, friend ur observation is quite right, and the reason for this is the space occupied by the data types. As double occupies twice as much space as float, so its precision is also twice than float.
    I don't think more is possible with intrinsic data types but do check this link: The GNU MP Bignum Library
    This library can provide limitless precision😉. But I think the library is for C++. Also not sure whether it can be added to ur native IDE, but if u use GCC then it may be possible.
  • Jeffrey Arulraj
    Jeffrey Arulraj
    Accurate outputs also depends on how the data is stored in memory Cause 1 in Float data type is stored as .9999999 rather than 1 this is a cause of the minor shift in your values you got
  • rukawa
    rukawa
    simplycoder
    Please read on what are floating numbers and how are they stored in memory
    also how to set precision in c
    You would get your doubts cleared.
    Thank you for your advice. Do you know a good book or article where I can get this cleared up?
  • Jeffrey Arulraj
    Jeffrey Arulraj
    Do check the CS IT resource section there are lots of books listed in them

    Try Yeshwant Kantekar's books they are really price less
  • Chinu1
    Chinu1
    #include
    int main()
    { int I=0,count=0;
    printf("Enter how many even numbers needed:");
    scanf("%d",&n);
    do {
    if(I%2==0)
    {
    printf("%d",I);
    count++;
    }
    }
    while(count return 0;
    }

You are reading an archived discussion.

Related Posts

All macho men will be up in cudgels against this report from UK, which proves with numbers that women are better drivers than men. That would prove that they are...
The Internet Archive is a not for profit organization that provides free access to historical collections that exist in digital format to all. Registration is free. https://archive.org/about/ There is another...
Biomedical devices often fail for fairly simple reasons. When one looks at the list, one may feel,'I know that'. Yet devices keep failing because of poor attention paid to these...
An engineer isn't an engineer without a calculator. In my engineering days (about a decade ago) the calculators that could solve the matrices problems in a jiffy were a rage...
Both of these are two-dimensional elements employed in finite element methods. now my question is what is the major difference between them, and what are their significances or specialities ?