Discussion:
pow() overflows but cannot catch error (BCB6)
(too old to reply)
Chris Gatcombe
2008-08-01 15:26:51 UTC
Permalink
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
2008-08-01 16:44:21 UTC
Permalink
math.h functions never throw an exception, since they have to be
compatible with C, and C does not have exceptions.

Richard
Post by Chris Gatcombe
Hi,
I have some legacy code (BCB6) using pow() which is giving me an
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.
[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);
.
.
.
}
.
.
[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.
Bob Gonder
2008-08-01 17:30:52 UTC
Permalink
Post by Chris Gatcombe
I have some legacy code (BCB6) using pow() which is giving me an
overflow error.
Try/Catch does not seem to do anything, and I'm not using _matherr() or
You should be..
Google _matherr borland for discussions on getting it to work.
AlexB
2008-08-04 04:14:53 UTC
Permalink
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

Loading...