Re: [問題] 浮點數的殘值
以下,我使用 VC++ 6.0
compile 的時候有 warning
/*
Compiling...
VC0331.CPP
d:\vc0331\vc0331.cpp(6) : warning C4305: 'initializing' : truncation from
'const double' to 'float'
Linking...
VC0331.exe - 0 error(s), 1 warning(s)
num1=123.1234588623046900000000
num2=123.1234567890123500000000
Press any key to continue
*/
#include <stdio.h>
int main()
{
float num1=123.12345678901234567890;
double num2=123.12345678901234567890;
printf("num1=%16.22f\n",num1);
printf("num2=%16.22lf\n",num2);
return 0;
}
※ 引述《TIME1987 (把握當下)》之銘言:
: 遇到的問題:float與double在位數上的不同
: #include <stdio.h>
: int main()
: {
: float num1=123.456789012345;
: double num2=123.456789012345;
: printf("num1=%16.12f\n",num1);
: printf("num2=%16.12f",num2);
: return 0;
: }
: 程式跑出來的結果:num1=123.456787109375
: num2=123.456789012345
: 開發平台:codeblock
: float只能容納4個位元組 所以是8個位數的精度
: 但為何num1=123.456787109375從第8位之後的數字會是這麼不規律呢?
: 書上說是記憶體的殘值 但這個殘值是怎麼出來的呢?
: 謝謝~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.231.93.135
推
11/12 20:14, , 1F
11/12 20:14, 1F
討論串 (同標題文章)