5

Max float is defied as:

math.h

#define    MAXFLOAT    0x1.fffffep+127f

I'm a little sad I never noticed this before. What's this actually say? I would have expected something like this:

#define    MAXFLOAT    0xFFFFFFFF-1

Would that even work?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
slf
  • 22,595
  • 11
  • 77
  • 101
  • related: [hexadecimal floating constant in C](http://stackoverflow.com/questions/4825824/hexadecimal-floating-constant-in-c) – sidyll Dec 07 '11 at 18:03
  • 1
    Is that specific to Objective-C? And is it `MAX_FLOAT` or `MAXFLOAT`? C has `FLT_MAX` in ``; since Objective-C is so strongly backwards compatible with C, I'd expect it to just use that. – Keith Thompson Dec 07 '11 at 18:24
  • 1
    `0xFFFFFFFF-1` is an integer expression, so it wouldn't work for `MAX(_)FLOAT` -- nor would it have the right value. – Keith Thompson Dec 07 '11 at 18:27
  • @KeithThompson: it's not specific to Objective-C; it's required for UNIX conformance. – Stephen Canon Dec 07 '11 at 19:05
  • @StephenCanon: You're right. It's spelled `MAXFLOAT`, and POSIX says it's obsolescent. Use `FLT_MAX` in `` instead. – Keith Thompson Dec 07 '11 at 19:22

2 Answers2

8

0x1.fffffep+127 is (roughly) 1.99999999999999999999998 times 2^127. It's a floating point number, with an exponent, in hexadecimal.

  • 0x = hex notation
  • 1 = integer part of the number
  • .fffffe = fractional part of the number
  • p+127 = scientific notation for "times two to the 127th power"
Scott Forbes
  • 7,397
  • 1
  • 26
  • 39
  • besides `p` and `e` are there more scientific notation syntax? – slf Dec 19 '11 at 18:09
  • [The ISO standard for the C programming language](http://www.open-std.org/JTC1/SC22/WG14/) only uses `p` and `e` – other languages may vary, but most of the popular languages (Perl, Python, Java, anything derived from C) use at least the `e` syntax. – Scott Forbes Dec 19 '11 at 19:32
  • So then what signifies `e` in this instance from being part of the number, instead of `p`? Is the literal only allowed before the `+`? – slf Dec 20 '11 at 13:46
  • I'm guessing it's because it's hex, so you can't use E, you have to go higher, the `p` literal is simply something outside A-F? – slf Dec 20 '11 at 13:50
  • 1
    The difference is that `e` is a power-of-ten exponent ("times _ten_ to the nth power"), and `p` is a power-of-two exponent ("times _two_ to the nth power"); syntax-wise you can't put a hexadecimal number with a decimal exponent (or vice versa), and you generally wouldn't want to. – Scott Forbes Dec 20 '11 at 17:47
5

MAXFLOAT is required for UNIX conformance:

MAXFLOAT

[XSI] Value of maximum non-infinite single-precision floating-point number.

0x1.fffffep+127f is precisely that value, represented as a standard C hexadecimal floating-point literal.

The C standard requires that FLT_MAX be defined in <float.h>, and it has the same value ("maximum representable finite floating-point number", per §5.2.4.2.2). FLT_MAX is the more portable choice, as it is required by the language standard.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269