0

Float max/min is

179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368

Compiling to assembly I see the literal is 0xffefffffffffffff. I am unable to understand how to write it in a float literal form. I tried -0xFFFFFFFFFFFFFp972 which resulted in 0xFFEFFFFFFFFFFFFE. Notice the last digit is E instead of F. I have no idea why the last bit is wrong or why 972 gave me the closest number. I didn't understand what I should be doing with the exponent bias either. I used 13 F's because that would set 52bits (the amount of bits in the mantissa) but everything else I'm clueless on

I want to be able to write double min/max as a literal and be able to understand it enough so I can parse it into a 8byte hex value

Ned
  • 33
  • 4
  • Include `float.h` and use the `FLT_MAX` macro to obtain the maximum value of `float` the machine can hold. `DBL_MAX` for `double`. – Rohan Bari Nov 22 '22 at 02:22
  • 1
    Re “Compiling to assembly I see the literal is 0xffefffffffffffff”: What you saw in assembly was probably something like `.long 0xffefffffffffffff`. That is not a floating-point literal. It is an integer literal. The compiler encoded the floating-point value into the IEEE-754 binary64 format and then used an assembly data definition statement to have that encoding put into memory. That integer is a representation of the bits that encode the floating-point literal; it is not the floating-point value and is not a floating-point literal. – Eric Postpischil Nov 22 '22 at 03:14

1 Answers1

3

How do I write float max as a float literal?

Use FLT_MAX. If making your own code, use exponential notation either as hex (preferred) or decimal. If in decimal, use FLT_DECIMAL_DIG significant digits. Any more is not informative. Append an f.

#include <float.h>
#include <stdio.h>

int main(void) {
  printf("%a\n", FLT_MAX);
  printf("%.*g\n", FLT_DECIMAL_DIG, FLT_MAX);
  float m0 = FLT_MAX;
  float m1 = 0x1.fffffep+127f;
  float m2 = 3.40282347e+38f;
  printf("%d %d\n", m1 == m0, m2 == m0);
}

Sample output

0x1.fffffep+127
3.40282347e+38
1 1

Likewise for double, yet no f.

printf("%a\n", DBL_MAX);
printf("%.*g\n", DBL_DECIMAL_DIG, DBL_MAX);

0x1.fffffffffffffp+1023
1.7976931348623157e+308

double m0 = FLT_MAX;
double m1 = 0x1.fffffffffffffp+1023;
double m2 = 1.7976931348623157e+308;

Rare machines will have different max values.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    Great. I had no idea about %a. It should be much easier to figure out how to parse the literal to the hex value – Ned Nov 22 '22 at 02:42