C language to see and remember Part 4 Call C language from Python (argument) double

Receives a * b / c as a return value from Python with real numbers a, b, c as arguments.

capi.py


a,b,c=10.0,20.0,3.0
d=capi.Fcal(a,b,c) #Calculated in c language
print(a*b/c) #Calculated with python
print(d)

Execution result
capi>python capi.py
66.66666666666667
66.66666666666667

capilib.c


#include <Python.h>
static PyObject* Fcal(PyObject* self, PyObject* args){
    double a,b,c,d;
    if (!PyArg_ParseTuple(args, "ddd",&a,&b,&c)){
        return NULL;
    }
    d=a*b/c;
    return Py_BuildValue("d", d);
}
// Function Definition struct
static PyMethodDef myMethods[] = {
    { "Fcal",Fcal, METH_VARARGS, "Fcal a*b/c"},
    { NULL }
};
static struct PyModuleDef myModule = {
    PyModuleDef_HEAD_INIT,"myModule","C API Module",-1,myMethods
};
PyMODINIT_FUNC PyInit_myModule(void){
    return PyModule_Create(&myModule);
}

** Attention point is **

p.c


double a,b,c,d;
    if (!PyArg_ParseTuple(args, "ddd",&a,&b,&c)){
        return NULL;
    }
    d=a*b/c;
    return Py_BuildValue("d", d);
  1. "ddd" is converted to double.
  2. Py_BuildValue ("d", d) returns the return value with double precision. If you calculate with float, it will be different from the value of python.

setup.py


from distutils.core import setup, Extension
setup(name = 'myModule', version = '1.0.0',  \
   ext_modules = [Extension('myModule', ['capilib.c'])])

** Creating a library **

capi>python setup.py install

Recommended Posts

C language to see and remember Part 4 Call C language from Python (argument) double
C language to see and remember Part 2 Call C language from Python (argument) string
C language to see and remember Part 5 Call C language from Python (argument) Array
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
C language to see and remember Part 1 Call C language from Python (hello world)
Go language to see and remember Part 8 Call GO language from Python
Go language to see and remember Part 7 C language in GO language
Call c language from python (python.h)
Call C language functions from Python to exchange multidimensional arrays
[Python] How to call a c function from python (ctypes)
Call Matlab from Python to optimize
Call C from Python with DragonFFI
Python to switch from another language
Call C / C ++ from Python on Mac
[Python] Hit Keras from TensorFlow and TensorFlow from c ++ to speed up execution
Call your own C language shared library from Python using ctypes
[It's not too late to learn Python from 2020] Part 3 Python Language Basic (1)
From Python to using MeCab (and CaboCha)
Porting and modifying doublet-solver from python2 to python3.
Socket communication by C language and Python
Generate C language from S-expressions in Python
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 1)
How to generate permutations in Python and C ++
Writing logs to CSV file (Python, C language)
[Python] How to read data from CIFAR-10 and CIFAR-100
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 3)
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➀
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 2)
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➁
Call a Python script from Embedded Python in C ++ / C ++
List of Python code to move and remember
I want to make C ++ code from Python code!
An easy way to call Java from Python
The story of porting code from C to Go and getting hooked (and to the language spec)
Try to make a Python module in C language
Changes from Python 2 to Python 3.0
Introduction to Python language
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➀
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➁
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
Python regular expression basics and tips to learn from scratch
I tried to illustrate the time and time in C language
Object-oriented in C: Refactored "○ ✕ game" and ported it to Python
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
Pass OpenCV data from the original C ++ library to Python
How to call Python or Julia from Ruby (experimental implementation)
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Ported from R language of "Sazae-san's rock-paper-scissors data analysis" to Python
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
[First Ripple] [I am] Call JavaScript (Node.js) from Python and process Ripple
Introduction to Socket API Learned in C Language Part 1 Server Edition
Install mecab on Sakura shared server and call it from python
[Introduction to Python] I compared the naming conventions of C # and Python.
How to get a string from a command line argument in python
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
[C language] How to create, avoid, and make a zombie process
Call dlm from python to run a time-varying coefficient regression model
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)