将无穷大转换为零 [英] Convert infinity to zero

查看:160
本文介绍了将无穷大转换为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个代码,在程序的某些部分计算1 / x的

很多不同的x'。大约100次x中的1次等于零,所以当

我打印结果时我获得了inf。我想知道是否有办法检测这个无穷大的价值。并将其转换为浮点零。我的意思是

喜欢


for i = ....

计算1 / x_i

如果x_i = inf则x_i = 0.

....

谢谢

Hi,

I have a code that in some part of the program calculates 1/x for a
lot of different x''s. About 1 of 100 times x is equal to zero, so when
I print the result I obtain inf. I wonder if there is a way to detect
this "infinity" and convert it to the float zero. I mean something
like

for i = ....
calculate 1/x_i
if x_i = inf then x_i = 0.
....
Thanks

推荐答案

在文章< 11 ********************* @ y80g2000hsf.googlegroups中。 com>,

< ho ********** @ gmail.comwrote:
In article <11*********************@y80g2000hsf.googlegroups. com>,
<ho**********@gmail.comwrote:

>

我有一个代码,在程序的某些部分计算1 / x用于许多不同的x'。大约100次x中的1次等于零,所以当我打印结果时我获得了inf。我想知道是否有办法检测这个无穷大的情况。并将其转换为浮点零。我的意思是

对于i = ....

计算1 / x_i

如果x_i = inf则x_i = 0.

....
>Hi,

I have a code that in some part of the program calculates 1/x for a
lot of different x''s. About 1 of 100 times x is equal to zero, so when
I print the result I obtain inf. I wonder if there is a way to detect
this "infinity" and convert it to the float zero. I mean something
like

for i = ....
calculate 1/x_i
if x_i = inf then x_i = 0.
....



--------

if(fabs( x_i)< epsilon)

{

/ *处理1 / x_i的坏值* /

}

另外

{

/ *做1 / x_i * /

的东西}

---- ----


这取决于你在做什么结果,但我不会惊讶

如果将INF折叠为0是错误的方式处理坏的价值。

dave


-

Dave Vandervies dj ****** @ csclub.uwaterloo.ca

我打赌我的一半CD来自现已不再出售的商店商业。这是一件好事,他们不需要每90天给家里打电话来保持工作,真的。 - 彼得·科莱特在可怕的魔鬼修道院中

--------
if(fabs(x_i) < epsilon)
{
/*handle bad value for 1/x_i*/
}
else
{
/*do stuff with 1/x_i*/
}
--------

It depends what you''re doing with the result, but I would be unsurprised
if folding INF to 0 is the wrong way to handle bad values.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I bet half of my CDs are from shops that are now no longer in business. It''s
a good thing that they don''t need to phone home every 90 days to keep
working, really. --Peter Corlett in the scary devil monastery


ho * *********@gmail.com 写道:




我有一个代码,在程序的某些部分计算1 / x的

很多不同的x'。大约100次x中的1次等于零,所以当

我打印结果时我获得了inf。我想知道是否有办法检测这个无穷大的价值。并将其转换为浮点零。我的意思是

喜欢


for i = ....

计算1 / x_i

如果x_i = inf则x_i = 0.

....
Hi,

I have a code that in some part of the program calculates 1/x for a
lot of different x''s. About 1 of 100 times x is equal to zero, so when
I print the result I obtain inf. I wonder if there is a way to detect
this "infinity" and convert it to the float zero. I mean something
like

for i = ....
calculate 1/x_i
if x_i = inf then x_i = 0.
....



这是一个C新闻组;张贴不完整的伪代码不一定

得到你想要的答案。

怎么样

#include< math.h> ;

.....

if(isinf(x_i)){

//做你想做的事

}


由于所有旧的编译器和库都不支持,你可能需要提供自己版本的这个函数。它可能很简单

#include< math.h>

#include< float.h>

int(isinf) (双x){

返回x DBL_MAX || x< -DBL_MAX;

}

当然你可以从常见问题解答中学到一些东西。

This is a C newsgroup; posting incomplete pseudo-code won''t necessarily
get you the answer you wanted.
How about
#include <math.h>
.....
if( isinf(x_i) ) {
//do whatever you wish
}

As this won''t be supported by all older compilers and libraries, you may
have to provide your own version of this function. It may be as simple as
#include <math.h>
#include <float.h>
int (isinf)(double x){
return x DBL_MAX || x < -DBL_MAX ;
}
Surely you could have learned something from FAQs.


ho ********** @ gmail.com 写道:




我有一个代码,在程序的某些部分计算1 / x的

很多不同的x''秒。大约100次x中的1次等于零,所以当

我打印结果时我获得了inf。我想知道是否有办法检测这个无穷大的价值。并将其转换为浮点零。我的意思是

喜欢


for i = ....

计算1 / x_i

如果x_i = inf则x_i = 0.

....
Hi,

I have a code that in some part of the program calculates 1/x for a
lot of different x''s. About 1 of 100 times x is equal to zero, so when
I print the result I obtain inf. I wonder if there is a way to detect
this "infinity" and convert it to the float zero. I mean something
like

for i = ....
calculate 1/x_i
if x_i = inf then x_i = 0.
....



当没有理由测试错误的结果时你可以在第一时间避免错误。


明显的

如果(!x){有什么问题? / *处理零* /}

是你感兴趣的是x足够的情况

小:

const almost_zero; / *为此分配适当的值* /

/& ...... * /

if(fabs(x)< almost_zero){/&处理接近零* / |

There is no reason to test the result of a mistake when you can avoid
the mistake in the first place.

What is wrong with the obvious
if (!x) { /* handle zero */ }
is that what you are interested in is the case where x is sufficiently
small:
const almost_zero; /* assign an appropriate value to this */
/& ... */
if (fabs(x) < almost_zero) { /& handle near zero */ |


这篇关于将无穷大转换为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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