最近写了个应用程序,其中用到了math.h中的很多函数,包括检查一个浮点数是不是正常数,或者检查一个浮点数是不是有限的。这两个宏分别是isnormal和isfinite,在PC上本地编译运行后结果完全正确,但是交叉编译时却死活不支持这两个宏,我先后尝试了几个版本的gcc交叉编译器,包括4.3.2 4.4.3 3.4.5等几个版本。在Google上Search了好久也没有答案,倒是Search出来了windows平台下的防UNIX环境软件cygwin在这两个宏有问题。最后只能换成isnan、isinf这两个宏代替,编译可以通过。
浮点数在运算中可能产生一些特殊的值,有关这方面的定义可以看一些manpage的解释:
NAME
fpclassify, isfinite, isnormal, isnan, isinf - floating-point classification macros
SYNOPSIS
#include <math.h>
int fpclassify(x);
int isfinite(x);
int isnormal(x);
int isnan(x);
int isinf(x);
Link with -lm.
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
fpclassify(), isfinite(), isnormal(): _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE; or cc -std=c99
isnan(): _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE || _ISOC99_SOURCE; or cc -std=c99
isinf(): _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE; or cc -std=c99
DESCRIPTION
Floating point numbers can have special values, such as infinite or NaN. With the macro fpclassify(x) you can
find out what type x is. The macro takes any floating-point expression as argument. The result is one of the
following values:
FP_NAN x is "Not a Number".
FP_INFINITE x is either positive infinity or negative infinity.
FP_ZERO x is zero.
FP_SUBNORMAL x is too small to be represented in normalized format.
FP_NORMAL if nothing of the above is correct then it must be a normal floating-point number.
The other macros provide a short answer to some standard questions.
isfinite(x) returns a non-zero value if
(fpclassify(x) != FP_NAN && fpclassify(x) != FP_INFINITE)
isnormal(x) returns a non-zero value if (fpclassify(x) == FP_NORMAL)
isnan(x) returns a non-zero value if (fpclassify(x) == FP_NAN)
isinf(x) returns 1 if x is positive infinity, and -1 if x is negative infinity.
CONFORMING TO
C99, POSIX.1.
For isinf(), the standards merely say that the return value is non-zero if and only if the argument has an infi‐
nite value.
可以看出来,浮点数计算中可能产生这五种异常 “FP_NAN FP_INFINITE FP_ZERO FP_SUBNORMAL FP_NORMAL” ,这些异常值继续计算是出错的。因此需要排除这些值,或者对此进行一些处理,因此定义了一些宏函数来进行检测。
在没有硬浮点协处理器支持的ARM平台上,需要软浮点技术进行浮点数运算(其实就是一个库函数)。这方面可能有一些问题存在需要详细了解。。