diff --git a/example.py b/example.py index c9b0ffc..32dbf17 100644 --- a/example.py +++ b/example.py @@ -1,4 +1,5 @@ -from fabelous_math import is_even, is_odd +from fabelous_math import is_even, is_odd, fabelous_sqrt +print(fabelous_sqrt(50000000000000)) print(is_even(5)) -print(is_odd(19)) \ No newline at end of file +print(is_odd(19)) diff --git a/pyproject.toml b/pyproject.toml index 91c3f0e..ea5ae0b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "fabelous_math" -version = "0.0.1" +version = "0.2.0" description = "Math functions written in C++ for faster code" authors = [ {name = "Falko Habel", email = "falko.habel@fabelous.app"} diff --git a/setup.py b/setup.py index c59254f..6f6d988 100644 --- a/setup.py +++ b/setup.py @@ -10,11 +10,23 @@ if platform.system() == "Windows": else: extra_compile_args.append('-std=c++17') -module = Extension( +simple_functions_module = Extension( 'fabelous_math.simple_functions', sources=[ 'src/fabelous_math/cpp/functions/simple_functions.cpp', - 'src/fabelous_math/cpp/functions/bindings.cpp' + 'src/fabelous_math/cpp/functions/bindings/simple_functions_bindings.cpp' + ], + include_dirs=['src/fabelous_math/include'], + extra_compile_args=extra_compile_args, + extra_link_args=extra_link_args, +) + + +sqrt_module = Extension( + 'fabelous_math.sqrt', + sources=[ + 'src/fabelous_math/cpp/functions/sqrt.cpp', + 'src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp' ], include_dirs=['src/fabelous_math/include'], extra_compile_args=extra_compile_args, @@ -24,7 +36,7 @@ module = Extension( setup( name='fabelous_math', description='Math functions written in C++ for faster code', - ext_modules=[module], + ext_modules=[simple_functions_module, sqrt_module], author="Falko Habel", author_email="falko.habel@fabelous.app" ) \ No newline at end of file diff --git a/src/fabelous_math/cpp/functions/bindings.cpp b/src/fabelous_math/cpp/functions/bindings/simple_functions_bindings.cpp similarity index 82% rename from src/fabelous_math/cpp/functions/bindings.cpp rename to src/fabelous_math/cpp/functions/bindings/simple_functions_bindings.cpp index aef16dc..0eac404 100644 --- a/src/fabelous_math/cpp/functions/bindings.cpp +++ b/src/fabelous_math/cpp/functions/bindings/simple_functions_bindings.cpp @@ -17,20 +17,20 @@ static PyObject* is_odd_wrapper(PyObject* self, PyObject* args) { return PyBool_FromLong(simple_functions::is_odd(number)); } -static PyMethodDef NumberUtilsMethods[] = { +static PyMethodDef SimpleFunctionsMethods[] = { {"is_even", is_even_wrapper, METH_VARARGS, "Check if a number is even"}, {"is_odd", is_odd_wrapper, METH_VARARGS, "Check if a number is odd"}, {NULL, NULL, 0, NULL} }; -static struct PyModuleDef number_utils_module = { +static struct PyModuleDef simple_functions_module = { PyModuleDef_HEAD_INIT, "simple_functions", "Module for checking if numbers are even or odd", -1, - NumberUtilsMethods + SimpleFunctionsMethods }; PyMODINIT_FUNC PyInit_simple_functions(void) { - return PyModule_Create(&number_utils_module); -} \ No newline at end of file + return PyModule_Create(&simple_functions_module); +} diff --git a/src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp b/src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp new file mode 100644 index 0000000..adf5436 --- /dev/null +++ b/src/fabelous_math/cpp/functions/bindings/sqrt_bindings.cpp @@ -0,0 +1,34 @@ +#include +#include "sqrt.hpp" +#include + +static PyObject* fabelous_sqrt_wrapper(PyObject* self, PyObject* args) { + long long number; + if (!PyArg_ParseTuple(args, "L", &number)) { // Ensure 'L' is correct for long long + return NULL; + } + try { + double result = fabelous_sqrt::sqrt(number); // Adjust to match the actual return type + return PyFloat_FromDouble(result); + } catch (const std::invalid_argument& e) { // Ensure this is the correct exception + PyErr_SetString(PyExc_ValueError, e.what()); + return NULL; + } +} + +static PyMethodDef SqrtMethods[] = { + {"fabelous_sqrt", fabelous_sqrt_wrapper, METH_VARARGS, "Compute the square root of a long number"}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef sqrt_module = { + PyModuleDef_HEAD_INIT, + "sqrt", + "Module for computing the square root of a number", + -1, + SqrtMethods +}; + +PyMODINIT_FUNC PyInit_sqrt(void) { + return PyModule_Create(&sqrt_module); +} \ No newline at end of file diff --git a/src/fabelous_math/cpp/functions/sqrt.cpp b/src/fabelous_math/cpp/functions/sqrt.cpp new file mode 100644 index 0000000..d0d4899 --- /dev/null +++ b/src/fabelous_math/cpp/functions/sqrt.cpp @@ -0,0 +1,21 @@ +#include "sqrt.hpp" +#include +#include + +double fabelous_sqrt::sqrt(long long number) { + if (number < 0) { + throw std::invalid_argument("Cannot compute square root of a negative number"); + } + // Special case for 0 and 1 + if (number == 0 || number == 1) { + return static_cast(number); + } + + long long x = number; + double y = static_cast((x + 1) / 2); + while (y < x) { + x = static_cast(y); + y = (static_cast(x) + static_cast(number) / static_cast(x)) / 2; + } + return y; +} \ No newline at end of file diff --git a/src/fabelous_math/include/sqrt.hpp b/src/fabelous_math/include/sqrt.hpp new file mode 100644 index 0000000..bdd2588 --- /dev/null +++ b/src/fabelous_math/include/sqrt.hpp @@ -0,0 +1,5 @@ +#pragma once + +namespace fabelous_sqrt { + double sqrt(long long number); +} \ No newline at end of file