473,722 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Catching floating point errors from linked C code

Hi all

I have some C code that is giving me some 'nan' values in some
calculations. The C code is wrapped using SWIG to give me a Python
module that I am then exercising through a unittest suite.

It seems that I should expect the C code to throw floating point
exceptions (SIGFPE) and either the whole thing to abort, or for Python
to catch the errors and report them. Instead I'm getting neither. I
want to be able to track down the exact location of problems in my C
code.

Is there something that Python does to turn on/off the catching of
divide-by-zero errors, eg by manipulating <signal.hsign al handlers
and/or <fenv.hsettings ?

Cheers
JP

Jan 25 '07 #1
4 1964
John Pye wrote:
Hi all

I have some C code that is giving me some 'nan' values in some
calculations. The C code is wrapped using SWIG to give me a Python
module that I am then exercising through a unittest suite.

It seems that I should expect the C code to throw floating point
exceptions (SIGFPE) and either the whole thing to abort, or for Python
to catch the errors and report them. Instead I'm getting neither. I
want to be able to track down the exact location of problems in my C
code.

Is there something that Python does to turn on/off the catching of
divide-by-zero errors, eg by manipulating <signal.hsign al handlers
and/or <fenv.hsettings ?
What platform are you on?

Python is probably running with floating point exceptions disabled,
but you can enable them in your C code, and restoring the floating
point mode when you leave, if you want. This is probably
only worth doing under a debugger, because otherwise you just end
up with an aborted instance of Python.

John Nagle
Jan 25 '07 #2
Hi John,

On Jan 25, 3:43 pm, John Nagle <n...@animats.c omwrote:
Python is probably running with floating point exceptions disabled,
but you can enable them in your C code, and restoring the floating
point mode when you leave, if you want. This is probably
only worth doing under a debugger, because otherwise you just end
up with an aborted instance of Python.
So as far as you know, Python doesn't switch stuff like
feenableexcept( FE_EXCEPT_ALL)? Does Python use the <signal.h>
internally, or are all its FPEs detected by explicit checks beforehand?

If writing a python module with SWIG, and the module uses Python
callbacks, do I need to restore the signal handlers before running the
callback, or python do that itself?

(These might be questions better asked on the SWIG list I guess)

Cheers
JP

Jan 25 '07 #3
John Pye wrote:
Hi John,

On Jan 25, 3:43 pm, John Nagle <n...@animats.c omwrote:
>>Python is probably running with floating point exceptions disabled,
but you can enable them in your C code, and restoring the floating
point mode when you leave, if you want. This is probably
only worth doing under a debugger, because otherwise you just end
up with an aborted instance of Python.


So as far as you know, Python doesn't switch stuff like
feenableexcept( FE_EXCEPT_ALL)? Does Python use the <signal.h>
internally, or are all its FPEs detected by explicit checks beforehand?
I have no idea what state Python leaves the FPU in, but whatever
the state is, you can read it, set your own state, and restore
the previous state before returning to Python.

The last time I had to do this, I used

unsigned int oldstate = // make almost all FPU errors fatal
_controlfp ((~_EM_INVALID) & _CW_DEFAULT, _MCW_EM);
...
_controlfp(olds tate); // restore old state

on Windows 2000.
John Nagle
Jan 25 '07 #4
Hi John

I think you're right, and I need to place FPE 'brackets' around my
code.

The thing that was confusing me was that python has division by zero
exceptions (for which I assumed fenv.h was being used), but when from
python I reach down into my SWIG code and do a 1/0, no SIGFPE is
thrown.

I will experiment with the _controlfp stuff (and linux equivs) and see
how it goes. Thanks for your help.

Cheers
JP
On Jan 25, 5:12 pm, John Nagle <n...@animats.c omwrote:
John Pye wrote:
Hi John,
On Jan 25, 3:43 pm, John Nagle <n...@animats.c omwrote:
>Python is probably running with floating point exceptions disabled,
but you can enable them in your C code, and restoring the floating
point mode when you leave, if you want. This is probably
only worth doing under a debugger, because otherwise you just end
up with an aborted instance of Python.
So as far as you know, Python doesn't switch stuff like
feenableexcept( FE_EXCEPT_ALL)? Does Python use the <signal.h>
internally, or are all its FPEs detected by explicit checks beforehand? I have no idea what state Python leaves the FPU in, but whatever
the state is, you can read it, set your own state, and restore
the previous state before returning to Python.

The last time I had to do this, I used

unsigned int oldstate = // make almost all FPU errors fatal
_controlfp ((~_EM_INVALID) & _CW_DEFAULT, _MCW_EM);
...
_controlfp(olds tate); // restore old state

on Windows 2000.

John Nagle
Jan 26 '07 #5

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

Similar topics

4
3310
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can interact with a rich 3D environment. Seem to be 90% of the way there! My problems are related to calculations where the result tends to zero (or another defined limit.) Have loads of cases where this kind of interaction occurs but this one
6
2218
by: sreelal | last post by:
Dear friends, Please consider the following C program. #include<stdio.h> struct employee { int id;
15
3930
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that differ by less than this epsilon?
4
9555
by: Rajesh | last post by:
struct symbol { char ch; float probability; char *codeword; }; void get_user_input(struct symbol **sptr) { .....
7
2339
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block of code, when System.Exception seems to get the job done for me. My application is an ASP.Net intranet site. When I catch an exception, I log the stack trace and deal with it, normally by displaying an error
13
4148
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make sense to punch out from managed code to native code (I was using IJW) in order to do some amount of floating point work and, if so, what that certain amount of floating point work was approximately. To attempt to do this I made a program that...
13
5170
by: Chris Stankevitz | last post by:
Hi, I have a very large Visual c++ .net 2003 7.1 native c application (approximately 500,000 lines of code). This application is a simulation that frequently works with floating point numbers. From time to time I do something bad -- for example divide by zero or try to grow a float beyond numeric_limits<float>::max() I told MSVC .net 2003 7.1 to "Break into the debugger" for ALL exceptions in
4
2835
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I would like your expert advise before I publish any serious nonsense. Any comments are welcome, style, organization, hard errors, etc.
8
5774
by: neha | last post by:
hi Its been Given that Which error are you likely to get when you run the following program? main() { struct emp { char name;
0
8863
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
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
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
9088
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
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3207
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
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.