diff --git a/src/fabelous_math/cpp/functions/bindings/pi_bindings.cpp b/src/fabelous_math/cpp/functions/bindings/pi_bindings.cpp new file mode 100644 index 0000000..c6ddb61 --- /dev/null +++ b/src/fabelous_math/cpp/functions/bindings/pi_bindings.cpp @@ -0,0 +1,28 @@ +#include +#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); +} \ No newline at end of file diff --git a/src/fabelous_math/cpp/functions/pi.cpp b/src/fabelous_math/cpp/functions/pi.cpp new file mode 100644 index 0000000..fa87882 --- /dev/null +++ b/src/fabelous_math/cpp/functions/pi.cpp @@ -0,0 +1,27 @@ +#include "pi.hpp" +#include +#include // 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; +} \ No newline at end of file diff --git a/src/fabelous_math/include/pi.hpp b/src/fabelous_math/include/pi.hpp new file mode 100644 index 0000000..b5f5f31 --- /dev/null +++ b/src/fabelous_math/include/pi.hpp @@ -0,0 +1,5 @@ +#pragma once + +namespace pi { + long double approximate_pi(long long number); +} \ No newline at end of file