473,396 Members | 2,158 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,396 software developers and data experts.

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.hsignal handlers
and/or <fenv.hsettings?

Cheers
JP

Jan 25 '07 #1
4 1940
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.hsignal 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.comwrote:
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.comwrote:
>>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(oldstate); // 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.comwrote:
John Pye wrote:
Hi John,
On Jan 25, 3:43 pm, John Nagle <n...@animats.comwrote:
>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(oldstate); // 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
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...
6
by: sreelal | last post by:
Dear friends, Please consider the following C program. #include<stdio.h> struct employee { int id;
15
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...
4
by: Rajesh | last post by:
struct symbol { char ch; float probability; char *codeword; }; void get_user_input(struct symbol **sptr) { .....
7
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...
13
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...
13
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....
4
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...
8
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
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...

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.