rbca (Recursive Binary Cosine Approximation) is a trigonometric approximation library with both symbolic and float APIs.
It computes cos, sin, and tan through recursive binary partitioning on angles.
The default APIs keep expressions in exact symbolic form (SymPy Expr) instead of forcing early floating-point evaluation. The _float APIs use float arithmetic internally for practical numeric calculation.
Current package version is 0.1.0.
- Symbolic trig outputs using SymPy expressions
- Practical float trig outputs using float arithmetic
- Recursive midpoint-based approximation for angle intervals
- Angle normalization over full
0~360degree input range - Three API families:
calc_*_pure: mathematically pure symbolic recursion from0to90calc_*: symbolic recursion seeded with well-known anchor angles (0, 15, 30, 45, 60, 75)calc_*_float: practical float calculation using the same anchor strategy ascalc_*
pip install rbcapip install -e .Project requirements:
- Python
>=3.10 sympy>=1.10.0
from rbca import calc_cos, calc_cos_float, calc_sin, calc_sin_float, calc_tan
from sympy import sqrtdenest
depth = 5
cos_expr = calc_cos(37, depth) # SymPy Expr
sin_expr = calc_sin(37, depth) # SymPy Expr
tan_expr = calc_tan(37, depth) # SymPy Expr
cos_value = calc_cos_float(37, depth) # float
sin_value = calc_sin_float(37, depth) # float
print(cos_expr) # symbolic expression
print(float(cos_expr)) # numeric projection
print(sqrtdenest(cos_expr)) # optional radical denesting
print(cos_value) # practical float result- Use
calc_cos_pure,calc_sin_pure, andcalc_tan_purewhen you want the mathematically pure symbolic path.- This path starts from
[0, 90]only.
- This path starts from
- Use
calc_cos,calc_sin, andcalc_tanwhen you want symbolic results with a more practical anchored start.- This path uses anchor angles (
0, 15, 30, 45, 60, 75, 90) to start from a narrower interval.
- This path uses anchor angles (
- Use
calc_cos_float,calc_sin_float, andcalc_tan_floatwhen you want practical float results.- This path is implemented with float arithmetic internally.
-
calc_cos(angle, depth) -> Expr -
calc_sin(angle, depth) -> Expr -
calc_tan(angle, depth) -> Expr -
calc_cos_pure(angle, depth) -> Expr -
calc_sin_pure(angle, depth) -> Expr -
calc_tan_pure(angle, depth) -> Expr -
calc_cos_float(angle, depth) -> float -
calc_sin_float(angle, depth) -> float -
calc_tan_float(angle, depth) -> float
For symbolic APIs, angle accepts Rational | int | float | str and is internally converted with sympy.Rational where needed.
Symbolic input note:
floatinputs are converted throughRational(float_value), which can preserve binary-float artifacts.- For exact decimal intent, prefer
strorRationaldirectly.- Example: use
"0.1"instead of0.1.
- Example: use
For float APIs, angle accepts int | float | str and is internally converted with float(angle).
depth is recursion depth:
depth = 0: return one of the current interval endpoints- larger depth: finer binary partitioning and generally tighter approximation
- Normalize input angle to first quadrant (
0~90) and track output sign. - Choose initial bracket interval:
calc_cos_pure:[0, 90]calc_cos: narrower interval between predefined anchor angles when possible.calc_cos_float: same anchored interval strategy ascalc_cos, but with float nodes and float values.
- Recursively bisect the interval by midpoint angle.
- Compute midpoint cosine via:
- a product/half-angle style expression using endpoint cosine values.
- Recurse until
depth == 0(or exact angle hit), then apply sign flip if needed.
- This library is symbolic-first; expressions can grow quickly as depth increases.
- For angles where cosine is zero (e.g.
90 + 180kdegrees),calc_tanandcalc_tan_purereturnsympy.zoo. - For the same tangent singularities,
calc_tan_floatlets Python'sZeroDivisionErrorpropagate. - Inputs with symbolic free variables are rejected during normalization.
Condition:
Using the half-angle formula,
Also, by the angle addition formula,
Since
we can use
and
Therefore,
Substituting this into the half-angle formula finally gives
The float version uses a different formula that is faster for float arithmetic: it applies the addition formula first, then the half-angle formula.
MIT License. See LICENSE.