困惑于printf%f [英] Confused to printf %f

查看:72
本文介绍了困惑于printf%f的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



昨天有人问我关于整数除法和printf

的问题,

我告诉他他应该做一个浮动/在你做任何事之前加倍

interger division。

但是他不这么认为,所以我试着做一些例子来解释,

但是,经过一些尝试,

当我尝试做一些整数常数除法时我很困惑,

我知道我应该做浮点除法类似3.0 / 6.0,

但我仍然感到困惑-0.124709来自以下

的例子?

我只是添加了一些虚拟变量,但结果将是显示一些

不同。

我溢出了什么吗?


对于不同的gcc来说情况有点不同/>
编译器,它真的很奇怪。

//示例1

#include< stdio.h>

int main()

{

//双i;

//长a = 1,b = 4;

printf("%f \ n",3/7);

printf("%f\ n",5/7);

printf("%。2f \\ \\ n",5/7);

printf("%f\ n",5.0 / 7.0);

printf("%d \ n" ;,4/7);

返回0;

}


-bash-3.00 $ gcc zc

-bash-3.00 $ ./a.out

-0.124709

-0.124709

-0.12

0.714286

0


**********************


//示例2:

#include< stdio.h>

int main()

{

加倍i;

长a = 1,b = 4;

printf("%f\ n,3/7 );

printf("%f \ nn",5/7);

printf("%。2f \ nn",5/7) ;

printf("%f \ nn",5.0 / 7.0);

printf("%d \ nn",4/7);

返回0;

}

-bash-3.00 $ gcc zc

-bash-3.00 $ ./a .out

0.000000

0.000000

0.00

0.71428 6

0

********

//示例3:

#include< stdio.h>

int main()

{

double i;

/ / long a = 1,b = 4;

printf("%f \ nn",3/7);

printf("%f \ n,5/7);

printf("%。2f \ nn",5/7);

printf("%f \ n" ;,5.0 / 7.0);

printf("%d \ nn",4/7);

返回0;

}


-bash-3.00 $ gcc -v

使用内置规格。

配置:FreeBSD / i386系统编译器

线程模型:posix

gcc版本3.4.2 [FreeBSD] 20040728


-bash-3.00 $ gcc zc

-bash-3.00 $ ./a.out

-0.124709

-0.124709

-0.12

0.714286

0

-bash-3.00 $ gcc41 zc

-bash-3.00 $ ./a.out

0.000000

0.000000

0.00

0.714286

0

谢谢任何信息!

解决方案

gcc zc

-bash-3.00

./ a.out

-0.124709

-0.124709

-0.12

0.714286

0


**********************


//示例2:

#include< stdio.h>

int main()

{

加倍i;

长a = 1,b = 4;

printf("%f \ nn",3/7);

printf("%f \ nn",5/7);

printf("%。2f \ nn",5/7);

printf("%f \ nn",5.0 / 7.0);

printf("%d \ nn",4/7);

返回0;

}

-bash-3.00


gcc zc

-bash- 3.00


Someone asked me a question about integer division and printf
yesterday,
I tell him he should do a casting to float/double before you do any
interger division.
But he doesn''t think so, so I try to do some example to explain,
However, after some trying,
I confused when I try to do some integer constant division,
I know I should do float division something like 3.0/6.0,
but I''m still confused where the -0.124709 comes for the following
example?
I just add some dummy variables, but the result will show some
different.
did I overflow anything?

The situation is somehow a little different for a different gcc
compiler, it''s really wierd.
//example 1
#include <stdio.h>
int main()
{
//double i;
//long a=1,b=4;
printf("%f\n",3/7);
printf("%f\n",5/7);
printf("%.2f\n",5/7);
printf("%f\n",5.0/7.0);
printf("%d\n",4/7);
return 0;
}

-bash-3.00$ gcc z.c
-bash-3.00$ ./a.out
-0.124709
-0.124709
-0.12
0.714286
0

**********************

//example 2:
#include <stdio.h>
int main()
{
double i;
long a=1,b=4;
printf("%f\n",3/7);
printf("%f\n",5/7);
printf("%.2f\n",5/7);
printf("%f\n",5.0/7.0);
printf("%d\n",4/7);
return 0;
}
-bash-3.00$ gcc z.c
-bash-3.00$ ./a.out
0.000000
0.000000
0.00
0.714286
0

********
//example 3:
#include <stdio.h>
int main()
{
double i;
//long a=1,b=4;
printf("%f\n",3/7);
printf("%f\n",5/7);
printf("%.2f\n",5/7);
printf("%f\n",5.0/7.0);
printf("%d\n",4/7);
return 0;
}

-bash-3.00$ gcc -v
Using built-in specs.
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 3.4.2 [FreeBSD] 20040728

-bash-3.00$ gcc z.c
-bash-3.00$ ./a.out
-0.124709
-0.124709
-0.12
0.714286
0
-bash-3.00$ gcc41 z.c
-bash-3.00$ ./a.out
0.000000
0.000000
0.00
0.714286
0

Thanks for any information!

解决方案

gcc z.c
-bash-3.00


./a.out
-0.124709
-0.124709
-0.12
0.714286
0

**********************

//example 2:
#include <stdio.h>
int main()
{
double i;
long a=1,b=4;
printf("%f\n",3/7);
printf("%f\n",5/7);
printf("%.2f\n",5/7);
printf("%f\n",5.0/7.0);
printf("%d\n",4/7);
return 0;
}
-bash-3.00


gcc z.c
-bash-3.00


这篇关于困惑于printf%f的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