为什么不打印1.0? [英] why doesn't it print 1.0?
问题描述
任何人都可以解释我PLZ,为什么这段代码不打印1.0?
#include< stdio.h>
void main (){
浮动x;
for(x = 0; x <= 1.0; x + = 0.1)
printf("% 0.1f",x);
}
On Sun,22 2004年2月,jwala写道:
任何人都可以解释我PLZ,为什么这段代码不打印1.0 ??
#include< stdio.h> ;
void main(){
int main(void){
^^^非常重要!
float x;
for(x = 0; x< = 1.0; x + = 0.1)
printf("%0.1f",x);
}
http://www.eskimo.com/~ scs / C-faq / s14.html
HTH,
-Arthur
< br>
2004年2月22日00:35:35 -0800, 06 ******* @ ioe.e du.np (jwala)写道:
任何人都可以解释我plz,为什么这段代码不打印1.0?
因为在''for''的''end-1'',这里是我的电脑1.0f - x == -0.0f!= 0.0f?
#include< stdio.h>
void main(){
float x;
for(x = 0; x< = 1.0; x + = 0.1)
printf("%0.1f",x);
}
这似乎没问题:
#include< ; stdio.h>
#include< float.h>
int main(void)
{float x;
for(x = 0.0f; x <= 1.0f + FLT_EPSILON; x + = 0.1f)
printf("%1.1f",x) ;
printf(" \ n");
返回0;
}
>
" jwala" < 06 ******* @ ioe.edu.np>在消息中写道
news:56 ************************** @ posting.google.c om ...任何人都可以解释我plz,为什么这段代码不打印1.0?
#include< stdio.h>
void main(){
int main(){
浮动x;
(x = 0; x <= 1.0; x + = 0.1)> printf("%0.1f",x);
putchar(''\ n'');
返回0;
}
如果输出的最后一行没有以换行符结尾,则
标准不保证您的程序会输出任何内容。
但是要回答你的问题,你需要对
浮点数的实现进行一些基础研究。在我的系统上输出以下程序
应该给你一个线索...
%类型float.c
#include< stdio.h>
int main(无效)
{
浮动x;
for(x = 0; x <= 1.1; x + = 0.1)
printf("%0.9f\\\
",x);
返回0;
}
%gcc -ansi -pedantic float.c -o float
%浮动
0.000000000
0.100000001
0.200000003
0.300000012
0.400000006
0.500000000
0.600000024
0.700000048
0.800000072
0.900000095
1.000000119
%
浮点计算通常不准确。
-
Peter
can anybody explain me plz, why this code doesn''t print 1.0??
#include <stdio.h>
void main(){
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);
}
On Sun, 22 Feb 2004, jwala wrote:
can anybody explain me plz, why this code doesn''t print 1.0??
#include <stdio.h>
void main(){
int main(void) {
^^^ very important!
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);
}
http://www.eskimo.com/~scs/C-faq/s14.html
HTH,
-Arthur
On 22 Feb 2004 00:35:35 -0800, 06*******@ioe.edu.np (jwala) wrote:
can anybody explain me plz, why this code doesn''t print 1.0??
because at ''end-1'' of ''for'', here in my PC 1.0f - x == -0.0f != 0.0f ?
#include <stdio.h>
void main(){
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);
}
This seems ok:
#include <stdio.h>
#include <float.h>
int main(void)
{float x;
for(x=0.0f; x <= 1.0f + FLT_EPSILON; x += 0.1f)
printf("%1.1f ", x);
printf("\n");
return 0;
}
"jwala" <06*******@ioe.edu.np> wrote in message
news:56**************************@posting.google.c om...can anybody explain me plz, why this code doesn''t print 1.0??
#include <stdio.h>
void main(){
int main(){
float x;
for (x=0;x<=1.0;x+=0.1)
printf("%0.1f ",x);
putchar(''\n'');
return 0;
}
If the last line of output isn''t terminated with a newline character, the
standard offers no guarantee that your program will output anything.
But to answer your question, you need to do some basic research on how
floating point numbers are implemented. The output of the following program
on my system should give you a clue...
% type float.c
#include <stdio.h>
int main(void)
{
float x;
for (x = 0; x <= 1.1; x += 0.1)
printf("%0.9f\n",x);
return 0;
}
% gcc -ansi -pedantic float.c -o float
% float
0.000000000
0.100000001
0.200000003
0.300000012
0.400000006
0.500000000
0.600000024
0.700000048
0.800000072
0.900000095
1.000000119
%
Floating point computation is generally not exact.
--
Peter
这篇关于为什么不打印1.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!