473,382 Members | 1,657 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,382 software developers and data experts.

C extension + libm oddity [fmod(2.0, 2.0) == nan ?!]

I've been trying to debug this for two days now, and it's a longshot
but I'm hoping that someone here might recognize a solution. I've got
a C extension which calls a function in a C library, which calls
another function in another library, which calls another function,
which calls fmod from the standard C math library. All of these are
shared libraries on Linux (x86 Gentoo 2.6.9). In other words, the
calling looks like this:

Python ->
Python C extension ->
Function from library 1 ->
Function from library 2 ->
fmod from libm.so

The critical line of C code looks like this:
ans = fmod ( x, 2.0 );

... where x is a double with value 2.0 (confirmed by gdb) and ans is a
double.

Now, fmod(2.0, 2.0) should be 0.0. The problem? ans is getting
assigned nan! I have stepped through it in the debugger now dozens of
times. Either fmod is putting the wrong return value on the stack, or
the stack is getting corrupted by something else and "ans" is getting
assigned the wrong value.

This happens only inside of the layered Python extension mess; if I try
to compile a test C program that makes similar calls to fmod, they work
just fine. Likewise, a very simple Python wrapper around fmod also
works (e.g. Python -> Python C extension -> fmod)

This all runs in a single thread, so it doesn't seem like it would be a
threading issue unless Python is making some threads under the hood.
All of the intermediary libraries were compiled with (-g -fPIC), no
optimization.

The intermediary libraries represent thousands of lines of very old
code. It is very possible that all sorts of memory leaks and other
subtle bugs exist, but what kind of memory leak could even cause this
kind of glitch!? How can I even approach debugging it? My next step
right now is going to be stepping through the individual
instructions... arrrrrrrrrrrrrgggh.
Versions: Python 2.4.1, gcc 3.3.6, glibc 2.3.5, Gentoo Linux 2.6.9.

Nov 5 '05 #1
2 1784
"Lonnie Princehouse" <fi**************@gmail.com> writes:
Now, fmod(2.0, 2.0) should be 0.0. The problem? ans is getting
assigned nan! I have stepped through it in the debugger now dozens of
times. Either fmod is putting the wrong return value on the stack, or
the stack is getting corrupted by something else and "ans" is getting
assigned the wrong value.


Have you compiled the C extension with all optimization turned off?
Especially with optimizations on, you can't really tell what is going
to get assigned in a variable because the code isn't computing the
intermediate values you might expect it to. I suggest disassembling
it and stepping through it instruction by instruction if you haven't
done that.
Nov 5 '05 #2
> Have you compiled the C extension with all optimization turned off?

Yes. The C extension's objects are compiled only with the debugging
flag and -fPIC for position indepdendent code, necessary for shared
objects. No optimization. The only things that have any optimization
are Python and glibc (using -O2) I guess I should try glibc without
optimization too... ick.
I suggest disassembling it and stepping through it instruction by instruction if you haven't done that.


Unfortunately I think you are correct ;-)

Nov 5 '05 #3

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

Similar topics

6
by: Greg Bryant | last post by:
PHP Newbie question: I installed the above, and if I point directly to a ..php file (i.e. http://localhost/phpinfo.php, I get the php script executed properly. If I put a <?php ... ?> tag in...
12
by: Tim Rowe | last post by:
If I do from __future__ import division then eval(1/2) gives me 0.5 as expected. But if I do print input("enter a sum: ") and enter 1/2 as the sum I get 0 as if I hadn't done the import. I thought...
12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
1
by: nospam | last post by:
Dear all, I have written a C++ program 1.cpp #include <stdioh> main() { } then I compile with g++ 1.cpp, it generated a ./a.out.
14
by: Josh Ferguson | last post by:
I don't believe a syntax driven equivalent exists for this, but I just thought it would be neat if you could use foreach to do something like this: foreach (Object x in collection1, collection2)...
0
by: Edwin Knoppert | last post by:
At home i can run exe's an my by Windows known special extensions for a specific application. At the office i can't seem to run the exe in my ASP.NET folder by doubleclicking it. It remains...
5
by: jmdocherty | last post by:
All, I've been trying to set up a CSS layout and all seems well in Firefox but in Safari it just seems to be plain weird! I hope someone can help tell me whether this is a problem with my code...
4
by: fd97207 | last post by:
when I did a dir libm.a on my m/c I was surprised to find the size of the file is only about 500 bytes. What does it contain? And from which file does the math code actually come from? I have MinGW...
5
by: rembremading | last post by:
Hi All, I want to use intels LibM math library for my c program under Linux with intel compiler 10.1. I tried to follow the steps in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.