| View previous topic :: View next topic |
| Author |
Message |
Dan Guest
|
Posted: Wed Apr 18, 2007 7:55 pm Post subject: log10: SING Error -> Try/Catch?? |
|
|
log10 functions where the value passed to the function is negative
results in a "LOG10: DOMAIN error" if the value is 0 then it's a "LOG10:
SING error" For example the following code will create a DOMAIN error.
#include <math.h>
double DoLogFunction(const double &val)
{
return log10(val);
}
void main()
{
DoLogFunction(0); //SING ERROR
DoLogFunction(-1); //DOMAIN ERROR
}
Try/Catch around the 'DoLogFunction()' function call or even around the
log10 call doesn't work.
Of course I can test val to see if it's negative and non-zero. The
problem is, I think I have done that in the following code:
double DoLogFunction(const double &val)
{
double result = 0;
if (val>0)
{
try
{
result = log10(val);
}
catch (...)
{
;//do nothing, result is already 0
}
}
return result;
}
However, occasionally I still get the error which results in a Windows
dialog box appearing indicating the error.
What is the BEST way to do this type of trapping so that I don't get the
error?
thanks in advance |
|
| Back to top |
|
 |
Thomas Maeder [TeamB] Guest
|
Posted: Thu Apr 19, 2007 12:22 am Post subject: Re: log10: SING Error -> Try/Catch?? |
|
|
Dan <Dan (AT) do (DOT) not.spam> writes:
| Quote: | log10 functions where the value passed to the function is negative
results in a "LOG10: DOMAIN error" if the value is 0 then it's a
"LOG10: SING error" For example the following code will create a
DOMAIN error.
#include <math.h
double DoLogFunction(const double &val)
{
return log10(val);
}
void main()
|
The return type of main() has to be int.
| Quote: | {
DoLogFunction(0); //SING ERROR
DoLogFunction(-1); //DOMAIN ERROR
}
Try/Catch around the 'DoLogFunction()' function call or even around
the log10 call doesn't work.
|
Because what the program does is causing undefined behavior, not
throwing exceptions.
| Quote: | What is the BEST way to do this type of trapping so that I don't get
the error?
|
As for most problems, there isn't a uniform best solution.
Have you considered providing your own version of _matherr()? |
|
| Back to top |
|
 |
Dan Guest
|
Posted: Tue Apr 24, 2007 6:56 pm Post subject: Re: log10: SING Error -> Try/Catch?? |
|
|
| Quote: | The return type of main() has to be int.
Ok; I just used this as an example; of course I didn't strictly copy and |
paste code, but instead just tried to make an example (I work in the
embedded world more often, and void main() is acceptable there. My
apologies.
| Quote: | Because what the program does is causing undefined behavior, not
throwing exceptions.
Is there a way to catch these so that they don't go all the up to the |
top of the application?
| Quote: | Have you considered providing your own version of _matherr()?
How? |
|
|
| Back to top |
|
 |
Thomas Maeder [TeamB] Guest
|
Posted: Tue Apr 24, 2007 9:12 pm Post subject: Re: log10: SING Error -> Try/Catch?? |
|
|
Dan <Dan (AT) do (DOT) not.spam> writes:
| Quote: | The return type of main() has to be int.
Ok; I just used this as an example; of course I didn't strictly copy
and paste code, but instead just tried to make an example (I work in
the embedded world more often, and void main() is acceptable
there. My apologies.
|
void main() isn't acceptable anywhere. At no time in history did C or
C++ accept any return type from main() other than int.
All this isn't extremely important, but whenever somebody posts code
to a newsgroup, somebody else might use it as a (good) example; I
therefore tend to be pedantic. My apologies :-)
| Quote: | Because what the program does is causing undefined behavior, not
throwing exceptions.
Is there a way to catch these so that they don't go all the up to
the top of the application?
Have you considered providing your own version of _matherr()?
How?
|
RTFM. Or Google. Or both. |
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Tue Apr 24, 2007 9:20 pm Post subject: Re: log10: SING Error -> Try/Catch?? |
|
|
maeder (AT) glue (DOT) ch (Thomas Maeder [TeamB]) wrote:
| Quote: | void main() isn't acceptable anywhere. At no time in history did C or
C++ accept any return type from main() other than int.
|
Though in a freestanding implementation, it may not be required that
main() actually exist!
| Quote: | All this isn't extremely important, but whenever somebody posts code
to a newsgroup, somebody else might use it as a (good) example; I
therefore tend to be pedantic. My apologies
|
Bearing in mind the sheer volume of Herbert-Schildt-inspired code out
there, it sometimes seems like a losing battle.
Alan Bellingham
--
ACCU Conference 2008: 2-5 April 2008 - Oxford (probably), UK |
|
| Back to top |
|
 |
Bob Gonder Guest
|
Posted: Tue Apr 24, 2007 9:44 pm Post subject: Re: log10: SING Error -> Try/Catch?? |
|
|
Dan wrote:
| Quote: | Have you considered providing your own version of _matherr()?
How?
|
http://msdn2.microsoft.com/en-us/library/aa298453(VS.60).aspx
There is also a section in the BDS help on it. |
|
| Back to top |
|
 |
Bruce Salzman Guest
|
Posted: Wed Apr 25, 2007 1:43 am Post subject: Re: log10: SING Error -> Try/Catch?? |
|
|
"Alan Bellingham" <alan (AT) lspace (DOT) org> wrote in message
news:lebs231ac8kcr1nvp2utompgbbt9djq1qh (AT) 4ax (DOT) com...
| Quote: |
Bearing in mind the sheer volume of Herbert-Schildt-inspired code
out
there, it sometimes seems like a losing battle.
|
The legacy of a former prog-rock keyboard player.
:^)
--
Bruce |
|
| Back to top |
|
 |
|