Usage of abs from math.h

644 views
Skip to first unread message

Gökhan Sever

unread,
May 22, 2011, 1:37:38 AM5/22/11
to cython-users
Hello,

%cython

#cdef extern from "math.h":
# double abs(double)

cdef double x1 = 7.55e-08
cdef double x2 = 5.75e-08

print abs(x2 - x1)

When I use the Python abs, I get the right result: 1.8e-08
However math.h abs gives me 0.0.

How can I get math.h abs giving me the correct result?

Thanks.

Daniel Jung

unread,
May 22, 2011, 5:46:41 AM5/22/11
to cython-users
Hi,

On 22 Mai, 07:37, Gökhan Sever <gokhanse...@gmail.com> wrote:
> How can I get math.h abs giving me the correct result?

hmm, are you sure that there is an abs function defined within math.h?
When I do a simple "import math; dir(math)", I can't find an abs
function. I'm using Python 2.6.6.

Regards,
Daniel Jung

Stefan Behnel

unread,
May 22, 2011, 7:56:40 AM5/22/11
to cython...@googlegroups.com
Daniel Jung, 22.05.2011 11:46:

Good call. There's a fabs() function in math.h and an integer abs()
function in stdlib.h. I think stdlib.h is automatically included, so you
end up using that.

Note that you can cimport the math.h functions from libc.math, so you don't
need to declare them yourself.

That being said, Cython should use fabs() from math.h when the builtin
abs() function is called on double values. It may be quite easy to add an
override in Builtin.py for that.

http://trac.cython.org/cython_trac/ticket/698

Stefan

Gökhan Sever

unread,
May 22, 2011, 12:45:12 PM5/22/11
to cython-users


On May 22, 5:56 am, Stefan Behnel <stefan...@behnel.de> wrote:
> Daniel Jung, 22.05.2011 11:46:
>
> > On 22 Mai, 07:37, Gökhan Sever<gokhanse...@gmail.com>  wrote:
> >> How can I get math.h abs giving me the correct result?
>
> > hmm, are you sure that there is an abs function defined within math.h?
> > When I do a simple "import math; dir(math)", I can't find an abs
> > function. I'm using Python 2.6.6.
>
> Good call. There's a fabs() function in math.h and an integer abs()
> function in stdlib.h. I think stdlib.h is automatically included, so you
> end up using that.

Probably this because I was reading from here ->
http://www.cplusplus.com/reference/clibrary/cmath/abs/
Somehow not seen the fabs function. Thanks for the pointer and
explanation.

>
> Note that you can cimport the math.h functions from libc.math, so you don't
> need to declare them yourself.

This line works fine locally, but fails running in Sage notebook.
from libc.math cimport fabs

Getting this error:
undeclared name not builtin: fabs

>
> That being said, Cython should use fabs() from math.h when the builtin
> abs() function is called on double values. It may be quite easy to add an
> override in Builtin.py for that.
>
> http://trac.cython.org/cython_trac/ticket/698
>
> Stefan

Thanks for the trac input. Indeed fabs creates 3 lines less code when
compiled comparing to the Python's abs.

Robert Bradshaw

unread,
May 23, 2011, 2:22:50 PM5/23/11
to cython...@googlegroups.com
On Sun, May 22, 2011 at 9:45 AM, Gökhan Sever <gokha...@gmail.com> wrote:
>
>
> On May 22, 5:56 am, Stefan Behnel <stefan...@behnel.de> wrote:
>> Daniel Jung, 22.05.2011 11:46:
>>
>> > On 22 Mai, 07:37, Gökhan Sever<gokhanse...@gmail.com>  wrote:
>> >> How can I get math.h abs giving me the correct result?
>>
>> > hmm, are you sure that there is an abs function defined within math.h?
>> > When I do a simple "import math; dir(math)", I can't find an abs
>> > function. I'm using Python 2.6.6.
>>
>> Good call. There's a fabs() function in math.h and an integer abs()
>> function in stdlib.h. I think stdlib.h is automatically included, so you
>> end up using that.
>
> Probably this because I was reading from here ->
> http://www.cplusplus.com/reference/clibrary/cmath/abs/
> Somehow not seen the fabs function. Thanks for the pointer and
> explanation.
>
>>
>> Note that you can cimport the math.h functions from libc.math, so you don't
>> need to declare them yourself.
>
> This line works fine locally, but fails running in Sage notebook.
> from libc.math cimport fabs
>
> Getting this error:
> undeclared name not builtin: fabs

Cython 0.14.1 has been merged into Sage, but Sage hasn't been released
in a while. (It's onto release candidate 4, so hopefully any day
now...)

Stefan Behnel

unread,
May 25, 2011, 8:53:06 AM5/25/11
to cython...@googlegroups.com
Gökhan Sever, 22.05.2011 18:45:

> On May 22, 5:56 am, Stefan Behnel wrote:
>> Cython should use fabs() from math.h when the builtin
>> abs() function is called on double values. It may be quite easy to add an
>> override in Builtin.py for that.
>>
>> http://trac.cython.org/cython_trac/ticket/698
>
> Thanks for the trac input. Indeed fabs creates 3 lines less code when
> compiled comparing to the Python's abs.

If you want to provide a patch, take a look at the function specific
optimisation methods in Optimize.py.

Stefan

Reply all
Reply to author
Forward
0 new messages