Sample code (t91.c):

#include <stdio.h>
#include <fenv.h>

#if _MSC_VER
#pragma fenv_access (on)
#else
#pragma STDC FENV_ACCESS ON
#endif

void show_fe_exceptions(void)
{
    printf("exceptions raised:");
    if(fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT)) printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID)) printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW)) printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW)) printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
int main(void)
{
    feraiseexcept(FE_OVERFLOW);
    show_fe_exceptions();
    return 0;
}

Invocations:

$ clang t91.c -Wall -Wextra -pedantic && ./a.exe
t91.c:7:14: warning: pragma STDC FENV_ACCESS ON is not supported, ignoring pragma [-Wunknown-pragmas]
#pragma STDC FENV_ACCESS ON
             ^
1 warning generated.
exceptions raised: FE_OVERFLOW

$ gcc t91.c -Wall -Wextra -pedantic && ./a.exe
t91.c:7: warning: ignoring ‘#pragma STDC FENV_ACCESS’ [-Wunknown-pragmas]
    7 | #pragma STDC FENV_ACCESS ON
      |
exceptions raised: FE_OVERFLOW

$ cl t91.c /fp:strict && t91
exceptions raised: FE_INEXACT FE_OVERFLOW

ISO/IEC 9899:2011 (E):

7.6.2.2 The fegetexceptflag function

Whether the feraiseexcept function additionally raises the ‘‘inexact’’ floating-point exception whenever it raises the ‘‘overflow’’ or ‘‘underflow’’ floating-point exception is implementation-defined.

However, documentation for gcc and documentation for Microsoft C does not (or I cannot find it) document the exact behavior of feraiseexcept function. Note, that Clang/LLVM documents its implementation-defined behavior directly via the source code.

Also ISO C conformant implementation shall be accompanied by a document that defines all implementation-defined and locale-specific characteristics and all extensions (ISO/IEC 9899:2011 (E), section 4 paragraph 8).

Question (finally!): why C implementations do not document all the implementation-defined characteristics / behavior?

UPD. Yes, GCC's manual says wrt Library Functions: The behavior of most of these points are dependent on the implementation of the C library, and are not defined by GCC itself. Hence, we have to look at glibc implementation of feraiseexcept (which is a weak_alias for __feraiseexcept).

0

There are 0 answers