0

I am working on my knowledge in C++ and I am expecting a value which I did not receive. Here is my code

#include <iostream> 
#include <float.h>

using std:: cout;
using std:: endl;
using std:: fixed;

float a = 10.0 / 3;
a = a * 10000000000000000000;
double b = 10.0 / 3;
b = b * 10000000000000000000;
long double c = 10.0 / 3;
c = c * 10000000000000000000;

cout << fixed << a << endl; // I get 33333333268354826240.000000
cout << fixed << b << endl; // I get 33333333333333336240.000000
cout << fixed << c << endl; // I get 33333333333333336240.000000 but this should be more accurate
cout << FLT_DIG << endl; // I am getting 6 
cout << DBL_DIG << endl; // I am getting 15 
cout << LDBL_DIG << endl; // I am getting 15 but this should be 18 

So am I doing something wrong? I followed how the tutoiral did it but I'm not sure why. So I am using Microsoft Visual Studio 2019 if that helps.

MarioM
  • 15
  • 2
  • Do `cout << sizeof(long double)`. If that is `8`, then your `long double` is the same size as your `double`, which totally allowed. – NathanOliver Nov 09 '21 at 20:35
  • 1
    https://learn.microsoft.com/en-us/cpp/c-language/type-long-double?view=msvc-170 "The long double type is identical to the double type." (note this is true for ony MSVC) – bolov Nov 09 '21 at 20:37
  • 1
    Note than both `b` and `c` are initialized with the same `double` quotient `10.0 / 3`. If code wants a `long double` division to initialize `c`, use `long double c = 10.0L / 3;` – chux - Reinstate Monica Nov 09 '21 at 20:48
  • MarioM, Concerning "I am getting 15 but this should be 18", who or what text suggested _should be 18_? – chux - Reinstate Monica Nov 09 '21 at 20:51
  • Hey guys, I tried the cout << sizeof(long double) so I is the same as double then. Thank you for you help. – MarioM Nov 09 '21 at 22:00

0 Answers0