主题:数值输出问题?
酱爆鱼虾蟹
[专家分:40] 发布于 2010-07-26 14:44:00
#include<iostream.h>
void min(float &min,float &max,float a[],int n);
int main()
{float a[]={66.0,33.1,88.3,55.3,22.05};
float min1,max;
min(min1,max,a,5);
cout<<"min="<<min1<<"max"<<max<<endl;
return 0;
}
void min(float &min,float &max,float a[],int n)
{
min=a[0];
max=a[0];
for(int i=0;i<n;i++)
{
if(min>a[i])
min=a[i];
else if(max<a[i])
max=a[i];
}
}
程序输出:min=22.049999max=88.30003
我初始的是定值他为什么输出近拟值啊?
回复列表 (共7个回复)
沙发
alweeq86 [专家分:1170] 发布于 2010-07-26 15:36:00
这就要考虑浮点数据存储的问题
板凳
alweeq86 [专家分:1170] 发布于 2010-07-26 15:39:00
我用VS2010结果很正常
3 楼
酱爆鱼虾蟹 [专家分:40] 发布于 2010-07-26 15:55:00
turbo C++ 3.0这个不行是不是编译器问题呢?
4 楼
yxqyrh [专家分:1070] 发布于 2010-07-27 11:44:00
这个不是编译器的问题
就如同1除以3,得到0.3333,假如告诉你只有5位来存储1/3的结果,那就只能是0.33333
你给出的浮点数值是十进制的,可能转换成二进制的他不能完全表示出来,只有取近似值了
5 楼
cgl_lgs [专家分:21040] 发布于 2010-07-27 15:11:00
建议除非内存空间受限,否则在PC机编程还是尽量用double,float速度并不比double快。
6 楼
xingxing01 [专家分:0] 发布于 2010-07-27 15:53:00
Starting [url=http://www.toppowerlevel.net]wow power leveling[/url] now, and running until the 17th of March, you can submit a picture of yourself sporting a Wowhead rocket logo [url=http://www.mogxe.com/PowerLevel.php?gid=1]wow power leveling[/url] to our contest page for a chance to win some [url=http://www.toppowerlevel.net/powerlist.php?fid=688]wow power leveling[/url] awesome prizes. We're also offering discounts on Wowhead products in [url=http://www.toppowerlevel.net/buy.php]wow gold[/url] the Wowhead [url=http://www.toppowerlevel.net/powerlist.php?fid=7422]aion power leveling[/url] Store during this contest, [url=http://www.toppowerlevel.net/gamelist.php?fid=7656]aion gold[/url] so now's the perfect time to pick up a t-shirt!Those [url=http://www.toppowerlevel.net]cheap wow power leveling[/url] of you who use the Mac version of the Wowhead Client may have been having some trouble [url=http://www.toppowerlevel.net]buy wow power leveling[/url] uploading your data lately. Well, we've been having a bit of trouble with the [url=http://www.toppowerlevel.net]cheap wow gold[/url] framework that the Mac Client uses to handle updating itself. We've updated the Client so that it works properly, [url=http://www.mogxe.com/PowerLevel.php?gid=21]aion power leveling[/url] but I'm afraid you'll need to download it again.
7 楼
moke9 [专家分:30] 发布于 2010-09-02 07:33:00
你好.我是全职网赚工作者.
如果你有时间有电脑.
想在网络上创业.请联系我..
项目绝对真实.详情QQ空间资料
加盟请联系 QQ908889846
我来回复