toolregistry.hub.calculator module¶
Calculator module providing mathematical calculation functions.
This module contains the Calculator class which provides a comprehensive set of mathematical operations including:
Constants: pi, e, tau, inf, nan
Basic arithmetic: add, subtract, multiply, divide, mod
Power and roots: power, sqrt, cbrt, isqrt
Distance and norm: dist, hypot
Trigonometric functions: sin, cos, tan, asin, acos, atan, degrees, radians
Hyperbolic functions: sinh, cosh, tanh, asinh, acosh, atanh
Logarithmic and exponential functions: log, ln, log10, log2, log1p, exp, expm1
Numerical processing: abs, round, floor, ceil, trunc, copysign, frexp, ldexp, modf, remainder, nextafter, ulp, fmod, isclose
Combinatorics: factorial, gcd, lcm, comb, perm
Special functions: erf, erfc, gamma, lgamma
Numerical validation: isfinite, isinf, isnan
Statistical functions: average, median, mode, standard_deviation, min, max, sum, prod, fsum
Financial calculations: simple_interest, compound_interest
Random number generation: random, randint
Expression evaluation: evaluate (supports expressions combining above functions)
Example
>>> from toolregistry.hub import Calculator
>>> calc = Calculator()
>>> calc.add(1, 2)
3
>>> calc.evaluate("add(2, 3) * power(2, 3) + sqrt(16)")
44
- class toolregistry.hub.calculator.Calculator[source]¶
Bases:
object
Performs mathematical calculations.
This class provides a unified interface for a wide range of mathematical operations, including basic arithmetic, scientific functions, statistical calculations, financial computations, random number generation, and expression evaluation.
- Constants()¶
pi, e, tau, inf, nan
- Basic arithmetic
add, subtract, multiply, divide, mod
- Power and roots
power, sqrt, cbrt, isqrt
- Distance and norm
dist, hypot
- Trigonometric functions
sin, cos, tan, asin, acos, atan, degrees, radians
- Hyperbolic functions
sinh, cosh, tanh, asinh, acosh, atanh
- Logarithmic and exponential functions
log, ln, log10, log2, log1p, exp, expm1
- Numerical processing
abs, round, floor, ceil, trunc, copysign, frexp, ldexp, modf, remainder, nextafter, ulp, fmod, isclose
- Combinatorics()¶
factorial, gcd, lcm, comb, perm
- Special functions
erf, erfc, gamma, lgamma
- Numerical validation
isfinite, isinf, isnan
- Statistical functions
average, median, mode, standard_deviation, min, max, sum, prod, fsum
- Financial calculations
simple_interest, compound_interest
- Random number generation
random, randint
- Expression evaluation
evaluate
- static dist(p: List[float], q: List[float]) float [source]¶
Calculates Euclidean distance between two points.
- static acosh(x: float) float [source]¶
Calculates the inverse hyperbolic cosine of x. x must be >= 1.
- static atanh(x: float) float [source]¶
Calculates the inverse hyperbolic tangent of x. |x| must be less than 1.
- static copysign(a: float, b: float) float [source]¶
Returns a float with the magnitude of a but the sign of b.
- static nextafter(x: float, y: float) float [source]¶
Returns next floating-point value after x towards y.
- static isclose(a: float, b: float, rel_tol: float = 1e-09, abs_tol: float = 0.0) bool [source]¶
Determines whether two floats are close in value.
- static perm(n: int, k: int) int [source]¶
Calculates the number of permutations of n items taken k at a time.
- static lgamma(x: float) float [source]¶
Calculates the natural logarithm of the absolute value of the Gamma function of x.
- static standard_deviation(numbers: List[float]) float [source]¶
Calculates population standard deviation of numbers.
- static fsum(numbers: List[float]) float [source]¶
Calculates an accurate floating point sum of numbers.
- static simple_interest(principal: float, rate: float, time: float) float [source]¶
Calculates simple interest.
- Parameters:
principal (float) – Initial amount
rate (float) – Annual interest rate (decimal)
time (float) – Time in years
- Returns:
Simple interest amount
- Return type:
float
- static compound_interest(principal: float, rate: float, time: float, periods: int = 1) float [source]¶
Calculates compound interest.
- Parameters:
principal (float) – Initial amount
rate (float) – Annual interest rate (decimal)
time (float) – Time in years
periods (int, optional) – Compounding periods per year. Defaults to 1.
- Returns:
Final amount after compounding
- Return type:
float
- static evaluate(expression: str) float | int | bool [source]¶
Evaluates a mathematical expression using a unified interface.
This method is intended for complex expressions that combine two or more operations. For simple, single-step operations, please directly use the corresponding static method (e.g., add, subtract).
- The evaluate method supports the following operations:
Constants: pi, e, tau, inf, nan
Basic arithmetic: add, subtract, multiply, divide, mod
Power and roots: power, sqrt, cbrt, isqrt
Distance and norm: dist, hypot
Trigonometric functions: sin, cos, tan, asin, acos, atan, degrees, radians
Hyperbolic functions: sinh, cosh, tanh, asinh, acosh, atanh
Logarithmic and exponential functions: log, ln, log10, log2, log1p, exp, expm1
Numerical processing: abs, round, floor, ceil, trunc, copysign, frexp, ldexp, modf, remainder, nextafter, ulp, fmod, isclose
Combinatorics: factorial, gcd, lcm, comb, perm
Special functions: erf, erfc, gamma, lgamma
Numerical validation: isfinite, isinf, isnan
Statistical functions: average, median, mode, standard_deviation, min, max, sum, prod, fsum
Financial calculations: simple_interest, compound_interest
Random number generation: random, randint
The expression should be a valid Python expression utilizing the above functions. For example: “add(2, 3) * power(2, 3) + sqrt(16)”.
- Parameters:
expression (str) – Mathematical expression to evaluate.
- Returns:
The result of the evaluated expression.
- Return type:
Union[float, int, bool]
- Raises:
ValueError – If the expression is invalid or its evaluation fails.