I want to make a simple C # define macro to return the smaller of two numbers.
I wanted to add a solution when numbers are floating point.
Consider when numbers are floating point numbers and one of the numbers is not-a-number . Then the result a < b always false regardless of the value of another number.
// the result is `b` when either a or b is NaN
It may be desirable for the result to be as shown below, where βNaN arguments are treated as missing dataβ. C11 Footnote # 242
a NaN | b NaN | a < b | min -------+---------+---------+--------------- No | No | No | b No | No | Yes | a No | Yes | . | a Yes | No | . | b Yes | Yes | . | either a or b
To do this with a macro in C, you can simply wrap the fmin() function, which will exceed the table above. Of course, code should usually use the fmin() function directly.
#include <math.h>
Note that fmin(0.0, -0.0) can return 0.0 or -0.0 . Both of them have equal value.
source share