ILOGB(3POSIX)
π ILOGB(3POSIX) β the whole vibe π
okay bestie, gather round, we're doing ilogb, ilogbf, and ilogbl from the POSIX Programmer's Manual (yes, the 3POSIX one, she's iconic). this is the page where we find out what unbiased exponent energy actually means. let's go π
πΈ PROLOG πΈ
so like, real talk first: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so pls go consult the corresponding Linux manual page for the deets on Linux behavior), or β and this is the sad part β the interface may not be implemented on Linux at all. π it's giving "check your distro before you commit".
β¨ NAME β¨
ilogb, ilogbf, ilogbl β return an unbiased exponent. periodt. that's the whole job. π
π SYNOPSIS π
#include <math.h>
int ilogb(double x);
int ilogbf(float x);
int ilogbl(long double x);
three sisters, three float types, one tiny int coming back at you. π
π¦ DESCRIPTION π¦
first things 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's unintentional β no drama was meant. this volume of POSIX.1β2017 defers to the ISO C standard. so ISO C is the referee, we're just the commentator. π«Ά
so what do these functions actually do? they shall return the exponent part of their argument x. formally, the return value is the integral part of log<sub>r</sub>|x| as a signed integral value, for non-zero x, where r is the radix of the machine's floating-point arithmetic, which is the value of FLT_RADIX defined in <float.h>. yeah. math. she's mathing. β¨
π if you, an application, want to check for error situations like a responsible main character: seterrnoto zero and callfeclearexcept(FE_ALL_EXCEPT)before calling these functions. then on return, iferrnois non-zero, or iffetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW)is non-zero, then an error has occurred. no cap. π¨
π RETURN VALUE π
upon successful completion, these functions shall return the exponent part of x as a signed integer value. they're equivalent to calling the corresponding logb() function and casting the returned value to type int. so if you were gonna do it manually β don't, this already did it for you. π
now the edge cases, and bestie there are so many edge cases:
- if
xis0, the valueFP_ILOGB0shall be returned. on XSI-conformant systems, a domain error shall occur; otherwise, a domain error may occur. - if
xisΒ±Inf, the value{INT_MAX}shall be returned. on XSI-conformant systems, a domain error shall occur; otherwise, a domain error may occur. - if
xis aNaN, the valueFP_ILOGBNANshall be returned. on XSI-conformant systems, a domain error shall occur; otherwise, a domain error may occur. - if the correct value is greater than
{INT_MAX}, a domain error shall occur and an unspecified value shall be returned. on XSI-conformant systems, a domain error shall occur and{INT_MAX}shall be returned. - if the correct value is less than
{INT_MIN}, a domain error shall occur and an unspecified value shall be returned. on XSI-conformant systems, a domain error shall occur and{INT_MIN}shall be returned.
see how it's giving "unspecified value" in some lanes and a very specific value in the XSI lane? iykyk. π
π₯ ERRORS π₯
these functions shall fail if:
- Domain Error β the correct value is not representable as an integer.
- Domain Error β the
xargument is zero,NaN, orΒ±Inf.
in both of those cases: 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. pick your fighter, but she's showing up. π
these functions may fail if:
- Domain Error β the
xargument is zero,NaN, orΒ±Inf.
same deal: 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.
and then, queen behavior: the following sections are informative. π (translation: the serious spec part is done, the rest is lore.)
π EXAMPLES π
None. that's it. that's the example section. she said "figure it out, bestie." πβ¨ (no seriously, there are none in the original, i'm not gatekeeping.)
π 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. so like, they don't talk to each other, but somebody has to show up to the party. π
π RATIONALE π
the errors come from taking the expected floating-point value and converting it to int, which is an invalid operation in IEEE Std 754β1985 (since overflow, infinity, and NaN are not representable in a type int), so it should be a domain error. makes sense right? you can't cram infinity into an int, she will not fit. π
and then this absolute banger of a line: there are no known implementations that overflow. for overflow to happen, {INT_MAX} must be less than LDBL_MAX_EXP*log2(FLT_RADIX), or {INT_MIN} must be greater than LDBL_MIN_EXP*log2(FLT_RADIX) if subnormals are not supported, or {INT_MIN} must be greater than (LDBL_MIN_EXP-LDBL_MANT_DIG)*log2(FLT_RADIX) if subnormals are supported. π₯ math homework but make it β¨spec justificationβ¨.
π§ FUTURE DIRECTIONS π§
None. the future is bright and uneventful. we love a stable queen. π
β SEE ALSO β
feclearexcept()fetestexcept()logb()scalbln()
also, from the Base Definitions volume of POSIX.1β2017:
- Section 4.20, Treatment of Error Conditions for Mathematical Functions
<float.h><math.h>
go read those, bestie, they're the supporting cast. π«Ά
π 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. 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, bestie!! π this one was brought to you by IEEE/The Open Group, 2017 β yes that's the source and the date, i would never gatekeep the footer. now go return an unbiased exponent and slay. bye mwah πβ¨π
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms