-1

I know this may seem too simple of a question but I'm having difficulty wrapping my head around the meaning/implication of double & long. All online resources are giving official definitions. I'm looking for a layman's explanation.

In Short, my question is: What is the difference between a long long, a long double & a long float?

I tried running a quick test (screenshot attached) to see the byte size of each type, but couldn't make sense out of it. How is a long double same size as a double?

enter image description here

Mahmoud Gabr
  • 109
  • 2
  • 12
  • 2
    (removing [tag:c#] - does not apply) – Marc Gravell Jul 28 '21 at 15:29
  • 1
    `screenshot attached` - seems you forgot to attach the screenshot? – Franz Gleichmann Jul 28 '21 at 15:33
  • 4
    `long long` is an integer and `long double` is floating point. `long double` must be at least as large as `double` but need not be larger; it is up to the implementation. `long float` does not exist in standard C. – Nate Eldredge Jul 28 '21 at 15:33
  • [I want to know the difference between a long double and a double](https://stackoverflow.com/q/53095270/995714), [Why are double and long double completely the same on my 64 bit machine?](https://stackoverflow.com/q/8922216/995714) – phuclv Jul 28 '21 at 15:42
  • @phuclv They're useful but they don't answer my question. – Mahmoud Gabr Jul 28 '21 at 15:51

2 Answers2

4

long long is integer (possibly with more range than long)
long double is floating point (possibly with more range/precision than double)
long float does not exist.

The integer types sorted by range are

  • _Bool
  • char or signed char or unsigned char
  • short (or short int) or short unsigned
  • int or unsigned
  • long (or long int) or long unsigned
  • long long (or long long int) or long long unsigned

The floating-point types sorted by range/precision are

  • float
  • double
  • long double
pmg
  • 106,608
  • 13
  • 126
  • 198
  • 1
    There's also the *specific width* integer types: `[u]intN_t`, `[u]int_leastN_t`, and `[u]int_fastN_t`. – pmg Jul 28 '21 at 15:43
1

How is a long double same size as a double?

Short and incomplete answer - because most current hardware only1 supports up to 64 bit words, which is the minimum necessary to meet the range and precision requirements for double. Once companies start regularly putting out hardware with larger word sizes, then you'll likely see larger sizes for long double.


  1. "Only" - sheesh. I remember when 32-bit machines were considered a big deal.
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • So is it safe to say that most (if not all) new hardware supports up to 64 bits? Also wouldn't that make a double as precise as a long double? Or is it that precision has nothing to do with byte size? – Mahmoud Gabr Jul 28 '21 at 16:42
  • @MahmoudGabr: Well...it's possible to have data types larger than the native word size - `double` was around long before 64-bit machines were common, and most systems have separate instruction sets for dealing with floating point. IINM, there are floating-point units that have 80-bit word sizes for doing intermediate calculations, although the results will be saved to a 64-bit type. But yeah, as of today `double` and `long double` have the same range and precision on most systems; that may change in the future, though. `int` and `long` have the same range and size on most systems, too. – John Bode Jul 28 '21 at 16:59
  • @MahmoudGabr: Having said all that, two different floating point types could absolutely have different ranges and precisions, even if they're the same size. One type could devote more bits to the exponent, giving it a wider range but less precision; the other could devote more bits to the significand, giving it greater precision but a narrower range. The C language definition requires `long double` to have *at least* the same range and precision as `double`, but implementations are allowed to make it have greater range and/or precision. – John Bode Jul 28 '21 at 17:04