0

My problem is my IDE (I don't know; maybe it can be the compiler etc.) doesn't recognize double and long double data types properly.

This is my basic example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    double x=1.126462842684262264;
    long double y=1.126462842684262264;
    printf("x = %lf \n",x);
    printf("x = %f \n",x);

    printf("y = %lf \n",y);
    printf("y = %Lf \n",y);

    return 0;
}

The output is

x = 1.126463
x = 1.126463
y = 1.126463
y = 1.126463

Process returned 0 (0x0)   execution time : 0.019 s
Press any key to continue.

Normally, long double precision's is 19 decimal places and double precision's is 15 decimal places. Why that happens and how to fix it?

  • I am using GNU GCC compiler.
  • My IDE is CodeBLocks.
Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
  • It is true that "long double precision's is 19 decimal places and double precision's is 15 decimal places". Hower, the default precision for `printf` is something else. If you would like to learn how to make `printf` show the full precision of floating point types, you should be able to find that information, and several examples, in your textbook. This is fairly basic information that's explained in every textbook. Is there anything specific in your textbook, about this topic, that's unclear to you? – Sam Varshavchik Nov 02 '20 at 14:37
  • Please don't tag with multiple languages. C and C++ are different languages, and the answer to your question probably depends on which one you are using. – cigien Nov 02 '20 at 14:37
  • 2
    Your variable `y` is unused – Kevin Nov 02 '20 at 14:38
  • @cigien sorry, this is my first question. I didnt notice. – BlgnrSoftDev Nov 02 '20 at 14:46
  • No problem :) At least, now you won't make the same mistake again :) – cigien Nov 02 '20 at 14:47
  • Use printf(" y = %.20lf\n", y); The %.20lf ask to print the number with 20 digits after decimal points. – ytlu Nov 02 '20 at 14:48
  • @SamVarshavchik i understand but i couldnt find any solution in my textbook. if there was , i wouldn't ask. – BlgnrSoftDev Nov 02 '20 at 14:50
  • @cigien yeah, exactly :) – BlgnrSoftDev Nov 02 '20 at 14:50
  • You need to distinguish between decimal places and significant digits. A double typically has an exponent range of about ±308, but supports about 15 significant digits. If the exponent represents tiny values, you'll have to print hundreds of zeros before you get any non-zero digits; if the exponent represents huge values, it may not be able to represent any decimal places with any accuracy. – Jonathan Leffler Nov 02 '20 at 17:18

0 Answers0