简体   繁体   English

Python 的 CFFI - 将浮点值 #define 指令传递给 cdef()

[英]Python's CFFI - Pass in floating point value #define directives to cdef()

CFFI's documentation says that it supports the use of constants and macros (like #define ) in the cdef "provided the macro is defined to be an integer value", but it doesn't explicitly say that floating point constants are not supported. CFFI 的文档说它支持在cdef中使用常量和宏(如#define )“前提是宏被定义为 integer 值”,但它没有明确表示不支持浮点常量。 I've tried using the #define FOO... syntax with floats and #define FOO 0.5 but both fail, and have not found any more information on how I can possibly use floating point values in this manner.我尝试使用带有浮点数和#define FOO 0.5#define FOO...语法,但都失败了,并且没有找到更多关于如何以这种方式使用浮点值的信息。 I'm trying to call functions from my C libraries from Python.我正在尝试从 Python 调用我的 C 库中的函数。

Is there a way to work around this limitation (if indeed it's a limitation) without touching the C code?有没有办法在不触及 C 代码的情况下解决这个限制(如果确实是一个限制)?

Only integers are supported.仅支持整数。 For things like floats, there are two solutions:对于浮点数之类的东西,有两种解决方案:

  1. You can of course write the constant as pure Python code, if it has a known value, and use that from the rest of your Python code.您当然可以将常量写为纯 Python 代码(如果它具有已知值),并使用 Python 代码的 rest 中的值。 In other words, put it outside CFFI.换句话说,把它放在CFFI之外。

  2. This works in API mode: static const float FOO;这适用于 API 模式: static const float FOO; declares FOO as something that can be read from in C, but not written to.FOO声明为可以在 C 中读取但不能写入的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM