addded a custom sqrt function to the code.
This commit is contained in:
parent
150d450965
commit
e849593530
|
@ -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))
|
||||
print(is_odd(19))
|
||||
|
|
|
@ -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"}
|
||||
|
|
18
setup.py
18
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"
|
||||
)
|
|
@ -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);
|
||||
}
|
||||
return PyModule_Create(&simple_functions_module);
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
#include <Python.h>
|
||||
#include "sqrt.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
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);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#include "sqrt.hpp"
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
|
||||
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<double>(number);
|
||||
}
|
||||
|
||||
long long x = number;
|
||||
double y = static_cast<double>((x + 1) / 2);
|
||||
while (y < x) {
|
||||
x = static_cast<long long>(y);
|
||||
y = (static_cast<double>(x) + static_cast<double>(number) / static_cast<double>(x)) / 2;
|
||||
}
|
||||
return y;
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
namespace fabelous_sqrt {
|
||||
double sqrt(long long number);
|
||||
}
|
Loading…
Reference in New Issue