Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the C++ suffix for long double literals?

In C++ (and C), a floating point literal without suffix defaults to double, while the suffix f implies a float. But what is the suffix to get a long double?

Without knowing, I would define, say,

const long double x = 3.14159265358979323846264338328; 

But my worry is that the variable x contains fewer significant bits of 3.14159265358979323846264338328 than 64, because this is a double literal. Is this worry justified?

like image 288
Walter Avatar asked Feb 04 '14 16:02

Walter


People also ask

What is a long double in C?

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 double and long double in C?

The double and long double are two data types used in programming languages such as C++. 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.

How do you write a long double in C++?

Try this instead: cout << "PI = " << setprecision(40) << pi << endl; If you try the above, you would find that the value actually printed will start losing precision after some decimal places(18-25 I guess). The precision of long double in c/c++ is implementation defined.

How do you do double precision in C++?

Rules and Regulations for Using Double in C++ The C++ double should have a floating-point precision of up to 15 digits as it contains a precision that is twice the precision of the float data type. When you declare a variable as double, you should initialize it with a decimal value.


1 Answers

From the C++ Standard

The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double.

It is interesting to compare with corresponding paragraph of the C Standard. In C there is used term floating constant instead of floating literal in C++:

4 An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double

like image 84
Vlad from Moscow Avatar answered Sep 28 '22 06:09

Vlad from Moscow