By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,404 Members | 933 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,404 IT Pros & Developers. It's quick & easy.

no exceptions are thrown in math operations

P: n/a
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
Mar 12 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 12 '07 #2

P: n/a
Alf P. Steinbach wrote:
>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.
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
Mar 12 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.