2

I'm working with C, I have to do an exercise in which I have to print the value of long double min and long double max.

I used float.h as header, but these two macros (LDBL_MIN/MAX) give me the same value as if it was just a double.

I'm using Visual Studio 2015 and if I hover the mouse on LDBL MIN it says #define LDBL_MIN DBL_MIN. Is that why it prints dbl_min instead of ldbl_min?

How can I fix this problem?

printf("Type: Long Double Value: %lf Min: %e Max: %e Memory:%lu\n", 
    val10, LDBL_MIN, LDBL_MAX, longd_size);

It is a problem because my assignment requires two different values for LDBL and DBL.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Tsurupeta
  • 63
  • 1
  • 8
  • 1
    Post the code/output rather than just _describe_ the code/output. Why do you think the values of `LDBL_MAX/DBL_MAX` must differ or is a problem? – chux - Reinstate Monica Oct 05 '16 at 17:30
  • MSVC has `#define LDBL_MAX DBL_MAX` and similar for MIN. It does implement `long double` but it is the same as `double`. – Weather Vane Oct 05 '16 at 17:32
  • The code is: printf("Type: Long Double Value: %lf Min: %e Max: %e Memory:%lu\n", val10, LDBL_MIN, LDBL_MAX, longd_size); It is a problem because my assignment requires two different values for LDBL and DBL – Tsurupeta Oct 05 '16 at 17:33
  • This question was caused by a problem that **can no longer be reproduced** or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting. – too honest for this site Oct 05 '16 at 17:34
  • 1
    @Tsurupeta: Why are you trying to `printf` a supposedly `long double` value with `%e`? `%e` is for `float` (and `double`). For `long double` you need `%Le`. – AnT stands with Russia Oct 11 '16 at 22:09
  • @Tsurupeta Any more help needed with this post? – chux - Reinstate Monica May 03 '21 at 18:45

3 Answers3

3

C does not specify that long double must have a greater precision/range than double.

Even if the implementation treats them as different types, they may have the same implementation, range, precision, min value, max value, etc.

Concerning Visual Studio, MS Long Double helps.

To fix the problem, use another compiler that supports long double with a greater precision/range than double. Perhaps GCC?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
2

From this reference on the lfoating point types:

long double - extended precision floating point type. Matches IEEE-754 extended floating-point type if supported, otherwise matches some non-standard extended floating-point type as long as its precision is better than double and range is at least as good as double, otherwise matches the type double. Some x86 and x86_64 implementations use the 80-bit x87 floating point type.

Added emphasis is mine.

What the above quote says is that while a compliant C compiler must have the long double type, it doesn't really have to support it differently than double. Something which is probably the case with the Visual Studio C compiler.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Those macros are either broken, or long double is just an alias for double on your system. To test, set a long double to DBL_MAX, multiply by two, then subtract DBL_MAX from it. If the result is finite, then you have extra exponent space in the long double. If not, and long double is bigger than double, the extra bytes could just be padding, or you could have the same exponent space and more precision. So LDBL_MAX's genuine value will be just a smidgen over DBL_MAX.

The easiest way to generate the max is simply to look up the binary representation. However if you want to do it in portable C, you can probe it by repeated multiplications to get the magnitude, then fill out the mantissa by repeatedly adding descending powers of two until you run out of precision.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18