Sean's Note: [C/C++] 函式 pow( ) 的用法

2007年4月29日 星期日

[C/C++] 函式 pow( ) 的用法

這是 math.h 裡的註解

Excess precision when using a 64-bit mantissa for FPU math ops can
cause unexpected results with some of the MSVCRT math functions.  For 
example, unless the function return value is stored (truncating to
53-bit mantissa), calls to pow with both x and y as integral values
sometimes produce a non-integral result.
                                                                               
One workaround is to reset the FPU env to 53-bit mantissa
by a call to fesetenv (FE_PC53_ENV).  Amother is to force storage
of the return value of individual math functions using wrappers.
NB, using these wrappers will disable builtin math functions and
hence disable the folding of function results at compile time when
arguments are constant.
最好不要拿 pow( ) 直接做 Boolen 運算
=================================================
int n = 3;
double temp;
i f ( pow(10, n) == 1000.0 )  <-- which seems alright, but BAD!!
  DO SOMTHING....
temp = pow(10, n);
i f ( temp == 1000.0 )  <-- need another storage, but BETTER!!
  DO SOMTHING....
=================================================

沒有留言:

張貼留言