5

How to convert double to long double as C++ standard. I think just casting like this is not the right way?

double value = 1.2;
long double newValue = (long double)value;
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
  • 1
    That will give you a long double containing the specified value (but beware that compilers are allowed to implement `double` and `long double` as the same type, and some do so). – Jerry Coffin Mar 02 '15 at 07:09
  • 1
    off topic, but is there a processor that supports long double natively? – thang Mar 02 '15 at 07:12
  • 1
    @thang: Yes--x86 does (for one example). It supports 32-bit (float), 64-bit (double) and 80-bit (long double) types in hardware. – Jerry Coffin Mar 02 '15 at 07:13
  • 1
    @JerryCoffin, you're right. I totally missed it. Seems Intel wants to get rid of it though: http://stackoverflow.com/questions/3206101/extended-80-bit-double-floating-point-in-x87-not-sse2-we-dont-miss-it. – thang Mar 02 '15 at 07:15
  • 1
    @thang: more or less, yeah. They certainly want to get rid of x87 (stack based processor, so using all registers was difficult), and haven't included 80-bit types in the SSE instruction sets. – Jerry Coffin Mar 02 '15 at 07:16
  • What do you mean by "convert as C++ standard"? – juanchopanza Mar 02 '15 at 07:54

3 Answers3

6

That will work fine, but it will not magically create extra precision in newValue. That is, it will not produce the same result as:

long double newValue = 1.2L;

which will set newValue to a closer approximation to 1.2.

rici
  • 234,347
  • 28
  • 237
  • 341
  • what do you think `about type` cast Vs `static cast` (your answer vs Evgenij Malashkin answer). which one is the C++ way? – Nayana Adassuriya Mar 02 '15 at 07:47
  • @NayanaAdassuriya: casting double to long double is an exact widening cast (because every `double` value is representable as a `long double`). Consequently, it is almost never necessary to cast, and my interpretation of the "C++ way" is that implicit casts are preferred in this case. If an explicit cast is required, I would normally use constructor syntax (`double(foo+3)`), but that's not possible with `long double` unless the `long double` is parenthesized, in which case it becomes a C-style cast `(long double)(foo+3)`, and that's how I would write it (but only if it was really necessary.) – rici Mar 02 '15 at 19:34
4

The standards guarantee that a long double can support all the values that a double can (or, to put it another way, the set of values a double can support is a subset of what a long double can represent).

So, the assignment

long double newValue = value;

is sufficient, albeit involving an implicit conversion of value from double to long double rather than an explicit conversion. It is a conversion that does not lose precision.

Explicit alternatives in C++ are

long double newValue = (long double)value;    // as in original question
long double newvalue = static_cast<long double>(value);

It is really a matter of subjective style which alternative is considered better, because they all achieve the same effect without potential loss of precision.

Note that, if going the other way (converting from long double to double) it is often preferable to use an explicit conversion, since the conversion potentially loses precision (and can therefore change the value) - which is why compilers often (can be configured to) give warnings on such implicit conversions.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Rob
  • 1,966
  • 9
  • 13
2

Employ static_cast:

long double newValue = static_cast<long double>(value);

Or in C++11 style:

auto newValue = static_cast<long double>(value);

(known as "explicitly typed initializer idiom").