Calculator Tools¶
The calculator tools provide various mathematical calculation functions, from basic arithmetic operations to more complex mathematical functions.
Class Overview¶
The calculator tools mainly include the following classes:
BaseCalculator- Base class providing core mathematical operationsCalculator- Main class providing mathematical calculation functions, including expression evaluation
Usage¶
Basic Usage¶
from toolregistry_hub import Calculator
# Calculate expressions
result = Calculator.evaluate("2 + 3 * 4")
print(result)
# Output: 14
# Get help information
help_info = Calculator.help("sqrt")
print(help_info)
# Output: function: sqrt(x: float) -> float
# square root of a number.
# List all available functions
functions = Calculator.list_allowed_fns(with_help=True)
print(functions)
# Output: {"add": "function: add(a: float, b: float) -> float\n Adds two numbers.", ...}
# (Returns a JSON string containing all available functions and their help information)
Supported Operations¶
The calculator supports the following operations:
Basic Arithmetic¶
- Addition (
add,+) - Subtraction (
subtract,-) - Multiplication (
multiply,*) - Division (
divide,/) - Floor division (
floor_divide,//) - Modulo (
mod,%) - Absolute value (
abs) - Power operation (
pow,**)
Advanced Mathematical Functions¶
- Square root (
sqrt) - Logarithm (
log,ln) - Exponential (
exp) - Trigonometric functions (not implemented)
Statistical Functions¶
- Minimum (
min) - Maximum (
max) - Sum (
sum) - Average (
average) - Median (
median) - Mode (
mode) - Standard deviation (
standard_deviation)
Other Functions¶
- Factorial (
factorial) - Greatest common divisor (
gcd) - Least common multiple (
lcm) - Distance calculation (
dist) - Simple interest calculation (
simple_interest) - Compound interest calculation (
compound_interest)
Detailed API¶
BaseCalculator Class¶
BaseCalculator is a base class that provides core mathematical operations.
Methods¶
add(a: float, b: float) -> float: Returns the sum of two numberssubtract(a: float, b: float) -> float: Returns the difference of two numbersmultiply(a: float, b: float) -> float: Returns the product of two numbersdivide(a: float, b: float) -> float: Returns the quotient of two numbersfloor_divide(a: float, b: float) -> float: Returns the floor division result of two numbersmod(a: float, b: float) -> float: Returns the modulo of two numbersabs(x: float) -> float: Returns the absolute value of a numberpow(base: float, exponent: float) -> float: Returns base raised to the power of exponentsqrt(x: float) -> float: Returns the square root of a numberlog(x: float, base: float = 10) -> float: Returns the logarithm of x with baseln(x: float) -> float: Returns the natural logarithm of xexp(x: float) -> float: Returns e raised to the power of xmin(numbers: List[float]) -> float: Returns the minimum value from a list of numbersmax(numbers: List[float]) -> float: Returns the maximum value from a list of numberssum(numbers: List[float]) -> float: Returns the sum of a list of numbersaverage(numbers: List[float]) -> float: Returns the average of a list of numbersmedian(numbers: List[float]) -> float: Returns the median of a list of numbersmode(numbers: List[float]) -> List[float]: Returns the mode of a list of numbersstandard_deviation(numbers: List[float]) -> float: Returns the standard deviation of a list of numbersfactorial(n: int) -> int: Returns the factorial of ngcd(a: int, b: int) -> int: Returns the greatest common divisor of two numberslcm(a: int, b: int) -> int: Returns the least common multiple of two numbersdist(p1: List[float], p2: List[float], p: int = 2) -> float: Calculates the distance between two pointssimple_interest(principal: float, rate: float, time: float) -> float: Calculates simple interestcompound_interest(principal: float, rate: float, time: float, n: int = 1) -> float: Calculates compound interest
Calculator Class¶
Calculator is the main class that provides mathematical calculation functions, including expression evaluation.
Methods¶
list_allowed_fns(with_help: bool = False) -> str: Lists all allowed functionshelp(fn_name: str | None = None) -> str: Gets help information for a specific function. Whenfn_nameis omitted, returns an overview of all allowed functions.evaluate(expression: str) -> Union[float, int, bool]: Evaluates the value of a mathematical expression
Examples¶
Calculating Expressions¶
from toolregistry_hub import Calculator
# Basic arithmetic
result = Calculator.evaluate("2 + 3 * 4")
print(result) # Output: 14
# Using functions
result = Calculator.evaluate("sqrt(16) + pow(2, 3)")
print(result) # Output: 12.0
# Mixed use of functions and operators
result = Calculator.evaluate("sqrt(16) + 2 ** 3")
print(result) # Output: 12.0
Getting Help Information¶
from toolregistry_hub import Calculator
# Get help information for a specific function
help_info = Calculator.help("sqrt")
print(help_info)
# Output: function: sqrt(x: float) -> float
# square root of a number.
# List all available functions
functions = Calculator.list_allowed_fns(with_help=True)
print(functions)
# Output: {"add": "function: add(a: float, b: float) -> float\n Adds two numbers.", ...}
# (Returns a JSON string containing all available functions and their help information)