Convert infinity to zero

H

horacius.rex

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
 
D

Dave Vandervies

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)
{
/*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
 
T

Tim Prince

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.
....
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.
 
M

Martin Ambuhl

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.
....

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 */ |
 
I

Irish

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

Maybe I'm missing something, but if x or x is equal to zero at some
point, I think you should handle that as soon as you know its value
rather than after you divide by it and save yourself (and your CPU)
the trouble of obtaining a value you're going to throw away/replace
with zero.

for(i=...)
if(x == 0) ... // set value to zero
else ... // set value to 1/x
...

Like Tim said, some actual code might help.
 
K

Kenny McCormack

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

Maybe I'm missing something, but if x or x is equal to zero at some
point, I think you should handle that as soon as you know its value
rather than after you divide by it and save yourself (and your CPU)
the trouble of obtaining a value you're going to throw away/replace
with zero.

for(i=...)
if(x == 0) ... // set value to zero
else ... // set value to 1/x
...

Like Tim said, some actual code might help.


But, as always, that's the problem with "simplifying your code down to
the smallest, compilable example that demonstrates the problem" (as we
all urge the newbies to do).

See, the actual thing that they are trying to learn is how to handle
errors. So, as we urge them to do, they simplify it down before
posting, then we (wrongly) try to solve their "problem as posted" -
i.e., don't cause the error in the first place - which bears no
relevance to their actual issue (what they are actually trying to teach
themselves).
 
B

Berk Birand

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

How about checking the value of 'x' before finding it's inverse, and doing
1/x only if it's not zero? As in


for i = ....
if (x_i != 0)
x = 1/x; // calculate 1/x_i
// no need to do the following, since x_i is already zero
// if x_i = inf then x_i = 0.

HTH,
Berk
 
H

horacius.rex

Yes, that's obvious, of course.

The problem is that, due to some details of my program (I receive the
results from a blackbox object), I can not do that check. That would
be the best, of course.

Sorry if I fogot to mention this on my previous post.

Perhaps is not possible to do this check on C ...
 
H

horacius.rex

Maybe I'm missing something, but if x or x is equal to zero at some
point, I think you should handle that as soon as you know its value
rather than after you divide by it and save yourself (and your CPU)
the trouble of obtaining a value you're going to throw away/replace
with zero.

for(i=...)
if(x == 0) ... // set value to zero
else ... // set value to 1/x
...

Like Tim said, some actual code might help.

But, as always, that's the problem with "simplifying your code down to
the smallest, compilable example that demonstrates the problem" (as we
all urge the newbies to do).

See, the actual thing that they are trying to learn is how to handle
errors. So, as we urge them to do, they simplify it down before
posting, then we (wrongly) try to solve their "problem as posted" -
i.e., don't cause the error in the first place - which bears no
relevance to their actual issue (what they are actually trying to teach
themselves).



Hi,

my problem is that I have a black-box routine which gives me the
results. So I can not check before anything. I can only work with the
results.

If not, of course that the solution would be obvious and I would fix
the program for not to give the "infinity" result.

So, any of you know how to solve this programming puzzle ? Can you
handle the infinity ? ....
 
H

horacius.rex

Maybe I'm missing something, but if x or x is equal to zero at some
point, I think you should handle that as soon as you know its value
rather than after you divide by it and save yourself (and your CPU)
the trouble of obtaining a value you're going to throw away/replace
with zero.

for(i=...)
if(x == 0) ... // set value to zero
else ... // set value to 1/x
...

Like Tim said, some actual code might help.

But, as always, that's the problem with "simplifying your code down to
the smallest, compilable example that demonstrates the problem" (as we
all urge the newbies to do).

See, the actual thing that they are trying to learn is how to handle
errors. So, as we urge them to do, they simplify it down before
posting, then we (wrongly) try to solve their "problem as posted" -
i.e., don't cause the error in the first place - which bears no
relevance to their actual issue (what they are actually trying to teach
themselves).



Hi,

my problem is that I have a black-box routine which gives me the
results. So I can not check before anything. I can only work with the
results.

If not, of course that the solution would be obvious and I would fix
the program for not to give the "infinity" result.

So, any of you know how to solve this programming puzzle ? Can you
handle the infinity ? ....
 
K

Kenny McCormack

Hi,

my problem is that I have a black-box routine which gives me the
results. So I can not check before anything. I can only work with the
results.

Your real problem is that you posted this twice.
If not, of course that the solution would be obvious and I would fix
the program for not to give the "infinity" result.

So, any of you know how to solve this programming puzzle ? Can you
handle the infinity ? ....

Exactly my point. That your real problem prevented you from doing it
"the easy way". That was the point I was trying to put in front of some
of the other posters.
 
K

Keith Thompson

[...]
my problem is that I have a black-box routine which gives me the
results. So I can not check before anything. I can only work with the
results.

If not, of course that the solution would be obvious and I would fix
the program for not to give the "infinity" result.

So, any of you know how to solve this programming puzzle ? Can you
handle the infinity ? ....

The <math.h> header declares a number of classification macros:

fpclassify() yields an int value for a floating-point argument, one of
FP_INFINITE
FP_NAN
FP_NORMAL
FP_SUBNORMAL
FP_ZERO

Other macros, yielding a true or false result, are:
isfinite()
isinf()
isnan()
isnormal()

These are new in C99, so your implementation may or may not support
them. Even if it does, they may or may not work as you want them to;
the standard doesn't require implementations to support NaNs and
Infinities.

If your black-box routine divides by zero internally (and you're not
able to detect this in advance by checking the arguments you feed into
the black box), then strictly speaking it's already invoked undefined
behavior, and you can't reliably do anything about it. But *if* your
implementation supports the classification macros, and *if* a
floating-point division by zero yields Infinity or NaN (NaN for
0.0/0.0, Infinity for nonzero/0.0), then that's probably your best
bet.

Consult your implementation's documentation to find out how it behaves
on floating-point division by zero and whether it supports the
classification macros.

See also section 7.12.3 of the C99 standard; the latest draft is
available at
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top