Compiler error when using fabsf from math.pxd

19 views
Skip to first unread message

Andrea Gavana

unread,
Nov 16, 2022, 2:12:34 AM11/16/22
to cython-users
Dear Cython users/developers,

    I am porting an old Python 2.7 codebase to Python 3.9.10, and among things that I have to do is this detail.

I am trying to use the fabsf function from libc.math (accepts float32 inputs and returns float32 inputs, as far as I understand: https://en.cppreference.com/w/c/numeric/math/fabs). 

I am running this simple piece of code as a test:

import numpy as np
cimport numpy as np
from libc.math cimport fabsf
from libc.float cimport FLT_MAX

cpdef bint test_fabsf(np.float32_t number):

    return fabsf(number) < FLT_MAX
   
With its setup.py file:

from setuptools import setup
from Cython.Build import cythonize

setup(
    ext_modules = cythonize("test_fabsf.pyx", language_level='3str')
)

I get the following:

Compiling test_fabsf.pyx because it changed.
[1/1] Cythonizing test_fabsf.pyx

Error compiling Cython file:
------------------------------------------------------------
...
import numpy as np
cimport numpy as np

from libc.math cimport fabsf
^
------------------------------------------------------------

test_fabsf.pyx:4:0: 'libc\math\fabsf.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
from libc.float cimport FLT_MAX


cpdef bint test_fabsf(np.float32_t number):

    return fabsf(number) < FLT_MAX
          ^
------------------------------------------------------------

test_fabsf.pyx:10:11: 'fabsf' is not a constant, variable or function identifier
Traceback (most recent call last):
  File "C:\Users\J0514162\MyProjects\Phaser\LinearSolvers\setup_fabsf.py", line 5, in <module>
    ext_modules = cythonize("test_fabsf.pyx", language_level='3str')
  File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\Cython\Build\Dependencies.py", line 1127, in cythonize
    cythonize_one(*args)
  File "C:\Users\J0514162\WinPython39\WPy64-39100\python-3.9.10.amd64\lib\site-packages\Cython\Build\Dependencies.py", line 1250, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: test_fabsf.pyx


I am not sure I understand the error. I can clearly see fabsf declared in Cython\Includes\numpy\math.pxd at line 64 like this:

    float fabsf "npy_fabsf"(float x)

This is on Windows 10 64 bit, with Python 3.9.10 64 bit, NumPy 1.21.5+mkl, Cython 0.29.32. I am sure I am missing something obvious, any suggestion is most welcome :-) .

Thank you.

Andrea.

D Woods

unread,
Nov 16, 2022, 3:20:53 PM11/16/22
to cython-users
On Wednesday, November 16, 2022 at 7:12:34 AM UTC andrea...@gmail.com wrote:

I am not sure I understand the error. I can clearly see fabsf declared in Cython\Includes\numpy\math.pxd at line 64 like this:

Yes, but that isn't where you've cimported it from. You've cimported from Cython/Includes/libc/math.pxd. Not from Numpy.


This is on Windows 10 64 bit, with Python 3.9.10 64 bit, NumPy 1.21.5+mkl, Cython 0.29.32. I am sure I am missing something obvious, any suggestion is most welcome :-) .

It looks like this function was added to libc.math in the Cython master branch but not to Cython 0.29.x (https://github.com/cython/cython/commit/4f9e2e37eb06de526938c428f35a355cdcc466f3).

You have three options:
1. upgrade Cython to the 3.0 alpha release.
2. cimport it from numpy.math instead (given that it seems to be defined there)
3. Just copy the declaration and put in in a `cdef extern from "<math.h>":` block - the Cython .pxd files don't do anything that you can't do yourself.

Andrea Gavana

unread,
Nov 16, 2022, 3:24:32 PM11/16/22
to cython-users
Hi,

    thank you for your answer.

I ended up doing this:

cdef extern from "math.h" nogil:
    cdef float fabsf(float x)

Seems to be working :-) .

Andrea.

 
Reply all
Reply to author
Forward
0 new messages