473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

What is the class of a loating point exception ?

In Stroustrup he talks of a MathErr class that you can use to catch
floating point exceptions.
It doesn't exist, at least not in Visual C++

I can catch FP exceptions using catch(...) but am stumped in finding
out what the class I'm catching is.
I would like to know how to catch them in a more elegant way than
catch(...) and then poling around in registers to guess the problem.

TIA
EoG

Mar 18 '07 #1
12 1770
On 2007-03-18 14:26, Ev********@googlemail.com wrote:
In Stroustrup he talks of a MathErr class that you can use to catch
floating point exceptions.
It doesn't exist, at least not in Visual C++

I can catch FP exceptions using catch(...) but am stumped in finding
out what the class I'm catching is.
I would like to know how to catch them in a more elegant way than
catch(...) and then poling around in registers to guess the problem.
In general floating point errors do not throw exceptions in C++, for example

double i = 0;
double j = 1/i;

will not throw an exception (it will however terminate your app). If you
want to handle floating point errors in C++ you have to check for them
yourself and throw an appropriate exception. One way tho do this is to
encapsulate float/double in a class with the same interface as dloat/
double but which performs those checks.

--
Erik Wikström
Mar 18 '07 #2
On Mar 18, 3:24 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
In general floating point errors do not throw exceptions in C++, for example

double i = 0;
double j = 1/i;

will not throw an exception (it will however terminate your app).
Isn't it undefined behaviour? On my gcc it does only result in having
NaN that prints #INF when cout-ed.

Mar 18 '07 #3
On 18 Mar, 14:24, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-03-18 14:26, EvilOld...@googlemail.com wrote:
In Stroustrup he talks of a MathErr class that you can use to catch
floating point exceptions.
It doesn't exist, at least not in Visual C++
I can catch FP exceptions using catch(...) but am stumped in finding
out what the class I'm catching is.
I would like to know how to catch them in a more elegant way than
catch(...) and then poling around in registers to guess the problem.

In general floating point errors do not throw exceptions in C++, for example

double i = 0;
double j = 1/i;

will not throw an exception (it will however terminate your app). If you
want to handle floating point errors in C++ you have to check for them
yourself and throw an appropriate exception. One way tho do this is to
encapsulate float/double in a class with the same interface as dloat/
double but which performs those checks.

--
Erik Wikström
I'm using VC++, and if you set the flags, it will throw an exception
on div/0 et al.
I've always done what Erik suggests, using test & throw, but have long
felt I could do a lot better.
I *nearly* have.
VC++ will throw a proper C++ style exception (not a Win32 SEH one)
But I ca'nt catch it.
If I could determine the type of exception I could then drill down
better.

My goal is to build a framework exceptionally bullet proof FP code,
that allows you to hunt down bugs with some precision.
I'm part way there, but have hit a wall.
Mar 18 '07 #4
<ja******@gmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...

On Mar 18, 3:24 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
In general floating point errors do not throw exceptions in C++, for
example

double i = 0;
double j = 1/i;

will not throw an exception (it will however terminate your app).
Isn't it undefined behaviour? On my gcc it does only result in having
NaN that prints #INF when cout-ed.

Actually, it's an infinity that prints #INF. Dividing by zero is
indeed undefined behavior in Standard C or Standard C++. IEEE 754,
however, defines 1/0 as producing Inf with no need to trap unless
certain bits in the FPP demand it. So it's also permissible, both
under IEEE 754 or other floating-point systems, to terminate the
program.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Mar 18 '07 #5
Ev********@googlemail.com wrote:
On 18 Mar, 14:24, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2007-03-18 14:26, EvilOld...@googlemail.com wrote:
>>In Stroustrup he talks of a MathErr class that you can use to catch
floating point exceptions.
It doesn't exist, at least not in Visual C++
I can catch FP exceptions using catch(...) but am stumped in finding
out what the class I'm catching is.
I would like to know how to catch them in a more elegant way than
catch(...) and then poling around in registers to guess the problem.
In general floating point errors do not throw exceptions in C++, for example

