473,785 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1793
On 2007-03-18 14:26, Ev********@goog lemail.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...@goog lemail.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.goo glegroups.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********@goog lemail.com wrote:
On 18 Mar, 14:24, Erik Wikström <Erik-wikst...@telia. comwrote:
>On 2007-03-18 14:26, EvilOld...@goog lemail.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.blogspo t.com/]______\/
Mar 18 '07 #6
On 18 Mar, 16:09, "P.J. Plauger" <p...@dinkumwar e.comwrote:
<jacek...@gmail .comwrote in message

news:11******** **************@ n59g2000hsh.goo glegroups.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********@goog lemail.com wrote:
On 18 Mar, 14:24, Erik Wikström <Erik-wikst...@telia. comwrote:
>On 2007-03-18 14:26, EvilOld...@goog lemail.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...@goog lemail.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

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

Similar topics

3
2384
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 has an HTML front to it. If you want to see the latest news about my _Charming Python_ or _XML Matters_ columns, or about other articles (later, perhaps stuff about my book), take a look at: http://gnosis.cx/rss.xml
10
2058
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 these ideas are finally beginning to sink in. I believe I looked at the same article a while back and decided I wasn't quite ready for it. If I understood things correctly, there seems to be a slight problem with the design of his exception safe...
7
7076
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 if that's your only reason not to help, please do. Otherwise, I guess it's OK. But, just remember, I'm not asking you to do my work for me, just to point out my error. My problem is not with the algorithm itself (standard divide and conquer on...
2
2043
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" facility. I am to get rid of them and replace them with exceptions. I need to create a "linkedListException" class that's declared and implemented in my "linkedListType" class. This class needs to inherit from the base "exception" class and return...
5
2571
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 handy for that purpose. The only problem when dealing with f.p. exception signals is that there is (afaik) no specification *when* the f.p. exception is raised, with one notable exception: 'feraiseexcept(int)' raises the exceptions passed in the...
26
4497
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 System.EnterpriseServices;
13
5058
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
1912
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 of type 'System.ArgumentException' occurred in system.windows.forms.dll" "Additional information: Invalid parameter used." Here is my code:-
3
6436
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. I am not satisfied with the underlying machinery of the solution though. I am an advanced C programmer and most do object-based programming in C++. Please do not reply to this article with references to basic material or obvious tips. An...
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.