MODF(3P)

🌸 MODF(3P) β€” the POSIX Programmer's Manual page, bestie edition 🌸

okay so before we even start, hi, hello, welcome to the POSIX Programmer's Manual πŸ’– this page lives in the 3P section, and it's about modf, modff and modfl. grab a snack, we're doing floating-point decomposition and it's actually kind of iconic.


🚨 PROLOG 🚨

okay so real talk first: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ β€” so if you're on Linux, pls pls pls go consult the corresponding Linux manual page for the tea on Linux behavior 🫢. also possible: the interface just isn't implemented on Linux at all. not me warning you before you even get to the fun part πŸ’€.


✨ NAME ✨

modf, modff, modfl β€” decompose a floating-point number πŸ¦‹

like. it splits a float into its two vibes. that's the whole job. slay.


🌈 SYNOPSIS 🌈

#include <math.h>

double modf(double x, double *iptr);
float modff(float value, float *iptr);
long double modfl(long double value, long double *iptr);

yes bestie, three of them, one for each flavour of float. they're a set. they come as a trio. periodt πŸ‘‘


πŸ’… DESCRIPTION πŸ’…

okay so first thing's first: the functionality described on this reference page is aligned with the ISO C standard. if there's any conflict between the requirements described here and the ISO C standard, that conflict is unintentional β€” nobody meant it, it's not a beef, it's a typo situation. and this volume of POSIX.1‐2017 defers to the ISO C standard. so like, ISO C is the main character here and we're just supporting cast ✨.

now the actual function, which is lowkey elegant:

these functions shall break the argument x into integral and fractional parts, and here's the cute part β€” each of which has the same sign as the argument. so if you hand it a negative, both halves come back negative. no sign-flipping jumpscares. we love consistency.

then it stores the integral part in the object pointed to by iptr, and the type of that integral part depends on which sibling you called:

  • for the modf() function, it's stored as a double
  • for the modff() function, it's stored as a float
  • for the modfl() function, it's stored as a long double

so the fractional part comes back as the return value, and the integral part gets yeeted into *iptr. two-for-one. it's giving efficiency πŸŽ€.


🫢 RETURN VALUE 🫢

upon successful completion, these functions shall return the signed fractional part of x. signed!! remember the sign thing. it matters.

and now the special cases, because floats always have special cases, they can never just be normal:

  • if x is NaN, a NaN shall be returned, and *iptr shall be set to a NaN. so like, both slots get NaN'd. no escape 😭
  • if x is Β±Inf, Β±0 shall be returned, and *iptr shall be set to Β±Inf. so the infinity part goes into the integral slot where it belongs (it IS the integral part, fr fr) and you get a signed zero back as the fraction. infinity has no fractional bits, it said "not today" πŸ’€

😭 ERRORS 😭

no errors are defined. none. zero. she doesn't do errors. she's unbothered, moisturized, in her lane, focused, flourishing ✨.

heads up tho: the following sections are informative. they're like, lore. background. not requirements. iykyk.

πŸ“ EXAMPLES πŸ“

None.

literally none. there are no examples. we're rawdogging this documentation. okay!! πŸ™ˆ


🧚 APPLICATION USAGE 🧚

here's the cute little relationship the modf() function maintains β€” it computes the function result and *iptr such that:

a = modf(x, iptr) ;
x == a+*iptr ;

see how it all adds back up?? the two pieces reassemble into the original. it's giving "we complete each other" πŸ₯Ή

…but, and this is important, that's allowing for the usual floating-point inaccuracies. so like. approximately. don't come for me with a bit-for-bit comparison, bestie, floats are famously messy 🌸.


πŸ‘‘ RATIONALE πŸ‘‘

None.

no rationale. no justification. it simply is. iconic behavior honestly πŸ’œ


⭐ FUTURE DIRECTIONS ⭐

None.

the future is undefined but this function is not changing. she's perfect as she is πŸ’…


πŸ”₯ SEE ALSO πŸ”₯

  • frexp()
  • isnan()
  • ldexp()

and also, from the Base Definitions volume of POSIX.1‐2017: <math.h> πŸ“š

go read those next, bestie, they're the friend group.


okay so legally I have to say this exactly and I'm gonna say it with love:

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. 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 .


and that's the whole page!! πŸ’– this one came to you courtesy of IEEE/The Open Group, 2017, and yes the footer really does just say MODF(3P) at the bottom like a little signature. mwah. go decompose something. you got this πŸ¦‹βœ¨

πŸ“¦ where this page comes from