Section (3) fenv
Name
feclearexcept, fegetexceptflag, feraiseexcept, fesetexceptflag, fetestexcept, fegetenv, fegetround, feholdexcept, fesetround, fesetenv, feupdateenv, feenableexcept, fedisableexcept, fegetexcept — floatingpoint rounding and exception handling
Synopsis
#include <fenv.h>
int
feclearexcept( 
int excepts) ; 
int
fegetexceptflag( 
fexcept_t *flagp, 
int excepts) ; 
int
feraiseexcept( 
int excepts) ; 
int
fesetexceptflag( 
const fexcept_t *flagp, 
int excepts) ; 
int
fetestexcept( 
int excepts) ; 
int
fegetround( 
void) ; 
int
fesetround( 
int rounding_mode) ; 
int
fegetenv( 
fenv_t *envp) ; 
int
feholdexcept( 
fenv_t *envp) ; 
int
fesetenv( 
const fenv_t *envp) ; 
int
feupdateenv( 
const fenv_t *envp) ; 
Note  

Link with 
DESCRIPTION
These eleven functions were defined in C99, and describe the handling of floatingpoint rounding and exceptions (overflow, zerodivide, etc.).
Exceptions
The dividebyzero
exception
occurs when an operation on finite numbers produces
infinity as exact answer.
The overflow
exception
occurs when a result has to be represented as a
floatingpoint number, but has (much) larger absolute value
than the largest (finite) floatingpoint number that is
representable.
The underflow
exception
occurs when a result has to be represented as a
floatingpoint number, but has smaller absolute value than
the smallest positive normalized floatingpoint number (and
would lose much accuracy when represented as a denormalized
number).
The inexact
exception occurs when the rounded result of an operation is
not equal to the infinite precision result. It may occur
whenever overflow
or
underflow
occurs.
The invalid
exception
occurs when there is no welldefined result for an
operation, as for 0/0 or infinity − infinity or
sqrt(−1).
Exception handling
Exceptions are represented in two ways: as a single bit (exception present/absent), and these bits correspond in some implementationdefined way with bit positions in an integer, and also as an opaque structure that may contain more information about the exception (perhaps the code address where it occurred).
Each of the macros FE_DIVBYZERO
, FE_INEXACT
, FE_INVALID
, FE_OVERFLOW
, FE_UNDERFLOW
is defined when the
implementation supports handling of the corresponding
exception, and if so then defines the corresponding bit(s),
so that one can call exception handling functions, for
example, using the integer argument FE_OVERFLOW
FE_UNDERFLOW
. Other exceptions may be
supported. The macro FE_ALL_EXCEPT
is the bitwise OR of all
bits corresponding to supported exceptions.
The feclearexcept
()
function clears the supported exceptions represented by the
bits in its argument.
The fegetexceptflag
()
function stores a representation of the state of the
exception flags represented by the argument excepts
in the opaque object
*flagp
.
The feraiseexcept
()
function raises the supported exceptions represented by the
bits in excepts
.
The fesetexceptflag
()
function sets the complete status for the exceptions
represented by excepts
to the value
*flagp
. This value
must have been obtained by an earlier call of fegetexceptflag
() with a last argument
that contained all bits in excepts
.
The fetestexcept
()
function returns a word in which the bits are set that were
set in the argument excepts
and for which the
corresponding exception is currently set.
Rounding mode
The rounding mode determines how the result of floatingpoint operations is treated when the result cannot be exactly represented in the significand. Various rounding modes may be provided: round to nearest (the default), round up (toward positive infinity), round down (toward negative infinity), and round toward zero.
Each of the macros FE_TONEAREST
, FE_UPWARD
, FE_DOWNWARD
, and FE_TOWARDZERO
is defined when the
implementation supports getting and setting the
corresponding rounding direction.
The fegetround
() function
returns the macro corresponding to the current rounding
mode.
The fesetround
() function
sets the rounding mode as specified by its argument and
returns zero when it was successful.
C99 and POSIX.12008 specify an identifier, FLT_ROUNDS
, defined in <
float.h
>
which indicates the implementationdefined rounding
behavior for floatingpoint addition. This identifier has
one of the following values:
 −1

