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 pi() float[source]

Returns the mathematical constant π.

static e() float[source]

Returns the mathematical constant e.

static tau() float[source]

Returns the mathematical constant tau (2π).

static inf() float[source]

Returns positive infinity.

static nan() float[source]

Returns NaN (Not a Number).

static add(a: float, b: float) float[source]

Adds two numbers.

static subtract(a: float, b: float) float[source]

Subtracts two numbers.

static multiply(a: float, b: float) float[source]

Multiplies two numbers.

static divide(a: float, b: float) float[source]

Divides two numbers.

static mod(a: float, b: float) float[source]

Calculates a modulo b.

static power(base: float, exponent: float) float[source]

Raises base to exponent power.

static sqrt(x: float) float[source]

Calculates square root of a number.

static cbrt(x: float) float[source]

Calculates cube root of a number.

static isqrt(n: int) int[source]

Calculates integer square root of non-negative integer n.

static dist(p: List[float], q: List[float]) float[source]

Calculates Euclidean distance between two points.

static hypot(*coordinates: float) float[source]

Calculates Euclidean norm (sqrt(sum(x^2))).

static sin(x: float) float[source]

Calculates sine of x (in radians).

static cos(x: float) float[source]

Calculates cosine of x (in radians).

static tan(x: float) float[source]

Calculates tangent of x (in radians).

static asin(x: float) float[source]

Calculates arcsine of x (in radians).

static acos(x: float) float[source]

Calculates arccosine of x (in radians).

static atan(x: float) float[source]

Calculates arctangent of x (in radians).

static degrees(x: float) float[source]

Converts angle x from radians to degrees.

static radians(x: float) float[source]

Converts angle x from degrees to radians.

static sinh(x: float) float[source]

Calculates the hyperbolic sine of x.

static cosh(x: float) float[source]

Calculates the hyperbolic cosine of x.

static tanh(x: float) float[source]

Calculates the hyperbolic tangent of x.

static asinh(x: float) float[source]

Calculates the inverse hyperbolic sine of x.

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 log(x: float, base: float = 10) float[source]

Calculates logarithm of x with given base.

static ln(x: float) float[source]

Calculates natural (base-e) logarithm of x.

static log10(x: float) float[source]

Calculates base-10 logarithm of x.

static log2(x: float) float[source]

Calculates base-2 logarithm of x.

static log1p(x: float) float[source]

Calculates the natural logarithm of 1+x.

static exp(x: float) float[source]

Calculates the exponential of x (e^x).

static expm1(x: float) float[source]

Calculates e^x - 1 accurately for small x.

static abs(x: float) float[source]

Calculates absolute value of x.

static round(x: float, digits: int = 0) float[source]

Rounds x to given number of decimal digits.

static floor(x: float) int[source]

Rounds x down to nearest integer.

static ceil(x: float) int[source]

Rounds x up to nearest integer.

static trunc(x: float) int[source]

Truncates x by removing the fractional part.

static copysign(a: float, b: float) float[source]

Returns a float with the magnitude of a but the sign of b.

static frexp(x: float)[source]

Returns the mantissa and exponent of x as the pair (m, e).

static ldexp(x: float, i: int) float[source]

Returns x * (2**i) computed exactly.

static modf(x: float)[source]

Returns the fractional and integer parts of x.

static remainder(x: float, y: float) float[source]

Returns IEEE 754-style remainder of x/y.

static nextafter(x: float, y: float) float[source]

Returns next floating-point value after x towards y.

static ulp(x: float) float[source]

Returns the value of the least significant bit of x.

static fmod(x: float, y: float) float[source]

Returns floating-point remainder of x/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 factorial(n: int) int[source]

Calculates factorial of n.

static gcd(a: int, b: int) int[source]

Calculates greatest common divisor of a and b.

static lcm(a: int, b: int) int[source]

Calculates least common multiple of a and b.

static comb(n: int, k: int) int[source]

Calculates the number of combinations (n choose k).

static perm(n: int, k: int) int[source]

Calculates the number of permutations of n items taken k at a time.

static erf(x: float) float[source]

Calculates the error function of x.

static erfc(x: float) float[source]

Calculates the complementary error function of x.

static gamma(x: float) float[source]

Calculates the Gamma function of x.

static lgamma(x: float) float[source]

Calculates the natural logarithm of the absolute value of the Gamma function of x.

static isfinite(x: float) bool[source]

Checks if x is finite.

static isinf(x: float) bool[source]

Checks if x is infinite.

static isnan(x: float) bool[source]

Checks if x is NaN.

static average(numbers: List[float]) float[source]

Calculates arithmetic mean of numbers.

static median(numbers: List[float]) float[source]

Calculates median of numbers.

static mode(numbers: List[float]) List[float][source]

Finds mode(s) of numbers.

static standard_deviation(numbers: List[float]) float[source]

Calculates population standard deviation of numbers.

static min(numbers: List[float]) float[source]

Finds the minimum value in a list of numbers.

static max(numbers: List[float]) float[source]

Finds the maximum value in a list of numbers.

static sum(numbers: List[float]) float[source]

Calculates the sum of a list of numbers.

static prod(numbers: List[float]) float[source]

Calculates the product of a list 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 random() float[source]

Generates random float between 0 and 1.

static randint(a: int, b: int) int[source]

Generates random integer between a and b.

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.