liveBook · Manning

Feature test

As explained in Modern C, Third Edition

A feature test is a compile-time or configuration-time check that determines whether an implementation supports a specified language or library feature.

A feature test checks at compile time or configuration time whether an implementation provides a language or library feature. Conditional definitions let portable code adapt to supported floating-point facilities. section 15.1.1

Conditionally recognize floating-point features

A program can define whether INFINITY is available with #ifdef, then test 1.0/0.0 against it. The same pattern conditionally builds an array of supported exceptions such as FE_DIVBYZERO, FE_OVERFLOW, and FE_UNDERFLOW. section 15.1.1

 int excepts[] = {
 #ifdef FE_DIVBYZERO
   FE_DIVBYZERO,
 #endif
 #ifdef FE_INEXACT
   FE_INEXACT,
 #endif
 #ifdef FE_INVALID
   FE_INVALID,
 #endif
 #ifdef FE_OVERFLOW
   FE_OVERFLOW,
 #endif
 #ifdef FE_UNDERFLOW
   FE_UNDERFLOW,
 #endif
 };

Inspect a conditionally assembled exception list that includes only macros provided by the implementation. section 15.1.1

How Modern C, Third Edition covers it

How deep it goes
Demonstrates conditional macro use and connects it to exception querying, but stops short of a general feature-test design.
Examples
Uses INFINITY detection and conditionally constructs arrays of floating-point exception macros.
What you can do after
Diagnose implementation support and conditionally use floating-point constants and exception facilities.
How it connects
Ties numerical portability to the floating-point environment, floating-point exceptions, and exception testing.

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.