Skip to content

Commit fb700b8

Browse files
committed
Update
1 parent d941d4a commit fb700b8

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed

highpymath/__init__.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,137 @@ def linear(a: any = None, b: any = None, c: any = None, search_a: bool = False,
277277
if isinstance(b, int):
278278
b = float(b)
279279
_result = _linear_base_c(a=a, b=b)
280+
if return_int:
281+
_result = int(_result)
282+
elif return_float:
283+
_result = float(_result)
284+
if return_string:
285+
_result = str(_result)
286+
return _result
287+
288+
def sin(a: any, return_int: bool = False, return_string: bool = False):
289+
"""
290+
Create the Sinus of a Number.
291+
"""
292+
from .highpymath import sin as _sin
293+
return_float = True
294+
if return_int:
295+
return_float = False
296+
if not isinstance(a, (int, float)):
297+
raise MathValueError("a must be a number")
298+
if isinstance(a, int):
299+
a = float(a)
300+
_result = _sin(a=a)
301+
if return_int:
302+
_result = int(_result)
303+
elif return_float:
304+
_result = float(_result)
305+
if return_string:
306+
_result = str(_result)
307+
return _result
308+
309+
def cos(a: any, return_int: bool = False, return_string: bool = False):
310+
"""
311+
Create the Cosinus of a Number.
312+
"""
313+
from .highpymath import cos as _cos
314+
return_float = True
315+
if return_int:
316+
return_float = False
317+
if not isinstance(a, (int, float)):
318+
raise MathValueError("a must be a number")
319+
if isinstance(a, int):
320+
a = float(a)
321+
_result = _cos(a=a)
322+
if return_int:
323+
_result = int(_result)
324+
elif return_float:
325+
_result = float(_result)
326+
if return_string:
327+
_result = str(_result)
328+
return _result
329+
330+
def tan(a: any, return_int: bool = False, return_string: bool = False):
331+
"""
332+
Create the Tanus of a Number.
333+
"""
334+
from .highpymath import tan as _tan
335+
return_float = True
336+
if return_int:
337+
return_float = False
338+
if not isinstance(a, (int, float)):
339+
raise MathValueError("a must be a number")
340+
if isinstance(a, int):
341+
a = float(a)
342+
_result = _tan(a=a)
343+
if return_int:
344+
_result = int(_result)
345+
elif return_float:
346+
_result = float(_result)
347+
if return_string:
348+
_result = str(_result)
349+
return _result
350+
351+
def asin(a: any, return_int: bool = False, return_string: bool = False):
352+
"""
353+
Create the Arcus Sinus of a Number.
354+
"""
355+
from .highpymath import asin as _asin
356+
return_float = True
357+
if return_int:
358+
return_float = False
359+
if not isinstance(a, (int, float)):
360+
raise MathValueError("a must be a number")
361+
if isinstance(a, int):
362+
a = float(a)
363+
_result = _asin(a=a)
364+
if return_int:
365+
_result = int(_result)
366+
elif return_float:
367+
_result = float(_result)
368+
if return_string:
369+
_result = str(_result)
370+
return _result
371+
372+
def acos(a: any, return_int: bool = False, return_string: bool = False):
373+
"""
374+
Create the Arcus Cosinus of a Number.
375+
"""
376+
from .highpymath import acos as _acos
377+
return_float = True
378+
if return_int:
379+
return_float = False
380+
if not isinstance(a, (int, float)):
381+
raise MathValueError("a must be a number")
382+
if isinstance(a, int):
383+
a = float(a)
384+
_result = _acos(a=a)
385+
if return_int:
386+
_result = int(_result)
387+
elif return_float:
388+
_result = float(_result)
389+
if return_string:
390+
_result = str(_result)
391+
return _result
392+
393+
def atan(a: any, use_leibniz: bool = False, return_int: bool = False, return_string: bool = False):
394+
"""
395+
Create the Arcus Tanus of a Number.
396+
"""
397+
from .highpymath import atan as _atan1
398+
from .highpymath import arctan as _atan2
399+
return_float = True
400+
if return_int:
401+
return_float = False
402+
if use_leibniz:
403+
_atan = _atan2
404+
else:
405+
_atan = _atan1
406+
if not isinstance(a, (int, float)):
407+
raise MathValueError("a must be a number")
408+
if isinstance(a, int):
409+
a = float(a)
410+
_result = _atan(a=a)
280411
if return_int:
281412
_result = int(_result)
282413
elif return_float:

src/lib.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,55 @@ fn linear_base_a(b: PyFloat, c: PyFloat) -> PyResult<PyFloat> {
110110
Ok(c - b)
111111
}
112112

113+
#[pyfunction]
114+
fn sin(a: PyFloat) -> PyResult<PyFloat> {
115+
Ok(a.sin())
116+
}
117+
118+
#[pyfunction]
119+
fn cos(a: PyFloat) -> PyResult<PyFloat> {
120+
Ok(a.cos())
121+
}
122+
123+
#[pyfunction]
124+
fn tan(a: PyFloat) -> PyResult<PyFloat> {
125+
Ok(a.tan())
126+
}
127+
128+
#[pyfunction]
129+
fn asin(a: PyFloat) -> PyResult<PyFloat> {
130+
Ok(a.asin())
131+
}
132+
133+
#[pyfunction]
134+
fn acos(a: PyFloat) -> PyResult<PyFloat> {
135+
Ok(a.acos())
136+
}
137+
138+
#[pyfunction]
139+
fn atan(a: PyFloat) -> PyResult<PyFloat> {
140+
Ok(a.atan())
141+
}
142+
143+
#[pyfunction]
144+
fn arctan(x: PyFloat) -> PyResult<PyFloat> {
145+
/// Berechne den arcus tangens Wert mit der Leibniz-Reihe
146+
let mut result: PyFloat = 0.0;
147+
let mut term: PyFloat = x; // Startterm x^1 / 1
148+
let mut i: PyInt = 1;
149+
150+
while term.abs() > 1e-15 {
151+
if i % 2 == 1 {
152+
result += term; // Addiere ungerade Terme
153+
} else {
154+
result -= term; // Subtrahiere gerade Terme
155+
}
156+
i += 1;
157+
term = term * x * x / ((2 * i - 1) as PyFloat) * ((2 * i - 2) as PyFloat); // Update term zu x^(2i+1) / (2i+1)
158+
}
159+
Ok(result)
160+
}
161+
113162
/// A Python module implemented in Rust.
114163
#[pymodule]
115164
fn highpymath(m: &PyModule) -> PyResult<()> {
@@ -125,6 +174,13 @@ fn highpymath(m: &PyModule) -> PyResult<()> {
125174
m.add_function(wrap_pyfunction!(linear_base_c, m)?)?;
126175
m.add_function(wrap_pyfunction!(linear_base_b, m)?)?;
127176
m.add_function(wrap_pyfunction!(linear_base_a, m)?)?;
177+
m.add_function(wrap_pyfunction!(sin, m)?)?;
178+
m.add_function(wrap_pyfunction!(cos, m)?)?;
179+
m.add_function(wrap_pyfunction!(tan, m)?)?;
180+
m.add_function(wrap_pyfunction!(asin, m)?)?;
181+
m.add_function(wrap_pyfunction!(acos, m)?)?;
182+
m.add_function(wrap_pyfunction!(atan, m)?)?;
183+
m.add_function(wrap_pyfunction!(arctan, m)?)?;
128184
m.add("MathValueError", m.py().get_type::<MathValueError>())?;
129185
Ok(())
130186
}

0 commit comments

Comments
 (0)