basic python files also added.

This commit is contained in:
Falko Victor Habel 2025-03-14 14:22:21 +01:00
parent 31ad0b27cb
commit 0ef21826db
3 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,28 @@
#include <Python.h>
#include "pi.hpp"
static PyObject* approximate_pi_wrapper(PyObject* self, PyObject* args) {
long long number;
if (!PyArg_ParseTuple(args, "L", &number)) { // Changed to 'L' for long long
return NULL;
}
double result = pi::approximate_pi(number); // Change to double
return PyFloat_FromDouble(result); // Return the actual result as a float
}
static PyMethodDef PiMethods[] = {
{"approximate_pi", approximate_pi_wrapper, METH_VARARGS, "Calculate the approximation of pi"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef pi_module = {
PyModuleDef_HEAD_INIT,
"pi",
"Module for calculating the approximation of pi",
-1,
PiMethods
};
PyMODINIT_FUNC PyInit_pi(void) {
return PyModule_Create(&pi_module);
}

View File

@ -0,0 +1,27 @@
#include "pi.hpp"
#include <cmath>
#include <iostream> // Include iostream for debugging
long double pi::approximate_pi(long long number) {
long long lange = number - 2;
long double a = 0, b = 0;
// Compute summation for 'a'
for (long long k = 1; k <= lange; ++k) { // Changed to include lange
long double value = std::sqrt(number * number - k * k);
a += value;
}
// Compute summation for 'b'
for (long long j = 0; j <= lange; ++j) { // Changed to include lange
long double value = std::sqrt(number * number - j * j);
b += value;
}
// Normalize the results
b = 4.0 / (number * number) * b;
a = 4.0 / (number * number) * a;
long double result = (a + b) / 2;
return result;
}

View File

@ -0,0 +1,5 @@
#pragma once
namespace pi {
long double approximate_pi(long long number);
}