double i = 0;
double j = 1/i;

will not throw an exception (it will however terminate your app). If you
want to handle floating point errors in C++ you have to check for them
yourself and throw an appropriate exception. One way tho do this is to
encapsulate float/double in a class with the same interface as dloat/
double but which performs those checks.

--
Erik Wikström

I'm using VC++, and if you set the flags, it will throw an exception
on div/0 et al.
I've always done what Erik suggests, using test & throw, but have long
felt I could do a lot better.
I *nearly* have.
VC++ will throw a proper C++ style exception (not a Win32 SEH one)
But I ca'nt catch it.
If I could determine the type of exception I could then drill down
better.

My goal is to build a framework exceptionally bullet proof FP code,
that allows you to hunt down bugs with some precision.
I'm part way there, but have hit a wall.

IIRC, when I did something like you are proposing, it required that I
made the exception class myself and somehow (its been a long time) hook
into the SEH to throw the exception myself. That was under VC++ 6.0.

You may want to try a newsgroup that specialises in MS VC++. This group
is for ANSI C++ language discussions.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 18 '07 #6
On 18 Mar, 16:09, "P.J. Plauger" <p...@dinkumware.comwrote:
<jacek...@gmail.comwrote in message

news:11**********************@n59g2000hsh.googlegr oups.com...

On Mar 18, 3:24 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
In general floating point errors do not throw exceptions in C++, for
example
double i = 0;
double j = 1/i;
will not throw an exception (it will however terminate your app).

Isn't it undefined behaviour? On my gcc it does only result in having
NaN that prints #INF when cout-ed.

Actually, it's an infinity that prints #INF. Dividing by zero is
indeed undefined behavior in Standard C or Standard C++. IEEE 754,
however, defines 1/0 as producing Inf with no need to trap unless
certain bits in the FPP demand it. So it's also permissible, both
under IEEE 754 or other floating-point systems, to terminate the
program.

P.J. Plauger
Dinkumware, Ltd.http://www.dinkumware.com
I see that it's permissible, but obviously not compulsory.
I'm trying to catch over/underflows and it's a bit painful.

Mar 18 '07 #7
On 2007-03-18 16:44, Ev********@googlemail.com wrote:
On 18 Mar, 14:24, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2007-03-18 14:26, EvilOld...@googlemail.com wrote:
In Stroustrup he talks of a MathErr class that you can use to catch
floating point exceptions.
It doesn't exist, at least not in Visual C++
I can catch FP exceptions using catch(...) but am stumped in finding
out what the class I'm catching is.
I would like to know how to catch them in a more elegant way than
catch(...) and then poling around in registers to guess the problem.

In general floating point errors do not throw exceptions in C++, for example

double i = 0;
double j = 1/i;

will not throw an exception (it will however terminate your app). If you
want to handle floating point errors in C++ you have to check for them
yourself and throw an appropriate exception. One way tho do this is to
encapsulate float/double in a class with the same interface as dloat/
double but which performs those checks.

--
Erik Wikström

I'm using VC++, and if you set the flags, it will throw an exception
on div/0 et al.
I've always done what Erik suggests, using test & throw, but have long
felt I could do a lot better.
I *nearly* have.
VC++ will throw a proper C++ style exception (not a Win32 SEH one)
But I ca'nt catch it.
If I could determine the type of exception I could then drill down
better.
Leaving the land of standardized C++ behind... It does not say in the
documentation of VC? An alternative, while not exactly what you want,
but couldn't you just

try {
/* FP OP */
}
catch (...) {
throw MyFPException;
}

This works if you can enclose the possible throwing FP operations with
try but at the same time be sure that no other exception can be thrown
in that section.