The rounding mode is not determinable.
0

Rounding is toward 0.
1

Rounding is toward nearest number.
2

Rounding is toward positive infinity.
3

Rounding is toward negative infinity.
Other values represent machinedependent, nonstandard rounding modes.
The value of FLT_ROUNDS
should reflect the current rounding mode as set by
fesetround
() (but see
BUGS).
Floatingpoint environment
The entire floatingpoint environment, including control
modes and status flags, can be handled as one opaque
object, of type fenv_t. The
default environment is denoted by FE_DFL_ENV
(of type const fenv_t *). This is the environment
setup at program start and it is defined by ISO C to have
round to nearest, all exceptions cleared and a nonstop
(continue on exceptions) mode.
The fegetenv
() function
saves the current floatingpoint environment in the object
*envp
.
The feholdexcept
()
function does the same, then clears all exception flags,
and sets a nonstop (continue on exceptions) mode, if
available. It returns zero when successful.
The fesetenv
() function
restores the floatingpoint environment from the object
*envp
. This object
must be known to be valid, for example, the result of a
call to fegetenv
() or
feholdexcept
() or equal to
FE_DFL_ENV
. This call does
not raise exceptions.
The feupdateenv
() function
installs the floatingpoint environment represented by the
object *envp
,
except that currently raised exceptions are not cleared.
After calling this function, the raised exceptions will be
a bitwise OR of those previously set with those in
*envp
. As before,
the object *envp
must be known to be valid.
ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).
Interface  Attribute  Value 
feclearexcept (), fegetexceptflag (), feraiseexcept (), fesetexceptflag (), fetestexcept (), fegetround (), fesetround (), fegetenv (), feholdexcept (), fesetenv (), feupdateenv (), feenableexcept (), fedisableexcept (), fegetexcept () 
Thread safety  MTSafe 
NOTES
Glibc notes
If possible, the GNU C Library defines a macro
FE_NOMASK_ENV
which
represents an environment where every exception raised
causes a trap to occur. You can test for this macro using
#ifdef
. It is defined only if
_GNU_SOURCE
is defined. The
C99 standard does not define a way to set individual bits
in the floatingpoint mask, for example, to trap on
specific flags. Since version 2.2, glibc supports the
functions feenableexcept
()
and fedisableexcept
() to set
individual floatingpoint traps, and fegetexcept
() to query the state.
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <fenv.h>int feenableexcept
(int excepts
);int fedisableexcept
(int excepts
); int fegetexcept(void);
The feenableexcept
() and
fedisableexcept
() functions
enable (disable) traps for each of the exceptions
represented by excepts
and return the
previous set of enabled exceptions when successful, and
−1 otherwise. The fegetexcept
() function returns the set of
all currently enabled exceptions.
BUGS
C99 specifies that the value of FLT_ROUNDS
should reflect changes to the
current rounding mode, as set by fesetround
(). Currently, this does not
occur: FLT_ROUNDS
always has
the value 1.
COLOPHON
This page is part of release 4.16 of the Linux manpages
project. A
description of the project, information about reporting bugs,
and the latest version of this page, can be found at
https://www.kernel.org/doc/man−pages/.
Copyright (c) 2000 Andries Brouwer (aebcwi.nl) %%%LICENSE_START(GPLv2+_DOC_FULL) This is free documentation; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The GNU General Public License_zsingle_quotesz_s references to object code and executables are to be interpreted as the output of any document formatting or typesetting system, including intermediate and printed output. This manual is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this manual; if not, see <http://www.gnu.org/licenses/>. %%%LICENSE_END 20000814 added GNU additions from Andreas Jaeger 20001205 some changes inspired by acahalan_zsingle_quotesz_s remarks 