Looking for the next IEEE 754 represented number (in the -INF direction) with C?

I am trying to write a function that takes a 32-bit floating point number (which was converted from a 32-bit binary string) and returns the previous float represented in a 32-bit binary. So far I have the conversion from the binary to shift, but I am having trouble understanding how to find the next representable value of IEEE 754. Can't you just subtract the smallest representable value (000 0000 0000 0000 0000 0001)? Also, what (if any) are the benefits of converting from IEEE 754 to Float before finding the closest binary value to represent?

So far I have a function that converts a floating point number to 32-bit binary information with simple precision. I would include my code, but this is for school, so I feel that I am sure that I will be online / receive explicit corrections and tips.

+4
source share
1 answer

Q: Can't you just subtract the smallest representable value?
A: No. Floating point numbers are distributed logarithmically, not linearly. Subtracting any fixed value, such as 0.000001, will not affect the large floatand will have excessively large effects for tiny values float.

Q: ... IEEE 754 Float ?
A: "IEEE 754" "Float", , - . 32- .

float IEEE 754 binary32. int32_t float. NaN, -INF.

float nextdown(float x) {
  union {
    float x;
    int32_t i;
  } u;
  u.x = x;
  if (u.i > 0) {
    u.i--;
  }
  else if (u.i < 0) {
    u.i++;
  }
  else {
    u.i = 0x80000001;
  }
  return u.x;
}

NaN. :

float nextdown(float x) {
  // catch NaN
  if (x != x) return x;

  union {
    float x;
    int32_t i;
  } u;
  ...

. OP , <math.h> nextafterf(x,-1.0f/0.0f), . NaN -INF.

+4

All Articles