1FENV(3)                    Linux Programmer's Manual                   FENV(3)
2
3
4

NAME

6       feclearexcept,  fegetexceptflag, feraiseexcept, fesetexceptflag, fetes‐
7       texcept,  fegetenv,  fegetround,  feholdexcept,  fesetround,  fesetenv,
8       feupdateenv - C99 floating point rounding and exception handling
9

SYNOPSIS

11       #include <fenv.h>
12
13       int feclearexcept(int excepts);
14       int fegetexceptflag(fexcept_t *flagp, int excepts);
15       int feraiseexcept(int excepts);
16       int fesetexceptflag(const fexcept_t *flagp, int excepts);
17       int fetestexcept(int excepts);
18
19       int fegetround(void);
20       int fesetround(int rounding_mode);
21
22       int fegetenv(fenv_t *envp);
23       int feholdexcept(fenv_t *envp);
24       int fesetenv(const fenv_t *envp);
25       int feupdateenv(const fenv_t *envp);
26

DESCRIPTION

28       These  eleven  functions were defined in C99, and describe the handling
29       of floating point rounding and exceptions (overflow, zero-divide etc.).
30
31   Exceptions
32       The DivideByZero exception occurs when an operation on  finite  numbers
33       produces infinity as exact answer.
34
35       The  Overflow exception occurs when a result has to be represented as a
36       floating point number, but has (much) larger absolute  value  than  the
37       largest (finite) floating point number that is representable.
38
39       The Underflow exception occurs when a result has to be represented as a
40       floating point number, but has smaller absolute value than the smallest
41       positive normalized floating point number (and would lose much accuracy
42       when represented as a denormalized number).
43
44       The Inexact exception occurs when the rounded result of an operation is
45       not  equal  to  the  infinite  precision result.  It may occur whenever
46       Overflow or Underflow occurs.
47
48       The Invalid exception occurs when there is no well-defined  result  for
49       an operation, as for 0/0 or infinity - infinity or sqrt(-1).
50
51   Exception handling
52       Exceptions  are  represented  in  two  ways: as a single bit (exception
53       present/absent), and these  bits  correspond  in  some  implementation-
54       defined  way  with  bit  positions in an integer, and also as an opaque
55       structure that may contain more information about the  exception  (per‐
56       haps the code address where it occurred).
57
58       Each  of  the macros FE_DIVBYZERO, FE_INEXACT, FE_INVALID, FE_OVERFLOW,
59       FE_UNDERFLOW is defined when the implementation  supports  handling  of
60       the  corresponding  exception, and if so then defines the corresponding
61       bit(s), so that one can call exception handling  functions  e.g.  using
62       the integer argument FE_OVERFLOW|FE_UNDERFLOW.  Other exceptions may be
63       supported. The macro FE_ALL_EXCEPT is the bitwise OR of all bits corre‐
64       sponding to supported exceptions.
65
66       The  feclearexcept()  function  clears  the supported exceptions repre‐
67       sented by the bits in its argument.
68
69       The fegetexceptflag() function stores a representation of the state  of
70       the  exception  flags represented by the argument excepts in the opaque
71       object *flagp.
72
73       The feraiseexcept() function raises  the  supported  exceptions  repre‐
74       sented by the bits in excepts.
75
76       The  fesetexceptflag() function sets the complete status for the excep‐
77       tions represented by excepts to the value *flagp.  This value must have
78       been obtained by an earlier call of fegetexceptflag() with a last argu‐
79       ment that contained all bits in excepts.
80
81       The fetestexcept() function returns a word in which the  bits  are  set
82       that  were  set in the argument excepts and for which the corresponding
83       exception is currently set.
84
85   Rounding
86       Each of the macros FE_DOWNWARD, FE_TONEAREST, FE_TOWARDZERO,  FE_UPWARD
87       is  defined  when  the  implementation supports getting and setting the
88       corresponding rounding direction.
89
90       The fegetround() function returns the macro corresponding to  the  cur‐
91       rent rounding mode.
92
93       The  fesetround()  function  sets the rounding mode as specified by its
94       argument and returns zero when it was successful.
95
96   Floating point environment
97       The entire floating point environment, including control modes and sta‐
98       tus  flags,  can  be handled as one opaque object, of type fenv_t.  The
99       default environment is denoted by FE_DFL_ENV (of type const fenv_t  *).
100       This is the environment setup at program start and it is defined by ISO
101       C to have round to nearest, all exceptions cleared and a non-stop (con‐
102       tinue on exceptions) mode.
103
104       The fegetenv() function saves the current floating point environment in
105       the object *envp.
106
107       The feholdexcept() function does the same, then  clears  all  exception
108       flags, and sets a non-stop (continue on exceptions) mode, if available.
109       It returns zero when successful.
110
111       The fesetenv() function restores the floating  point  environment  from
112       the  object  *envp.   This  object must be known to be valid, e.g., the
113       result  of  a  call  to  fegetenv()  or  feholdexcept()  or  equal   to
114       FE_DFL_ENV.  This call does not raise exceptions.
115
116       The feupdateenv() function installs the floating-point environment rep‐
117       resented by the object *envp, except that currently  raised  exceptions
118       are  not  cleared.   After calling this function, the raised exceptions
119       will be a bitwise OR of those previously set with those in  *envp.   As
120       before, the object *envp must be known to be valid.
121

RETURN VALUE

123       These  functions  return  zero  on  success  and  non-zero  if an error
124       occurred.
125

GNU DETAILS

127       If possible, the GNU C Library defines a macro FE_NOMASK_ENV which rep‐
128       resents  an  environment  where every exception raised causes a trap to
129       occur.  You can test for this macro using #ifdef.  It is  only  defined
130       if  _GNU_SOURCE  is defined.  The C99 standard does not define a way to
131       set individual bits in the floating point mask, e.g. to  trap  on  spe‐
132       cific  flags.   glibc  2.2  supports the functions feenableexcept() and
133       fedisableexcept() to set individual floating point traps, and  fegetex‐
134       cept() to query the state.
135
136       #define _GNU_SOURCE
137       #include <fenv.h>
138
139       int feenableexcept (int excepts);
140       int fedisableexcept (int excepts);
141       int fegetexcept (void);
142
143       The  feenableexcept()  and fedisableexcept() functions enable (disable)
144       traps for each of the exceptions represented by excepts and return  the
145       previous  set  of enabled exceptions when successful, and -1 otherwise.
146       The fegetexcept() function returns the set  of  all  currently  enabled
147       exceptions.
148

NOTES

150       Link with -lm.
151

CONFORMING TO

153       IEC 60559 (IEC 559:1989), ANSI/IEEE 854, C99.
154

SEE ALSO

156       feature_test_macros(7)
157
158
159
160Linux Manpage                     2000-08-12                           FENV(3)
Impressum