Catching FP division by 0?

B

BigMan

On most architectures integer division by 0 results in throwing an
exception (does anyone know of one that behaves differently?). I need a
way to catch FP division by 0. Is there a standard way to do so?
 
R

Rolf Magnus

BigMan said:
On most architectures integer division by 0 results in throwing an
exception

It does?
(does anyone know of one that behaves differently?).

Yes. On mine (Linux), it results in a signal SIGFPE. I think that's true for
most or all Un*x systems.
I need a way to catch FP division by 0. Is there a standard way to do so?

No. The only standard way is to avoid division by 0.
 
F

falcon

Just check if the divisor is not equal to zero. Why do you need an
exception to handle this. Throw your own exception if need be. This
should work on all systems.
 
B

BigMan

Division by 0 is better handled by hardware. Moreover, most CPU support
division by zero checks.
 
R

Rolf Magnus

BigMan said:
Division by 0 is better handled by hardware.

One could argue that a division by zero is an error that shouldn't occur
anyway. So it is better to not need to handle it in the first place.
 
B

BigMan

Let's assume that I agree with you on that point. How do you suggest
that we avoid such errors?
 
J

Jeff Flinn

falcon said:
Just check if the divisor is not equal to zero. Why do you need an
exception to handle this.

In my experience, translating the hardware exception to a C++ exception
rather than using an if( divisor == 0 ) led to ~80% speed improvement. This
was in a tight loop in an RK integrator.

Jeff Flinn
 
I

Ioannis Vranos

BigMan said:
On most architectures integer division by 0 results in throwing an
exception (does anyone know of one that behaves differently?). I need a
way to catch FP division by 0. Is there a standard way to do so?


Use some assert function that throws exceptions. Boost library is
probably providing such facilities.
 
E

evaned

Just check if the divisor is not equal to zero. Why do you need an
exception to handle this.

In addition to speed improvements (no slowdown unless it's necessary),
because it's safer. It's useful for the same reason that new throws an
exception by default so you don't have to check the return value
against 0 before using it, and one of the main reasons exceptions were
introduced in the first place.
 
J

Jeff Flinn

BigMan said:
What is the standard way to translate hardware exceptions to C++
exceptions, then?

IIRC, there is no standard or portable way. On win32 there is a
_set_se_translator function that allows translation of windows structured
exceptions to C++ typed exceptions.

Jeff Flinn
 
A

Alf P. Steinbach

* BigMan:
What is the standard way to translate hardware exceptions to C++
exceptions, then?

As other have written, no such.

However, consider whether you can do all right with knowing whether
a division by zero, or other fp error, _has_ occured during the
calculation.

In that case, use std::numeric_limits<double> to check that you have
a non-signalling IEEE fp implementation (in numeric_limits ieee is called
iec, as I recall). For example as a compile time assertion. Then check
for NaN and Inf at the end of the calculation.
 
R

Rolf Magnus

BigMan said:
What is the standard way to translate hardware exceptions to C++
exceptions, then?

There is no standard way, because there is no standard way of getting
notified about a division by zero. It is platform dependant.
 
I

Ioannis Vranos

BigMan said:
What is the standard way to translate hardware exceptions to C++
exceptions, then?


No standard way. If the data can contain a 0 and divide by it (e.g. user
input), you should place explicit run-time checks for it.

If there should not be (you do not expect) a division by 0 there by
logical design, then you should place some "assert" code, like using the
Boost facilities that I mentioned in another message of mine.
 
I

Ioannis Vranos

BigMan said:
I cannot get your point. Could you be more precise, please?


If the data can contain a 0 and divide by it (e.g. user input), you
should place explicit run-time checks for it.

If there should not be (you do not expect) a division by 0 there by
logical design, then you should place some "assert" code, like using
some of the Boost facilities that I mentioned.
 
E

Edernity

Greetings,
It is possible too, to get the fP status from the processor flag.
uncommonly practice, highly platform dependant, go against open systems
cause that is, and highly exclusive/a nice way to say
extravagant+thickmaybe (to open or not to open) if that what u wonder.
Vranos way is common practice. Why, who the hell know
au revoir
Edernity
---------
if it help
please do help me provide better school supplies for childrenof
indonesia.
it may be a donation but I merely ask you to letme send email to you
from one of the product of I'm affiliated and please register there on
mechant account free. If you even want you refund back on your dollar
of i-kard, its ok, I'll send you just use regular delivery on shipping
please. Please note this is very safe since this is a bonafid company.
remember your credit card company will confirm you on your purchase.
u can add or remove more credit cards into your account. you can/should
also ask for always confirmation on send out to you. That will get
those savage cracker bloke out. even u can use some more free teen
refillable visa/mastercard from your bank cc account (can be obtaion
free).
It is just unnerving seeing how can people do not do anything as so
they need so little. 10% could support {{{{{the child 1 month.}}}}
Still have doubts, sign on a personal account and let me contact you
again on it. just one mail pls
also
I'm also affiliated to this site:
www.getaportal.com/portals/edd y_ruslim
Unless u wanto e-gold it at 1369872
New to egold:www.e-gold.com/e-gold.asp?cid= 1369872
NAH, previous way cost u zilch
God bless
 
O

Old Wolf

BigMan said:
On most architectures integer division by 0 results in throwing an
exception (does anyone know of one that behaves differently?). I
need a way to catch FP division by 0. Is there a standard way to
do so?

I have observed in GCC/CygWin that :

double d = 1. / 0;

or any other such division, results in no exception and 'd' having
a value which prints as 'Inf'. This value can successfully
participate in future arithmetic operations (eg. Inf + 1 == Inf).

Obviously this is non-portable but your particular system might
have some similar feature.
 

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
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top