返回浮点的问题 [英] problem returning floating point

查看:57
本文介绍了返回浮点的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了这个小应用来解释我有一个更大的

计划的问题。


在下面的代码我''从键盘上取10个整数。在

average()的调用中,然后将这10个整数加上并按入口数除以

返回平均值,即浮点数。


有人可以看看为什么我得到例子:如果总数是227然后除以

10我得到22.00000的回报而不是22.7的正确回答。


提前谢谢

Pete


#include< stdio.h>

浮动平均值( int,int []); / *原型返回浮动* /


int main()

{

int num [10]; / *数组保持10个整数* /

浮动平均值; / * float来保持平均值* /

int i,size = 10;


printf("输入10个整数:\ n");


/ *键盘需要10个整数* /

for(i = 0; i< size; i ++)

{

scanf("%d",& num [i]);

}


avg =平均值(大小,数量); / *调用average()传递大小和数组* /

printf("%f",avg);

}


/ * average()计算总计10个整数&计算平均值,返回浮点数

ans * /


浮动平均值(int count,int numbers [])

{

int total = 0;

int i;

float ans;


for(i = 0 ; i< count; i ++)

{

总计+ =数字[i];


/ * printf("总计=%d \ n,总计); * /

ans =总计/计数;

}

返回ans;


}

解决方案

彼得说:


我写了这个小应用来解释一个问题我正在用一个

更大的程序。


在下面的代码我从键盘上拿出10个整数。在电话中

到平均值()然后将这10个整数加上并除以

整数以返回平均值,即浮点数。


有人可以看看为什么我得到的例子:如果总数是227然后将
除以10我得到22.00000的回报而不是正确答案22.7。



227.0 / 10.0确实是22.7,在合理范围内 - 浮点数的性质

是这样的,你不能总是得到一个尽可能准确地回答。


所以:


227.0 / 10.0是22.7(ish)

227.0 / 10是22.7(ish)

227 / 10.0是22.7(ish)

227/10是22(精确)


发现差异?


< snip>


一次修改就足以解决上述问题:


浮动平均值(int count,int numbers [])

{

int total = 0;



更改为:


浮动总数= 0;


- -

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日





2008年11月6日星期四07:51:03 +0000,Richard Heathfield写道:


Peter说:


>浮动平均值(int count,int numbers [])<对于(i = 0; i< count; i ++)
{} > {
总计+ =数字[i];
ans =总计/计数;
}
返回ans;
}



一次修改就足以解决上述问题:


> int total = 0;



更改为:


float total = 0;



并非严格错误,但建议不好。


只要使用的整数大小足以容纳总和,它是一个更快,更准确的加法整数。


一个更好的改变是:


ans =(double)total /(double)count;


此外,计算每个循环内的除法是非常愚蠢的,

虽然一个不太合适的编译器会实现这个并优化它。


这个函数的我的版本看起来像:


float average(int count,int * numbers){

int total = 0;

int * stop = numbers + count;


而(数字<停止)

总计+ = *数字++;


返回(双倍)总数/计数;

}


我已经删除了下标以支持比较指针,以及

提高速度,以防这是性能关键。


如果一个int无法保持

十个输入的最大可能总和,我会使用长或长。如果那仍然不够,我会使用两个嵌套循环,内部一个使用整数加起来
,外部一个使用双精度。再次,这将提供

提高准确性和提高速度。


在文章< Bh ****** **********@newsfe25.ams2>,

viza< to ****** @ gm-il.com.obviouschange.invalidwrote:
< blockquote class =post_quotes>
>在int无法保持十个输入的最大可能总和的情况下,我将使用长或长。在那仍然不够的情况下,我会使用两个嵌套循环,内部一个使用整数加起来,而外部一个使用双精度。同样,这将提高准确性和提高速度。



在这样的情况下,当一个较小的数字被添加到一个大数字时,就会出现精度损失。因此,合理的方法是将每一半数字加起来
,然后将这些数字加在一起,使用相同的

方法递归地加起来。这样的东西

(未经测试):


double sum_ints(int count,int * numbers)

{

if(count == 0)

返回0.0;

否则if(count == 1)

return(double)数字[0];

否则返回sum_ints(数/ 2,数字)+

sum_ints(count-count / 2,数字+ count / 2);

}


- Richard

-

请记得提及我/你留下的录音带。


I have written this small app to explain an issue I''m having with a larger
program.

In the following code I''m taking 10 ints from the keyboard. In the call to
average() these 10 ints are then added and Divided by the number of ints to
return the average, a float.

Can someone see why I get for example: if the total is 227 then divided by
10 I get a return of 22.00000 instead of the correct answer 22.7.

Thanks in advance
Pete

#include <stdio.h>
float average(int, int[]); /* Prototype returning float */

int main()
{
int num[10]; /* array to hold 10 ints */
float avg; /* float to hold the average */
int i, size=10;

printf("Enter 10 integers: \n");

/* Takes ten ints for keyboard */

for(i=0; i<size; i++)
{
scanf("%d", &num[i]);
}

avg = average(size, num); /* Call to average() passing size and array */
printf("%f", avg);
}

/* average() calculates total of 10 ints & calculates average, returns float
ans */

float average(int count, int numbers[])
{
int total=0;
int i;
float ans;

for(i=0; i<count; i++)
{
total +=numbers[i];

/*printf("Total = %d \n", total);*/
ans = total/count;
}
return ans;

}

解决方案

Peter said:

I have written this small app to explain an issue I''m having with a
larger program.

In the following code I''m taking 10 ints from the keyboard. In the call
to average() these 10 ints are then added and Divided by the number of
ints to return the average, a float.

Can someone see why I get for example: if the total is 227 then divided
by 10 I get a return of 22.00000 instead of the correct answer 22.7.

227.0 / 10.0 is indeed 22.7, within reason - the nature of floating point
is such that you can''t always get an answer as precisely as you''d like.

So:

227.0 / 10.0 is 22.7 (ish)
227.0 / 10 is 22.7 (ish)
227 / 10.0 is 22.7 (ish)
227 / 10 is 22 (precisely)

Spot the difference?

<snip>

One modification will suffice to fix the above problem:

float average(int count, int numbers[])
{
int total=0;

Change this to:

float total=0;

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Hi

On Thu, 06 Nov 2008 07:51:03 +0000, Richard Heathfield wrote:

Peter said:

>float average(int count, int numbers[])
{
int total=0;
int i;
float ans;

for(i=0; i<count; i++)
{
total +=numbers[i];
ans = total/count;
}
return ans;
}


One modification will suffice to fix the above problem:

>int total=0;


Change this to:

float total=0;

Not strictly wrong, but bad advice.

As long as the size of the integer used is big enough to hold the sum, it
is both faster and more accurate to do the addition in an integer.

A much better change to make is:

ans = (double)total / (double)count;

Also, it is massively stupid to calculate the division inside each loop,
although a half-decent compiler will realise this and optimise it away.

My version of this function would look like:

float average( int count, int *numbers ){
int total= 0;
int *stop= numbers + count;

while( numbers < stop )
total+= *numbers++;

return (double)total / count;
}

I have removed the subscripting in favour of comparing pointers, to
improve speed, in case this is performance critical.

In the case where an int couldn''t hold the maximum possible sum of the
ten inputs, I would use a long or long long. In the case where that was
still not enough, I would use two nested loops, the inner one adding up
using integers, and the outer one using doubles. Again, this would give
both increased accuracy and increased speed.


In article <Bh****************@newsfe25.ams2>,
viza <to******@gm-il.com.obviouschange.invalidwrote:

>In the case where an int couldn''t hold the maximum possible sum of the
ten inputs, I would use a long or long long. In the case where that was
still not enough, I would use two nested loops, the inner one adding up
using integers, and the outer one using doubles. Again, this would give
both increased accuracy and increased speed.

Loss of precision in a case like this arises when a small number is
added to a large one. A reasonable approach therefore is to add up
each half of the numbers and then add these together, using the same
method recursively to add up the halves. Something like this
(untested):

double sum_ints(int count, int *numbers)
{
if(count == 0)
return 0.0;
else if(count == 1)
return (double)numbers[0];
else return sum_ints(count/2, numbers) +
sum_ints(count-count/2, numbers+count/2);
}

-- Richard
--
Please remember to mention me / in tapes you leave behind.


这篇关于返回浮点的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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