FPGETROUND(3)
π¦ FPGETROUND(3) π¦
okay bestie, welcome to the Library Functions Manual page for the IEEE floating point interface. This is the whole fpgetround / fpsetround / fpsetprec / fpgetprec / fpgetmask / fpsetmask / fpgetsticky / fpresetsticky situation, and yes, we're gonna get through it together π
β¨ NAME β¨
This page is about fpgetround, fpsetround, fpsetprec, fpgetprec, fpgetmask, fpsetmask, fpgetsticky, and fpresetsticky β the IEEE floating point interface π
π SYNOPSIS π
First, you pull in this header:
#include <ieeefp.h>
Then the rounding-mode enum:
typedef enum {
FP_RN, /* round to nearest */
FP_RM, /* round down to minus infinity */
FP_RP, /* round up to plus infinity */
FP_RZ /* truncate */
} fp_rnd_t;
And the rounding functions:
fp_rnd_t
fpgetround(void);
fp_rnd_t
fpsetround(fp_rnd_t direction);
Next, the precision enum:
typedef enum {
FP_PS, /* 24 bit (single-precision) */
FP_PRS, /* reserved */
FP_PD, /* 53 bit (double-precision) */
FP_PE /* 64 bit (extended-precision) */
} fp_prec_t;
And the precision functions:
fp_prec_t
fpgetprec(void);
fp_prec_t
fpsetprec(fp_prec_t precision);
Then the exception-mask defines and the mask/sticky functions:
#define fp_except_t int
#define FP_X_INV 0x01 /* invalid operation */
#define FP_X_DNML 0x02 /* denormal */
#define FP_X_DZ 0x04 /* zero divide */
#define FP_X_OFL 0x08 /* overflow */
#define FP_X_UFL 0x10 /* underflow */
#define FP_X_IMP 0x20 /* (im)precision */
#define FP_X_STK 0x40 /* stack fault */
fp_except_t
fpgetmask(void);
fp_except_t
fpsetmask(fp_except_t mask);
fp_except_t
fpgetsticky(void);
fp_except_t
fpresetsticky(fp_except_t sticky);
πΈ DESCRIPTION πΈ
okay so first things first: these routines are deprecated π«Ά New code should use the functionality provided by fenv(3). Not me being dramatic, that's just the assignment.
Here's the vibe: when a floating point exception is detected, the exception sticky flag is set and the exception mask is tested. If the mask is set, then a trap occurs. These routines let you set the floating point exception masks, and they let you reset the exception sticky flags after an exception is detected. They also let you set the floating point rounding mode and precision. iconic, honestly β¨
Now the function-by-function breakdown:
fpgetround()β returns the current floating point rounding mode.fpsetround()β sets the floating point rounding mode and returns the previous mode.fpgetprec()β returns the current floating point precision.fpsetprec()β sets the floating point precision and returns the previous precision.fpgetmask()β returns the current floating point exception masks.fpsetmask()β sets the floating point exception masks and returns the previous masks.fpgetsticky()β returns the current floating point sticky flags.fpresetsticky()β clears the floating point sticky flags and returns the previous flags.
And here's sample code which prevents a trap on divide-by-zero, bestie:
fpsetmask(~FP_X_DZ);
a = 1.0;
b = 0;
c = a / b;
fpresetsticky(FP_X_DZ);
fpsetmask(FP_X_DZ);
π§ IMPLEMENTATION NOTES π§
The fpgetprec() and fpsetprec() functions provide functionality unavailable on many platforms. At present, they are implemented only on the i386 and amd64 platforms.
Changing precision is not a supported feature: it may be ineffective when code is compiled to take advantage of SSE, and many library functions and compiler optimizations depend upon the default precision for correct behavior. so yeah, tread carefully, bestie π
π SEE ALSO π
π HISTORY π
These routines are based on SysV/386 routines of the same name. vintage behavior, lowkey iconic π
π CAVEATS π
After a floating point exception and before a mask is set, the sticky flags must be reset. If another exception occurs before the sticky flags are reset, then a wrong exception type may be signaled. no cap, this one matters π
xoxo, Debian β December 3, 2010 ππ
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1993 Andrew Moore, Talke Studio
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms