Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pow() overflows but cannot catch error (BCB6)

29 views
Skip to first unread message

Chris Gatcombe

unread,
Aug 1, 2008, 11:26:51 AM8/1/08
to
Hi,

I have some legacy code (BCB6) using pow() which is giving me an
overflow error. It appears like a MessageBox() popup with the text "pow:
OVERFLOW error", but I do not see anything like this anywhere in my
source code.

Try/Catch does not seem to do anything, and I'm not using _matherr() or
any of the 8087 FPU control functions. Even if I do use _matherr() I
still can't catch the overflow.


My code looks like this:

[DebugPrintThreadId() prints a debug message to the error/event log]
[f64 is a typedef for double]


f64
qc_pow(
f64 const in_x,
f64 const in_y)
{
.
.
.
DebugPrintThreadId(fname,"Begin");
f64 retval;
msg = "in_x = " + to_str(in_x) + ", in_y = " + to_str(in_y);
DebugPrintThreadId(fname,msg);
DebugPrintThreadId(fname,"Calling pow()...");
errno = 0;
try
{
if (in_x > 1e200) DebugPrintThreadId(fname,"in_x too big");
if (in_y > 200) DebugPrintThreadId(fname,"in_y too big");
retval = std::pow(in_x,in_y);
}
catch (...)
{
DebugPrintThreadId(fname,"Caught exception");
if (in_x < 0) retval = -std::numeric_limits<f64>::max();
if (in_x > 0) retval = std::numeric_limits<f64>::max();
}
DebugPrintThreadId(fname,"...returned from pow()");
msg = "errno = " + to_str(errno);
DebugPrintThreadId(fname,msg);
.
.
.
}

My error log looks like this:

.
.
[qc_pow] Begin
[qc_pow] in_x = 10, in_y = 321.864
[qc_pow] Calling pow()...
[qc_pow] in_y too big
.
.


There is no message from the catch block, nor from the final "..returned
from" message. Its like qc_pow() just aborted.

What am I missing (or misunderstanding) here?

Thanks,

Chris.

Richard Kaiser

unread,
Aug 1, 2008, 12:44:21 PM8/1/08
to
math.h functions never throw an exception, since they have to be
compatible with C, and C does not have exceptions.

Richard


Chris Gatcombe schrieb:

AlexB

unread,
Aug 4, 2008, 12:14:53 AM8/4/08
to
Include into project:

MyErr.h
-------
int _matherr(_exception *e);

MyErr.cpp
---------
int _matherr(_exception *e)
{
AnsiString msg;
switch(e->type)
{
case DOMAIN:
msg.printf("%s: arg out of range", e->name); break;
case SING:
msg.printf("%s: singularity", e->name); break;
case OVERFLOW:
msg.printf("%s: overflow", e->name); break;
case UNDERFLOW:
e->retval=0; // msg.printf("%s: underflow", e->name); break;
case TLOSS:
return 1; // msg.printf("%s: loss of accuracy", e->name); break;
case STACKFAULT:
msg.printf("%s: FPU stack overflow", e->name); break;
default:
msg.printf("%s: unknown error", e->name);
}
throw Exception(msg);
}

For more information see comments in *matherr.c files in
RAD Studio\5.0\source\cpprtl\Source\math.
--
Alex

0 new messages