473,830 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert infinity to zero

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

May 16 '07 #1
12 2575
In article <11************ *********@y80g2 000hsf.googlegr oups.com>,
<ho**********@g mail.comwrote:
>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

--
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
May 16 '07 #2
ho**********@gm ail.com wrote:
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.
May 16 '07 #3
ho**********@gm ail.com wrote:
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 */ |

May 16 '07 #4
On May 16, 12:10 pm, horacius....@gm ail.com wrote:
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[i] 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[i] == 0) ... // set value to zero
else ... // set value to 1/x[i]
...

Like Tim said, some actual code might help.

May 16 '07 #5
In article <11************ **********@w5g2 000hsg.googlegr oups.com>,
Irish <ir*******@gmai l.comwrote:
>On May 16, 12:10 pm, horacius....@gm ail.com wrote:
>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[i] 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[i] == 0) ... // set value to zero
else ... // set value to 1/x[i]
...

Like Tim said, some actual code might help.
But, as always, that's the problem with "simplifyin g 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).

May 16 '07 #6
On Wed, 16 May 2007 09:10:48 -0700, horacius.rex wrote:
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

--
Posted via a free Usenet account from http://www.teranews.com

May 17 '07 #7
ho**********@gm ail.com wrote:
>
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.
....
Just reverse it.

if (x_i) {
calculate ....
}

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 17 '07 #8
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 ...


On 16 mayo, 19:33, Martin Ambuhl <mamb...@earthl ink.netwrote:
horacius....@gm ail.com wrote:
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 */ |

May 17 '07 #9
On 16 mayo, 23:26, gaze...@xmissio n.xmission.com (Kenny McCormack)
wrote:
In article <1179345346.304 531.304...@w5g2 000hsg.googlegr oups.com>,

Irish <irish....@gmai l.comwrote:
On May 16, 12:10 pm, horacius....@gm ail.com wrote:
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[i] 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[i] == 0) ... // set value to zero
else ... // set value to 1/x[i]
...
Like Tim said, some actual code might help.

But, as always, that's the problem with "simplifyin g 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 ? ....

May 17 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
2686
by: Grant Edwards | last post by:
I finally figured out why one of my apps sometimes fails under Win32 when it always works fine under Linux: Under Win32, the pickle module only works with a subset of floating point values. In particular the if you try to dump/load an infinity or nan value, the load operation chokes: Under Linux: $ python
3
5427
by: Sidney Cadot | last post by:
Hi all, As I understand it, the rounding direction of signed integer division in C is unspecified in C89/C94, and specified to be 'towards-zero' in C99. I need division 'towards -infinity' (with a 'mod' operation to match), i.e. for x and y I need to obtain D and M such that (1) D*abs(y)+M == x
2
32338
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in what order the data is stored so i try both ways. unsigned char array2 = { 0x23, 0x44, 0xc2, 0xde}; float *pfloat1, *pfloat2;
12
16556
by: Alan | last post by:
how to convert double to short ? for example, I want to convert double doubleVal1 = 15000.1; double doubleVal2 = 12000.0; short shortVal; shortVal = doubleVal1 - doubleVal2; I want the result of shortVal = 3000 and store as short instead of double
2
4378
by: Russell Smith | last post by:
Timestamps support infinity. However if appears dates do not. When timestamps are cast to dates, there is no output. Is this an acceptable option or not? Below are a number of examples showing what I am experiencing. The last own shows how converting timestamps to dates and then ordering doesn't give you the order you want. Maybe you should just order by the timestamp to begin with. However Date does not understand infinity at all.
19
3619
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and places: let's move from here forward please. The usability of any program depends on exact behavior...
28
5943
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly. See section 4.7 for Rounding issues.
13
2325
by: kimiraikkonen | last post by:
Hello, I have an aritmetic calculation like this: First note that: i need a "timer" to get the value for value3. (however removing "timer" didn't differ) Dim value1 As Long Dim value2 As String Dim value3 As String value3 = (value2 * 8) / value1
9
1494
by: John B | last post by:
Hi all, Say I have the int 123456789 What would be the quickest/best way to convert it to int{1,2,3,4,5,6,7,8,9} What I came up with was to determine the largest factor of 10 (100M) divide by that, truncate decimals and that'd be the 1st number, subtract that number multiplied by the current factor, next smallest factor etc.. But it seems very roundabout, surely there'd be a better way :)
0
9793
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10777
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10493
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10526
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10206
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9315
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3960
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.