--
Erik Wikström
Mar 18 '07 #8
On Mar 18, 4:44 pm, EvilOld...@googlemail.com wrote:
My goal is to build a framework exceptionally bullet proof FP code,
that allows you to hunt down bugs with some precision.
I'm part way there, but have hit a wall.
Have you considered performance issues? Because in almost every
serious computation performance is a key parameter.

And also as I have learnt the hard way* some bugs just emerge from
rounding errors. So keep an eye for that too.

* After debugging the whole night I discovered that after swithing all
my templates to double my programs starts to give reasonable
resultus.

Mar 18 '07 #9
I'm trying to do diagnositcs on broken floating point code, hence I
don't care about speed, but care a lot about it not dying.
Erik has recaptured my point that I've reached. I can catch FP
exceptions, but can't work out their class and am forced to use
catch(...)

I'm doing it in (spite of) VC++ but I came to this thread because I
was hoping that standard C++ might have a standard way of handing FP
exceptions.

If this isn't part of the C++ standard, why not ?
I accept that at the lower level it's processor and O/S dependant, but
isn't the point of a standardised high(ish) language that I can write
platform independant code ?

I've read the MS documentation, and whereas each part seems to be
true, it's lots of little bits. Thus you will get code that requires
very precise setting of barely documented parameters.
Don't appreciate docs of the form
"sets the <term not definedto <term not definedstate if you are
running on <obsolete processorbut this is deprecated you should use
<completely undocumented featureto do something subtly different
that we can't be bothered to tel lyou about. The GCC stuff seems as
bad, indeed it reads susprisingly like the MS docs, like the same evil
genous did both :(

I keep thinking that I'm being tragically stupid here. A large % of
all the FP work in the world is done in C++, it's not like I'm pushing
back the boundaries here.

Mar 19 '07 #10
On 19 Mar, 19:10, EvilOld...@googlemail.com wrote:
I'm trying to do diagnositcs on broken floating point code, hence I
don't care about speed, but care a lot about it not dying.
Erik has recaptured my point that I've reached. I can catch FP
exceptions, but can't work out their class and am forced to use
catch(...)

I'm doing it in (spite of) VC++ but I came to this thread because I
was hoping that standard C++ might have a standard way of handing FP
exceptions.

If this isn't part of the C++ standard, why not ?
Nope, not as exceptions at least. It looks like you can determine the
type of FP exception (at least to some degree) using SEH.
I accept that at the lower level it's processor and O/S dependant, but
isn't the point of a standardised high(ish) language that I can write
platform independant code ?
If you want true platform independence you can only standardize the
common subset of all platforms, so if not all platforms support a
certain action then you can not include it unless it can be emulated.
I would suspect that FP exceptions were one of those things that were
not supported on all platforms.

Another problem with FP exceptions is that they are often asynchronous
in the sense that due to optimizations and other stuff you have
already started to execute one or more other statements when the
exception arises. What if you are executing two FP ops and one of them
raises an exception, how to tell which caused the exception, remember
that they can also have been rearranged by the CPU before execution.

--
Erik Wikström

Mar 20 '07 #11
On 20 Mar, 07:52, "Erik Wikström" <eri...@student.chalmers.sewrote:
On 19 Mar, 19:10, EvilOld...@googlemail.com wrote:
I'm trying to do diagnositcs on broken floating point code, hence I
don't care about speed, but care a lot about it not dying.
Erik has recaptured my point that I've reached. I can catch FP
exceptions, but can't work out their class and am forced to use
catch(...)
I'm doing it in (spite of) VC++ but I came to this thread because I
was hoping that standard C++ might have a standard way of handing FP
exceptions.
If this isn't part of the C++ standard, why not ?

Nope, not as exceptions at least. It looks like you can determine the
type of FP exception (at least to some degree) using SEH.
I accept that at the lower level it's processor and O/S dependant, but
isn't the point of a standardised high(ish) language that I can write
platform independant code ?

If you want true platform independence you can only standardize the
common subset of all platforms, so if not all platforms support a
certain action then you can not include it unless it can be emulated.
I would suspect that FP exceptions were one of those things that were
not supported on all platforms.

Another problem with FP exceptions is that they are often asynchronous
in the sense that due to optimizations and other stuff you have
already started to execute one or more other statements when the
exception arises. What if you are executing two FP ops and one of them
raises an exception, how to tell which caused the exception, remember
that they can also have been rearranged by the CPU before execution.

--
Erik Wikström
Although FP exceptions are asychronous, is it not the case that
processors have the equivalent of the Intel fwait, which synchronises
them ?

Mar 20 '07 #12
On 20 Mar, 09:43, "dominic.con...@gmail.com"
<dominic.con...@gmail.comwrote:
On 20 Mar, 07:52, "Erik Wikström" <eri...@student.chalmers.sewrote:
On 19 Mar, 19:10, EvilOld...@googlemail.com wrote:
I'm trying to do diagnositcs on broken floating point code, hence I
don't care about speed, but care a lot about it not dying.
Erik has recaptured my point that I've reached. I can catch FP
exceptions, but can't work out their class and am forced to use
catch(...)
I'm doing it in (spite of) VC++ but I came to this thread because I
was hoping that standard C++ might have a standard way of handing FP
exceptions.
If this isn't part of the C++ standard, why not ?
Nope, not as exceptions at least. It looks like you can determine the
type of FP exception (at least to some degree) using SEH.
I accept that at the lower level it's processor and O/S dependant, but
isn't the point of a standardised high(ish) language that I can write
platform independant code ?
If you want true platform independence you can only standardize the
common subset of all platforms, so if not all platforms support a
certain action then you can not include it unless it can be emulated.
I would suspect that FP exceptions were one of those things that were
not supported on all platforms.
Another problem with FP exceptions is that they are often asynchronous
in the sense that due to optimizations and other stuff you have
already started to execute one or more other statements when the
exception arises. What if you are executing two FP ops and one of them
raises an exception, how to tell which caused the exception, remember
that they can also have been rearranged by the CPU before execution.
--
Erik Wikström

Although FP exceptions are asychronous, is it not the case that
processors have the equivalent of the Intel fwait, which synchronises
them ?
Don't know, but I bet that some processors for embedded systems don't.

--
Erik Wikström

Mar 20 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Mertz | last post by:
At the suggestion of one of my correspondents, I slightly reluctantly implemented an RSS feed for my website/writing. It is perhaps a bit crude so far, but maybe I'll spiff it up. The RSS also...
10
by: Steven T. Hatton | last post by:
I read Stroustrup's article of the day: http://www.research.att.com/~bs/C++.html Programming with Exceptions. InformIt.com. April 2001. http://www.research.att.com/~bs/eh_brief.pdf Some of...
7
by: Aguilar, James | last post by:
Hello all, To begin, yes, this -is- a homework assignment. However, it is for my Algorithms class, and my instructor has given us explicit permission to use "expert" groups like newsgroups, so...
2
by: dinks | last post by:
Hi, I'm new to C++ and have been assigned a task which i dont completely understand. Any help would be greately appreciated. Here is the problem: The class "linkedListType" use the "assert"...
5
by: juergen perlinger | last post by:
Hello out there. sometimes I need to have proper control of the floating point arithmetic of the C(and C++) runtime system, and using the f.p. exception handling of the C99 standard is quite...
26
by: Lasse Edsvik | last post by:
Hello I'm trying to build a simple COM+ app in vs.net using C# and i cant register it in component manager..... what more is needed than this: using System; using...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
0
by: Deepak C.G via .NET 247 | last post by:
I want to dispose the image object in my child form, unless I won't dispose this object i can't delete the image file in my folder. I get this error in MDIparent form "An unhandled exception...
3
by: vainstah | last post by:
Hello Guys and Galls, To start off, I have reached the solution I was looking for, but I would like comments and feedback on the solution I have reached and tips/tricks on making it more elegant....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.