Updated: May 06, 2022 |

*Compute the next representable floating-point number in a given direction*

#include <math.h> double nexttoward ( doublex, long doubley); float nexttowardf ( floatx, long doubley); long double nexttowardl ( long doublex, long doubley);

`x`- The number for which you want the next number in the specified direction.
`y`- A number that specifies the direction you want to go; see below.

- libm
- The general-purpose math library.
- libm-sve
- (QNX Neutrino 7.1 or later) A library that optimizes the code for ARMv8.2 chips that have Scalable Vector Extension hardware.

Your system requirements will determine how you should work with these libraries:

- If you want only selected processes to run with the SVE version, you can include both libraries in your OS image and use the -l m or -l m-sve option to qcc to link explicitly against the appropriate one.
- If you want all processes to use the SVE version, include libm-sve.so in your OS image and set up a symbolic link from libm.so to libm-sve.so. Use the -l m option to qcc to link against the library.

Note:
Compile your program with the -fno-builtin option to prevent the compiler from using a
built-in version of the function.

The nexttoward(), nexttowardf(), and nexttowardl() functions
compute the next representable double-precision floating-point value
following `x` in the direction of `y`.
These functions are equivalent to
nextafter(), nextafterf(), and nextafterl(), respectively,
except that:

- the second argument is of type long double
- if
`x`equals`y`, nexttoward(), nexttowardf(), and nexttowardl() return`y`converted to the type of the function.

To check for error situations, use feclearexcept() and fetestexcept(). For example:

- Call
`feclearexcept(FE_ALL_EXCEPT)`before calling nexttoward(), nexttowardf(), or nexttowardl(). - On return, if
`fetestexcept(FE_ALL_EXCEPT)`is nonzero, then an error has occurred.

The next machine floating-point number of `x` in the
direction towards `y`, converted to the type that the function returns.

If | These functions return: | Errors: |
---|---|---|

x equals y |
y |
— |

x is finite, and the correct value would overflow |
Inf | FE_OVERFLOW |

x or y is NaN |
NaN | — |

x != y, and the correct value is subnormal, zero, or underflows |
The correct value (if representable), or 0.0 | FE_UNDERFLOW |

These functions raise FE_INEXACT if the FPU reports that the result can't be exactly represented as a floating-point number.

#include <stdio.h> #include <inttypes.h> #include <math.h> #include <fenv.h> #include <stdlib.h> void dump_to_hex(double d) { printf("0x%08x %08x \n", (uint32_t)(*((uint64_t*)&d) >> 32), (uint32_t)(*((uint64_t*)&d))); } int main(int argc, char** argv) { double a, b, c; int except_flags; a = 0; feclearexcept(FE_ALL_EXCEPT); b = nexttoward(a, -1); except_flags = fetestexcept(FE_ALL_EXCEPT); if(except_flags) { /* An error occurred; handle it appropriately. */ } feclearexcept(FE_ALL_EXCEPT); c = nexttoward(a, 1); except_flags = fetestexcept(FE_ALL_EXCEPT); if(except_flags) { /* An error occurred; handle it appropriately. */ } printf("Next possible value before %f is %f \n", a, b); printf("-->"); dump_to_hex(a); printf("-->"); dump_to_hex(b); printf("Next possible value after %f is %f \n", a, c); printf("-->"); dump_to_hex(a); printf("-->"); dump_to_hex(c); return EXIT_SUCCESS; }

produces the output:

Next possible value before 0.000000 is 0.000000 -->0x00000000 00000000 -->0x80000000 00000001 Next possible value after 0.000000 is 0.000000 -->0x00000000 00000000

Safety: | |
---|---|

Cancellation point | No |

Interrupt handler | Yes |

Signal handler | Yes |

Thread | Yes |