no exceptions are thrown in math operations

F

Fabian Wein

Hi,

I had one case, where gcc 4.1.0 on an opteron gave 0.0 in case of an
division by 0. I wrote an simple example where I get now inf.
When i do a square-root of a negative number via pow() I get nan.

Now my question is, is there a way that in such cases exceptions are
thrown? At least for the function call this would be nice!

Thanks a lot,

Fabian

double x = 62.5e-6;
double t = x / 0.0;
std::cout << x << " divided by 0.0 = " << t << std::endl;
// -> 6.25e-05 divided by 0.0 = inf

double p = pow(-0.0003, 0.5);
std::cout << "pow = " << p << std::endl;
// -> pow = nan
 
A

Alf P. Steinbach

* Fabian Wein:
I had one case, where gcc 4.1.0 on an opteron gave 0.0 in case of an
division by 0. I wrote an simple example where I get now inf.
When i do a square-root of a negative number via pow() I get nan.

Now my question is, is there a way that in such cases exceptions are
thrown? At least for the function call this would be nice!

In practice it's likely that your C++ compiler supports throwing
floating point operations, but this is not supported by the standard.

If you make floating point operations throw, there is a good chance
you'll have to deal with asynchronous non-C++ operations, and
performance may suffer.

The C library mechanism is the 'signal' function, still in the C++
library, but since a signal handler is invoked asynchronously there are
severe restrictions on what it can do without possibly screwing up
things: in a C++ program setting a flag is in practice the most.
 
F

Fabian Wein

Alf said:
In practice it's likely that your C++ compiler supports throwing
floating point operations, but this is not supported by the standard.
Thanks for the answer! I found no switch up to now for gcc but I know
more now :)

Cool would be such a feature for valgrind :)

Fabian
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top