gcc-ARM交叉編譯器死活不支援math.h中的isnormal、isfinite兩個巨集
最近寫了個應用程式,其中用到了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平臺上,需要軟浮點技術進行浮點數運算(其實就是一個庫函式)。這方面可能有一些問題存在需要詳細瞭解。。