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+z is invalid, or the value x*y is invalid and z is not a NaN 😭
    • if the integer expression (math_errhandling & MATH_ERRNO) is non-zero, then errno shall be set to [EDOM]. if the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero, then the invalid floating-point exception shall be raised.
  • Range Error β€” the result overflows πŸ”₯
    • if the integer expression (math_errhandling & MATH_ERRNO) is non-zero, then errno shall be set to [ERANGE]. if the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero, then the overflow floating-point exception shall be raised.

these functions may fail if:

  • Domain Error β€” the value x*y is invalid and z is a NaN πŸ‘€
    • if the integer expression (math_errhandling & MATH_ERRNO) is non-zero, then errno shall be set to [EDOM]. if the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero, then the invalid floating-point exception shall be raised.
  • Range Error β€” the result underflows 😬
    • if the integer expression (math_errhandling & MATH_ERRNO) is non-zero, then errno shall be set to [ERANGE]. if the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero, then the underflow floating-point exception shall be raised.
  • 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, then errno shall be set to [ERANGE]. if the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero, then the overflow floating-point exception shall be raised.
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> πŸ“š


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