Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something wrong with the way I am using a long double?

Tags:

c++

I have recently become interested in learning about programming in c++, because I want to get a bit deeper understanding of the way computers work and handle instructions. I thought I would try out the data types, but I don't really understand what's happening with my output...

#include <iostream>
#include <iomanip>

using namespace std;

int main() {

    float fValue = 123.456789;
    cout << setprecision(20) << fixed << fValue << endl;
    cout << "Size of float: " << sizeof(float) << endl;

    double dValue = 123.456789;
    cout << setprecision(20) << fixed << dValue << endl;
    cout << "Size of double: " << sizeof(double) << endl;

    long double lValue = 123.456789;
    cout << setprecision(20) << fixed << lValue << endl;
    cout << "Size of long double: " << sizeof(long double) << endl;

    return 0;
}

The output I expected would be something like:

123.45678710937500000000
Size of float: 4
123.45678900000000000000
Size of double: 8
123.45678900000000000000
Size of long double: 16

This is my actual output:

123.45678710937500000000
Size of float: 4
123.45678900000000000000
Size of double: 8
-6518427077408613100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000000000000000000
Size of long double: 12

Any ideas on what happened would be much appreciated, thanks!

Edit:

System:
Windows 10 Pro Technical Preview
64-bit Operating System, x64-based processor
Eclipse CDT 8.5

MinGW Installation

like image 468
Cyber Storm Avatar asked Feb 15 '15 07:02

Cyber Storm


People also ask

Should I use long or double?

If you need either more digits in the number, or a bigger range, move to double , and if that's not good enough, use long double . But for most things, double should be perfectly adequate.

Is long double more precise?

In C and related programming languages, long double refers to a floating-point data type that is often more precise than double precision though the language standard only requires it to be at least as precise as double .

What is the difference between double and long double?

The main difference between double and long double is that double is used to represent a double precision floating point while long precision is used to represent extended precision floating point value. In brief, long double provides more precision than double.

How do you represent a long double?

%Lf format specifier for long double %lf and %Lf plays different role in printf. So, we should use %Lf format specifier for printing a long double value.


1 Answers

From the patch that fixed this in earlier versions:

MinGW uses the Microsoft runtime DLL msvcrt.dll. Here lies a problem: while gcc creates 80 bits long doubles, the MS runtime accepts 64 bit long doubles only.

This bug happens to me when I use 4.8.1 revision 4 from MinGW-get (the most recent version it offers), but not when I use 4.8.1 revision 5.

So you are not using long double wrong (although there would be better accuracy to do long double lValue = 123.456789L to make sure it doesn't take 123.456789 as a double, then cast it to a long double).

The easiest way to fix this would be to simply change the version of MinGW you are using to 4.9 or 4.7, depending on what you need (you can get 4.9 here).

If you are willing to instead use printf, you could change to printf("%Lf", ...), and either:

  • add the flag -posix when you compile with g++
  • add #define __USE_MINGW_ANSI_STDIO 1 before #include <cstdio> (found this from the origional patch)

Finally, you can even just cast to a double whenever you try to print out the long double (there is some loss of accuracy, but it shouldn't matter when just printing out numbers).

To find more details, you can also look at my blog post on this issue.

Update: If you want to continue to use Mingw 4.8, you can also just download a different distribution of Mignw, which didn't have that problem for me.

like image 162
Morgan Redshaw Avatar answered Sep 21 '22 13:09

Morgan Redshaw