FMA(3P)
π FMA(3P) π
okay bestie, welcome to the POSIX Programmer's Manual β¨ this is the whole entire fma situation and honestly? it's giving math girlboss energy π
πΈ PROLOG πΈ
so real talk: this manual page is part of the POSIX Programmer's Manual π«Ά the Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the deets on Linux behavior), or the interface might not even be implemented on Linux at all π no cap, always double-check.
π NAME π
fma, fmaf, fmal β floating-point multiply-add β¨ like, it multiplies AND adds AND rounds, one single glow-up operation π
π¦ SYNOPSIS π¦
#include <math.h>
double fma(double x, double y, double z);
float fmaf(float x, float y, float z);
long double fmal(long double x, long double y, long double z);
three flavors, three sizes, all iconic π
π DESCRIPTION π
okay so, the functionality on this reference page is aligned with the ISO C standard π if there's any conflict between the requirements here and the ISO C standard, that conflict is unintentional β my bad, not on purpose π this volume of POSIX.1β2017 defers to the ISO C standard, periodt.
these functions shall compute (x * y) + z, rounded as one ternary operation β¨ like they compute the value (as if) to infinite precision and then round once to the result format, according to the rounding mode characterized by the value of FLT_ROUNDS πΈ
an application wishing to check for error situations should set errno to zero and call feclearexcept(FE_ALL_EXCEPT) before calling these functions π on return, if errno is non-zero or fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) is non-zero, an error has occurred. like, the vibes are off, something went wrong π
β RETURN VALUE β
upon successful completion, these functions shall return (x * y) + z, rounded as one ternary operation π«Ά slay.
if the result overflows or underflows, a range error may occur π¬ on systems that support the IEC 60559 Floating-Point option, if the result overflows a range error shall occur (so like, mandatory, no skipping).
if x or y are NaN, a NaN shall be returned π
if x multiplied by y is an exact infinity and z is also an infinity but with the opposite sign, a domain error shall occur, and either a NaN (if supported), or an implementation-defined value shall be returned π¨
if one of x and y is infinite, the other is zero, and z is not a NaN, a domain error shall occur, and either a NaN (if supported), or an implementation-defined value shall be returned π¨
if one of x and y is infinite, the other is zero, and z is a NaN, a NaN shall be returned and a domain error may occur π
if x*y is not 0*Inf nor Inf*0 and z is a NaN, a NaN shall be returned π
π ERRORS π
these functions shall fail if:
- Domain Error β the value of
x*y+zis invalid, or the valuex*yis invalid andzis not a NaN π- if the integer expression
(math_errhandling & MATH_ERRNO)is non-zero, thenerrnoshall be set to[EDOM]. if the integer expression(math_errhandling & MATH_ERREXCEPT)is non-zero, then the invalid floating-point exception shall be raised.
- if the integer expression
- Range Error β the result overflows π₯
- if the integer expression
(math_errhandling & MATH_ERRNO)is non-zero, thenerrnoshall be set to[ERANGE]. if the integer expression(math_errhandling & MATH_ERREXCEPT)is non-zero, then the overflow floating-point exception shall be raised.
- if the integer expression
these functions may fail if:
- Domain Error β the value
x*yis invalid andzis a NaN π- if the integer expression
(math_errhandling & MATH_ERRNO)is non-zero, thenerrnoshall be set to[EDOM]. if the integer expression(math_errhandling & MATH_ERREXCEPT)is non-zero, then the invalid floating-point exception shall be raised.
- if the integer expression
- Range Error β the result underflows π¬
- if the integer expression
(math_errhandling & MATH_ERRNO)is non-zero, thenerrnoshall be set to[ERANGE]. if the integer expression(math_errhandling & MATH_ERREXCEPT)is non-zero, then the underflow floating-point exception shall be raised.
- if the integer expression
- Range Error β the result overflows π₯ (yes bestie, it's listed again here too, we don't skip entries in this house)
- if the integer expression
(math_errhandling & MATH_ERRNO)is non-zero, thenerrnoshall be set to[ERANGE]. if the integer expression(math_errhandling & MATH_ERREXCEPT)is non-zero, then the overflow floating-point exception shall be raised.
- if the integer expression
the following sections are informative β like, they're the commentary track, not the requirements π§
π EXAMPLES π
None. β literally none, the examples section said "not today" π
π§ APPLICATION USAGE π§
on error, the expressions (math_errhandling & MATH_ERRNO) and (math_errhandling & MATH_ERREXCEPT) are independent of each other, but at least one of them must be non-zero β¨ like, they don't talk to each other, but one of them has to show up to the party π
π RATIONALE π
in many cases, clever use of floating (fused) multiply-add leads to much improved code π but its unexpected use by the compiler can undermine carefully written code π± (not the compiler doing a little trolling, but also, yes).
the FP_CONTRACT macro can be used to disallow use of floating multiply-add; and the fma() function guarantees its use where desired π«Ά
many current machines provide hardware floating multiply-add instructions; software implementation can be used for others π»β¨
π FUTURE DIRECTIONS π
None. β the future is bright but this section is empty π
πΈ SEE ALSO πΈ
feclearexcept(),fetestexcept()
the Base Definitions volume of POSIX.1β2017, Section 4.20, Treatment of Error Conditions for Mathematical Functions, <math.h> π
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group π in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document π (so if they disagree, the original wins, no debate). the original Standard can be obtained online at http://www.opengroup.org/unix/online.html π
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format π to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html π
okay that's the whole page bestie, you ate that π this lil' sign-off comes to you from IEEE/The Open Group, dated 2017, and the page you just read is FMA(3P) β take care of her, she's iconic β¨ππ¦
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms