3

As you know long double is identical to double in Visual Studio, unfortunately. However the intellisense feature shows a different story. For example check out the following code;

#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
    cout << "Size of       float: " << sizeof(float)  << endl
         << "Size of      double: " << sizeof(double) << endl
         << "Size of long double: " << sizeof(long double)  << endl;


    cout << endl << setprecision(30) <<
        "      float: " << 125.6f << endl << 
        "     double: " << 125.6  << endl <<
        "long double: " << 125.6L;

    return 0;
}

Output is

Size of       float: 4
Size of      double: 8
Size of long double: 8

      float: 125.59999847412109375
     double: 125.599999999999994315658113919
long double: 125.599999999999994315658113919

But if you hover your mouse over 125.6L and 125.6, you see a difference in precision; double precision

long double precision

Although long double and double are identical in Visual Studio, what is the reason for the difference?

wohlstad
  • 12,661
  • 10
  • 26
  • 39
Kitiara
  • 343
  • 6
  • 21
  • 1
    2 arithmetic types of the same "category" and the same size are not necessarily the same, even if they behave exactly the same. You may see the same issue with `int`, `long` and `long long`... – fabian Aug 07 '22 at 11:58
  • 1
    @fabian Ok, so what makes it different ? – Kitiara Aug 07 '22 at 12:04
  • You shouldn't focus on the differences between Visual Studio and Intellisense, but on the differences in how your compiler interprets them. I found a similar question with such an answer: --> https://stackoverflow.com/a/3454586/10133085 – Adam Kuzański Aug 07 '22 at 12:14
  • Does this answer your question? [long double vs double](https://stackoverflow.com/questions/3454576/long-double-vs-double) – Adam Kuzański Aug 07 '22 at 12:14
  • @Kitiara The treatment of those types by the compiler. Note that those being treated differently is probably the result of trying to keep programs compatible regardless of the size of the number of bits a integral type uses. Without this, you may get a compiler error on some platforms, but not on others for the following code: `void f(int){} void f(long){}` – fabian Aug 07 '22 at 12:22
  • 1
    @AdamKuzański No that question is different. I have got `double` and `long double` as the same size. The intellisense shows a difference in precision but `double` and `long double` in my system behaves the same. I just wonder what makes intellisense to show different precision. Also note that `std::is_same::value` equals `0`. – Kitiara Aug 07 '22 at 12:25
  • 4
    There is no difference in precision, it is just the display that happens to be different. You lose some digits to squeeze in the the `L` in the box. – BoP Aug 07 '22 at 12:38
  • 2
    @BoP Looks like you are right, the digit also rounded in some cases. – Kitiara Aug 07 '22 at 12:44
  • 1
    @Bop At last! Some (reliable) facts. – Paul Sanders Aug 07 '22 at 12:46
  • 1
    @Kitiara Hi, glad to know you've found the solution to resolve this issue! Please consider adding an answer and changing its status to Answered. See [can I answer my own question..](https://stackoverflow.com/help/self-answer), Just a reminder :) – Jingmiao Xu-MSFT Aug 19 '22 at 09:31

1 Answers1

1

It appears that it's just the display that's different. The numeric section of the display seems to have a character limit and the character L causes the numeric section to round in some cases and even truncated.

Kitiara
  • 343
  • 6
  • 21