101

AFAIK, C supports just a few data types:

int, float, double, char, void enum.

I need to store a number that could reach into the high 10 digits. Since I'm getting a low 10 digit # from

INT_MAX

, I suppose I need a double.

<limits.h> doesn't have a DOUBLE_MAX. I found a DBL_MAX on the internet that said this is LEGACY and also appears to be C++. Is double what I need? Why is there no DOUBLE_MAX?

Neuron
  • 5,141
  • 5
  • 38
  • 59
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • 2
    1) As others pointed out, you don't need a `double`, you need a 64-bit integral type (either `long long`, or `uint64_t`). 2) If you do use a `double`, you don't need `DBL_MAX` (which is the largest number that can be stored), you need [`DBL_DIG`](http://tigcc.ticalc.org/doc/float.html#DBL_DIG), which is the number of significant digits you can store. (It won't do you any good to be able to store 9999999999 if you can't distinguish it from 9999999998). In any event, on most systems, a `double` can precisely store all integers of the size you are asking for. – Robᵩ Apr 29 '11 at 17:12
  • 1
    On most systems, a double can precisely store all postive integers less than 9007199254740993. – Robᵩ Apr 29 '11 at 17:34
  • 1
    C has many types: `char, unsigned char, signed char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, float, double, long double, void`, enums, structs, functions, arrays, and pointers to all of these. – Mooing Duck Sep 10 '15 at 01:39

6 Answers6

115

DBL_MAX is defined in <float.h>. Its availability in <limits.h> on unix is what is marked as "(LEGACY)".

(linking to the unix standard even though you have no unix tag since that's probably where you found the "LEGACY" notation, but much of what is shown there for float.h is also in the C standard back to C89)

Random832
  • 37,415
  • 3
  • 44
  • 63
  • 8
    I suspect the "LEGACY" came from some C++ site that wants you to use their arcane std numeric limits system instead of macros because "macros are dirty" or such... – R.. GitHub STOP HELPING ICE Nov 29 '12 at 13:55
  • 2
    As I said, the only place I've seen it tagged LEGACY was in a unix standard that was talking about _its presence in limits.h_ rather than DBL_MAX as a whole – Random832 Nov 29 '12 at 17:40
64

You get the integer limits in <limits.h> or <climits>. Floating point characteristics are defined in <float.h> for C. In C++, the preferred version is usually std::numeric_limits<double>::max() (for which you #include <limits>).

As to your original question, if you want a larger integer type than long, you should probably consider long long. This isn't officially included in C++98 or C++03, but is part of C99 and C++11, so all reasonably current compilers support it.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    vs2012 complained without having the brackets i.e: std::numeric_limits::max(). did try a quick edit of the ans but the edit was too small. – QED Jul 27 '14 at 02:49
  • 1
    and for the minimum, the counterpart `min` is tricky (it is positive number for double type): https://en.cppreference.com/w/cpp/types/numeric_limits/min, so `numeric_limits::lowest()` is more predictable: https://en.cppreference.com/w/cpp/types/numeric_limits/lowest – zubko Mar 29 '19 at 16:55
11

Its in the standard float.h include file. You want DBL_MAX

Sodved
  • 8,428
  • 2
  • 31
  • 43
9

Using double to store large integers is dubious; the largest integer that can be stored reliably in double is much smaller than DBL_MAX. You should use long long, and if that's not enough, you need your own arbitrary-precision code or an existing library.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
5

You are looking for the float.h header.

Femaref
  • 60,705
  • 7
  • 138
  • 176
3

INT_MAX is just a definition in limits.h. You don't make it clear whether you need to store an integer or floating point value. If integer, and using a 64-bit compiler, use a LONG (LLONG for 32-bit).

entropo
  • 2,481
  • 15
  • 15