CrazyEngineers
  • Decimal numbers in C language

    rukawa

    rukawa

    @rukawa-Y0pUkC
    Updated: Oct 22, 2024
    Views: 999
    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?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Jeffrey Arulraj

    MemberNov 10, 2012

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

    Even upto 10^-1440 and beyond is representable in C language
    Are you sure? This action cannot be undone.
    Cancel
  • rukawa

    MemberNov 11, 2012

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberNov 11, 2012

    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😎
    Are you sure? This action cannot be undone.
    Cancel
  • rukawa

    MemberNov 11, 2012

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberNov 11, 2012

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberNov 11, 2012

    #-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: <a href="https://gmplib.org/" target="_blank" rel="nofollow noopener noreferrer">The GNU MP Bignum Library</a>
    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Jeffrey Arulraj

    MemberNov 11, 2012

    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
    Are you sure? This action cannot be undone.
    Cancel
  • rukawa

    MemberNov 11, 2012

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • Jeffrey Arulraj

    MemberNov 11, 2012

    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
    Are you sure? This action cannot be undone.
    Cancel
  • Chinu1

    MemberNov 29, 2012

    #include<stdio.h>
    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<n)
    return 0;
    }
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register