Sean's Note: 4月 2007

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....
=================================================

2007年4月21日 星期六

[C/C++] ACM - 628 - Passwords

1st is a Russian who solved 1978 problems,
3rd is a Japanese who solved 1790 problems,
21nd is  a Taiwanese who solved 1278 problems,
1389th is me who solved 170 problems,
They are far far far  away from me.......


這題並不會很難, 但是用到 pow() 函式的時候卻發現了有趣的問題.
情況一:
pow(10, 2) == 100    <= TRUE
pow(10, 3) == 1000  <= TRUE
pow(10, 4) == 10000 <= TRUE
情況二:
n = 2;
pow(10, n) == 100     <= TRUE
n = 3;
pow(10, n) == 1000   <= FALSE
n = 4;
pow(10, n) == 10000 <= TRUE
不知道為什麼會這樣哩  : P
去問問數值分析老師好了, 哈哈.