473,943 Members | 25,469 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
12 2587
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 #11
In article <11************ **********@n59g 2000hsh.googleg roups.com>,
<ho**********@g mail.comwrote:
....
>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.

May 17 '07 #12
ho**********@gm ail.com writes:
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:
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.
[...]
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.hheader 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>.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 18 '07 #13

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

Similar topics

28
2731
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
5434
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
32359
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
16575
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
4395
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
3652
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
6008
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
2336
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
1510
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
10135
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
9970
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
11534
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...
1
11299
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
10662
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
9865
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...
0
7390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6308
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4910
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

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.