Calculating floating point data type ranges

Is it possible to calculate ranges of floating-point numbers, double and long double data in a portable way without reading float.h and using ANSI C? In the portable, I mean those cases where the target machine does not adhere to the IEEE 754 standard.

I am reading the K & R book and Exercise 2-1 asks me to “calculate” them, so I assume that it means to completely eliminate float.h, which includes FLT_MIN, FLT_MAX, DBL_MIN and DBL_MAX (reading these values ​​will not directly classify as a "calculation").

+5
source share
4 answers

Due to the risk of an unnecessary response:

. . <float.h> - , .

+3

( IEEE 754 float double) ():

~(-1.0) | 0.5

, , . :

uint64_t m_one, half;
double max;

*(double *)(void *)&m_one = -1.0;
*(double *)(void *)&half = 0.5;
*(uint64_t *)(void *)&max = ~m_one | half;

, ? , .

, k , . 2 0.

() 2**(k-1) - 1, , 0 , .

:

  • , 0, ;
  • , infinity, NaN

, , , 2**k - 2 2**(k-1) - 1, .

double k = 11, .. 1023, 2**1023, 1E+308.

  • , 0
  • , , 1
  • , 1

, :

  • -1.0 , - - , - 0
  • ~(-1.0) ,
  • 0.5 0; 1, .. , .

, -, .


80- x86 (aka long double), -twiddling , , 32- .

2**(k-1) - 1 - , . , 1.0 0.5 , .

base b (aka radix) 2, b**(-1) 0.5 = 2**(-1).

reserverd, 1.0 0.5. ( , ). 1.0 , .


:

~(-1.0) | 0.5

, 2, .

~(-1.0) | 1.0

, .

+11

For 99.99% of all applications, you must accept IEEE 754 and use the constants defined in <float.h>. In the other 0.01%, you will work with very specialized equipment, in which case you should know what to use based on the hardware.

+4
source

You can try to make the float bigger until it overflows.

+2
source

All Articles