fenv.3 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. .\" Copyright (c) 2004 David Schultz <[email protected]>
  2. .\" All rights reserved.
  3. .\"
  4. .\" Redistribution and use in source and binary forms, with or without
  5. .\" modification, are permitted provided that the following conditions
  6. .\" are met:
  7. .\" 1. Redistributions of source code must retain the above copyright
  8. .\" notice, this list of conditions and the following disclaimer.
  9. .\" 2. Redistributions in binary form must reproduce the above copyright
  10. .\" notice, this list of conditions and the following disclaimer in the
  11. .\" documentation and/or other materials provided with the distribution.
  12. .\"
  13. .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. .\" SUCH DAMAGE.
  24. .\"
  25. .\" $FreeBSD: src/lib/msun/man/fenv.3,v 1.8 2011/10/21 14:23:59 das Exp $
  26. .\"
  27. .Dd March 16, 2005
  28. .Dt FENV 3
  29. .Os
  30. .Sh NAME
  31. .Nm feclearexcept ,
  32. .Nm fegetexceptflag ,
  33. .Nm feraiseexcept ,
  34. .Nm fesetexceptflag ,
  35. .Nm fetestexcept ,
  36. .Nm fegetround ,
  37. .Nm fesetround ,
  38. .Nm fegetenv ,
  39. .Nm feholdexcept ,
  40. .Nm fesetenv ,
  41. .Nm feupdateenv ,
  42. .Nm feenableexcept ,
  43. .Nm fedisableexcept ,
  44. .Nm fegetexcept
  45. .Nd floating-point environment control
  46. .Sh LIBRARY
  47. .Lb libm
  48. .Sh SYNOPSIS
  49. .In fenv.h
  50. .Fd "#pragma STDC FENV_ACCESS ON"
  51. .Ft int
  52. .Fn feclearexcept "int excepts"
  53. .Ft int
  54. .Fn fegetexceptflag "fexcept_t *flagp" "int excepts"
  55. .Ft int
  56. .Fn feraiseexcept "int excepts"
  57. .Ft int
  58. .Fn fesetexceptflag "const fexcept_t *flagp" "int excepts"
  59. .Ft int
  60. .Fn fetestexcept "int excepts"
  61. .Ft int
  62. .Fn fegetround void
  63. .Ft int
  64. .Fn fesetround "int round"
  65. .Ft int
  66. .Fn fegetenv "fenv_t *envp"
  67. .Ft int
  68. .Fn feholdexcept "fenv_t *envp"
  69. .Ft int
  70. .Fn fesetenv "const fenv_t *envp"
  71. .Ft int
  72. .Fn feupdateenv "const fenv_t *envp"
  73. .Ft int
  74. .Fn feenableexcept "int excepts"
  75. .Ft int
  76. .Fn fedisableexcept "int excepts"
  77. .Ft int
  78. .Fn fegetexcept void
  79. .Sh DESCRIPTION
  80. The
  81. .In fenv.h
  82. routines manipulate the floating-point environment,
  83. which includes the exception flags and rounding modes defined in
  84. .St -ieee754 .
  85. .Ss Exceptions
  86. Exception flags are set as side-effects of floating-point arithmetic
  87. operations and math library routines, and they remain set until
  88. explicitly cleared.
  89. The following macros expand to bit flags of type
  90. .Vt int
  91. representing the five standard floating-point exceptions.
  92. .Bl -tag -width ".Dv FE_DIVBYZERO"
  93. .It Dv FE_DIVBYZERO
  94. A divide-by-zero exception occurs when the
  95. .Em exact
  96. result of a computation is infinite (according to the limit definition).
  97. For example, dividing a finite non-zero number by zero or computing
  98. .Fn log 0
  99. raises a divide-by-zero exception.
  100. .It Dv FE_INEXACT
  101. An inexact exception is raised whenever there is a loss of accuracy
  102. due to rounding.
  103. .It Dv FE_INVALID
  104. Invalid operation exceptions occur when a program attempts to
  105. perform calculations for which there is no reasonable representable
  106. answer.
  107. For instance, subtraction of like-signed infinities, division of zero by zero,
  108. ordered comparison involving \*(Nas, and taking the real square root of a
  109. negative number are all invalid operations.
  110. .It Dv FE_OVERFLOW
  111. In contrast with divide-by-zero,
  112. an overflow exception occurs when an infinity is produced because
  113. the magnitude of the exact result is
  114. .Em finite
  115. but too large to fit in the destination type.
  116. For example, computing
  117. .Li DBL_MAX * 2
  118. raises an overflow exception.
  119. .It Dv FE_UNDERFLOW
  120. Underflow occurs when the result of a computation loses precision
  121. because it is too close to zero.
  122. The result is a subnormal number or zero.
  123. .El
  124. .Pp
  125. Additionally, the
  126. .Dv FE_ALL_EXCEPT
  127. macro expands to the bitwise OR of the above flags and any
  128. architecture-specific flags.
  129. Combinations of these flags are passed to the
  130. .Fn feclearexcept ,
  131. .Fn fegetexceptflag ,
  132. .Fn feraiseexcept ,
  133. .Fn fesetexceptflag ,
  134. and
  135. .Fn fetestexcept
  136. functions to clear, save, raise, restore, and examine the
  137. processor's floating-point exception flags, respectively.
  138. .Pp
  139. Exceptions may be
  140. .Em unmasked
  141. with
  142. .Fn feenableexcept
  143. and masked with
  144. .Fn fedisableexcept .
  145. Unmasked exceptions cause a trap when they are produced, and
  146. all exceptions are masked by default.
  147. The current mask can be tested with
  148. .Fn fegetexcept .
  149. .Ss Rounding Modes
  150. .St -ieee754
  151. specifies four rounding modes.
  152. These modes control the direction in which results are rounded
  153. from their exact values in order to fit them into binary
  154. floating-point variables.
  155. The four modes correspond with the following symbolic constants.
  156. .Bl -tag -width ".Dv FE_TOWARDZERO"
  157. .It Dv FE_TONEAREST
  158. Results are rounded to the closest representable value.
  159. If the exact result is exactly half way between two representable
  160. values, the value whose last binary digit is even (zero) is chosen.
  161. This is the default mode.
  162. .It Dv FE_DOWNWARD
  163. Results are rounded towards negative \*[If].
  164. .It Dv FE_UPWARD
  165. Results are rounded towards positive \*[If].
  166. .It Dv FE_TOWARDZERO
  167. Results are rounded towards zero.
  168. .El
  169. .Pp
  170. The
  171. .Fn fegetround
  172. and
  173. .Fn fesetround
  174. functions query and set the rounding mode.
  175. .Ss Environment Control
  176. The
  177. .Fn fegetenv
  178. and
  179. .Fn fesetenv
  180. functions save and restore the floating-point environment,
  181. which includes exception flags, the current exception mask,
  182. the rounding mode, and possibly other implementation-specific
  183. state.
  184. The
  185. .Fn feholdexcept
  186. function behaves like
  187. .Fn fegetenv ,
  188. but with the additional effect of clearing the exception flags and
  189. installing a
  190. .Em non-stop
  191. mode.
  192. In non-stop mode, floating-point operations will set exception flags
  193. as usual, but no
  194. .Dv SIGFPE
  195. signals will be generated as a result.
  196. Non-stop mode is the default, but it may be altered by
  197. .Fn feenableexcept
  198. and
  199. .Fn fedisableexcept .
  200. The
  201. .Fn feupdateenv
  202. function restores a saved environment similarly to
  203. .Fn fesetenv ,
  204. but it also re-raises any floating-point exceptions from the old
  205. environment.
  206. .Pp
  207. The macro
  208. .Dv FE_DFL_ENV
  209. expands to a pointer to the default environment.
  210. .Sh EXAMPLES
  211. The following routine computes the square root function.
  212. It explicitly raises an invalid exception on appropriate inputs using
  213. .Fn feraiseexcept .
  214. It also defers inexact exceptions while it computes intermediate
  215. values, and then it allows an inexact exception to be raised only if
  216. the final answer is inexact.
  217. .Bd -literal -offset indent
  218. #pragma STDC FENV_ACCESS ON
  219. double sqrt(double n) {
  220. double x = 1.0;
  221. fenv_t env;
  222. if (isnan(n) || n < 0.0) {
  223. feraiseexcept(FE_INVALID);
  224. return (NAN);
  225. }
  226. if (isinf(n) || n == 0.0)
  227. return (n);
  228. feholdexcept(&env);
  229. while (fabs((x * x) - n) > DBL_EPSILON * 2 * x)
  230. x = (x / 2) + (n / (2 * x));
  231. if (x * x == n)
  232. feclearexcept(FE_INEXACT);
  233. feupdateenv(&env);
  234. return (x);
  235. }
  236. .Ed
  237. .Sh SEE ALSO
  238. .Xr cc 1 ,
  239. .Xr feclearexcept 3 ,
  240. .Xr fedisableexcept 3 ,
  241. .Xr feenableexcept 3 ,
  242. .Xr fegetenv 3 ,
  243. .Xr fegetexcept 3 ,
  244. .Xr fegetexceptflag 3 ,
  245. .Xr fegetround 3 ,
  246. .Xr feholdexcept 3 ,
  247. .Xr feraiseexcept 3 ,
  248. .Xr fesetenv 3 ,
  249. .Xr fesetexceptflag 3 ,
  250. .Xr fesetround 3 ,
  251. .Xr fetestexcept 3 ,
  252. .Xr feupdateenv 3 ,
  253. .Xr fpgetprec 3 ,
  254. .Xr fpsetprec 3
  255. .Sh STANDARDS
  256. Except as noted below,
  257. .In fenv.h
  258. conforms to
  259. .St -isoC-99 .
  260. The
  261. .Fn feenableexcept ,
  262. .Fn fedisableexcept ,
  263. and
  264. .Fn fegetexcept
  265. routines are extensions.
  266. .Sh HISTORY
  267. The
  268. .In fenv.h
  269. header first appeared in
  270. .Fx 5.3 .
  271. It supersedes the non-standard routines defined in
  272. .In ieeefp.h
  273. and documented in
  274. .Xr fpgetround 3 .
  275. .Sh CAVEATS
  276. The FENV_ACCESS pragma can be enabled with
  277. .Dl "#pragma STDC FENV_ACCESS ON"
  278. and disabled with the
  279. .Dl "#pragma STDC FENV_ACCESS OFF"
  280. directive.
  281. This lexically-scoped annotation tells the compiler that the program
  282. may access the floating-point environment, so optimizations that would
  283. violate strict IEEE-754 semantics are disabled.
  284. If execution reaches a block of code for which
  285. .Dv FENV_ACCESS
  286. is off, the floating-point environment will become undefined.
  287. .Sh BUGS
  288. The
  289. .Dv FENV_ACCESS
  290. pragma is unimplemented in the system compiler.
  291. However, non-constant expressions generally produce the correct
  292. side-effects at low optimization levels.