Skip to content

constants_functions

shortfx.fxNumeric.constants_functions

Scientific and mathematical constants module.

Provides a curated set of fundamental mathematical and physical constants with full precision. Constants can be queried by name for AI agent integration or used directly as module-level values.

Key Features
  • Mathematical constants (π, e, φ, √2, etc.)
  • Physical constants (speed of light, Planck, Boltzmann, etc.)
  • Named lookup via get_constant() for MCP/AI usage
  • Full catalog via list_constants()
Example

from shortfx.fxNumeric.constants_functions import get_constant, PI get_constant("pi") 3.141592653589793 PI 3.141592653589793

Attributes

ABSOLUTE_ZERO_CELSIUS = -273.15 module-attribute

Absolute zero in Celsius (°C).

AVOGADRO = 6.02214076e+23 module-attribute

Avogadro's number (mol⁻¹).

BOLTZMANN = 1.380649e-23 module-attribute

Boltzmann constant (J/K).

E = math.e module-attribute

Euler's number, the base of the natural logarithm (e ≈ 2.71828).

ELECTRON_MASS = 9.1093837015e-31 module-attribute

Electron mass (kg).

ELEMENTARY_CHARGE = 1.602176634e-19 module-attribute

Elementary charge e (C).

EULER_MASCHERONI = 0.5772156649015329 module-attribute

The Euler-Mascheroni constant (γ ≈ 0.57722).

GAS_CONSTANT = 8.314462618 module-attribute

Ideal gas constant R (J/(mol·K)).

GRAVITATIONAL = 6.6743e-11 module-attribute

Newtonian gravitational constant G (m³/(kg·s²)).

GRAVITY = 9.80665 module-attribute

Standard acceleration of gravity g (m/s²).

INF = math.inf module-attribute

Positive infinity.

LN10 = math.log(10) module-attribute

The natural logarithm of 10 (ln(10) ≈ 2.30259).

LN2 = math.log(2) module-attribute

The natural logarithm of 2 (ln(2) ≈ 0.69315).

LOG10E = math.log10(math.e) module-attribute

The base-10 logarithm of e (log₁₀(e) ≈ 0.43429).

LOG2E = math.log2(math.e) module-attribute

The base-2 logarithm of e (log₂(e) ≈ 1.44270).

NAN = math.nan module-attribute

Not a Number (NaN).

PHI = (1 + math.sqrt(5)) / 2 module-attribute

The golden ratio (φ ≈ 1.61803).

PI = math.pi module-attribute

The ratio of a circle's circumference to its diameter (π ≈ 3.14159).

PLANCK = 6.62607015e-34 module-attribute

Planck constant (J·s).

PROTON_MASS = 1.67262192369e-27 module-attribute

Proton mass (kg).

REDUCED_PLANCK = 1.054571817e-34 module-attribute

Reduced Planck constant ℏ = h/(2π) (J·s).

SPEED_OF_LIGHT = 299792458.0 module-attribute

Speed of light in vacuum (m/s).

SQRT2 = math.sqrt(2) module-attribute

The square root of 2 (√2 ≈ 1.41421).

SQRT3 = math.sqrt(3) module-attribute

The square root of 3 (√3 ≈ 1.73205).

STEFAN_BOLTZMANN = 5.670374419e-08 module-attribute

Stefan-Boltzmann constant σ (W/(m²·K⁴)).

TAU = math.tau module-attribute

The ratio of a circle's circumference to its radius (τ = 2π ≈ 6.28318).

VACUUM_PERMEABILITY = 1.25663706212e-06 module-attribute

Vacuum permeability μ₀ (H/m).

VACUUM_PERMITTIVITY = 8.8541878128e-12 module-attribute

Vacuum permittivity ε₀ (F/m).

Functions

get_constant(name: str) -> float

Returns the value of a scientific or mathematical constant by name.

Description

Looks up a constant from the built-in catalog of mathematical and physical constants. Names are case-insensitive.

Parameters:

Name Type Description Default
name str

The name of the constant (e.g. "pi", "avogadro", "c").

required

Returns:

Name Type Description
float float

The numeric value of the constant.

Raises:

Type Description
TypeError

If name is not a string.

KeyError

If the constant name is not found.

Usage Example

from shortfx.fxNumeric.constants_functions import get_constant get_constant("pi") 3.141592653589793 get_constant("avogadro") 6.02214076e+23 get_constant("speed_of_light") 299792458.0

Cost: O(1)

Source code in shortfx/fxNumeric/constants_functions.py
def get_constant(name: str) -> float:
    """Returns the value of a scientific or mathematical constant by name.

    Description:
        Looks up a constant from the built-in catalog of mathematical and
        physical constants. Names are case-insensitive.

    Args:
        name (str): The name of the constant (e.g. "pi", "avogadro", "c").

    Returns:
        float: The numeric value of the constant.

    Raises:
        TypeError: If name is not a string.
        KeyError: If the constant name is not found.

    Usage Example:
        >>> from shortfx.fxNumeric.constants_functions import get_constant
        >>> get_constant("pi")
        3.141592653589793
        >>> get_constant("avogadro")
        6.02214076e+23
        >>> get_constant("speed_of_light")
        299792458.0

    Cost: O(1)
    """
    if not isinstance(name, str):
        raise TypeError("Constant name must be a string.")

    key = name.strip().lower()

    if key not in _CONSTANTS:
        available = ", ".join(sorted(_CONSTANTS.keys()))
        raise KeyError(
            f"Unknown constant '{name}'. Available: {available}"
        )

    return _CONSTANTS[key][0]

list_constants(category: Optional[str] = None) -> list[dict[str, str]]

Lists all available scientific and mathematical constants.

Description

Returns a catalog of all constants with their name, symbol, value, and description. Optionally filter by category.

Parameters:

Name Type Description Default
category Optional[str]

Filter by "math" or "physical". None returns all constants.

None

Returns:

Type Description
list[dict[str, str]]

list[dict[str, str]]: List of constant info dicts with keys name, symbol, value, description.

Raises:

Type Description
ValueError

If category is not "math", "physical", or None.

Usage Example

from shortfx.fxNumeric.constants_functions import list_constants math_consts = list_constants("math") len(math_consts) > 0 True

Cost: O(n) where n is the number of constants.

Source code in shortfx/fxNumeric/constants_functions.py
def list_constants(category: Optional[str] = None) -> list[dict[str, str]]:
    """Lists all available scientific and mathematical constants.

    Description:
        Returns a catalog of all constants with their name, symbol,
        value, and description. Optionally filter by category.

    Args:
        category (Optional[str]): Filter by "math" or "physical".
            None returns all constants.

    Returns:
        list[dict[str, str]]: List of constant info dicts with keys
            name, symbol, value, description.

    Raises:
        ValueError: If category is not "math", "physical", or None.

    Usage Example:
        >>> from shortfx.fxNumeric.constants_functions import list_constants
        >>> math_consts = list_constants("math")
        >>> len(math_consts) > 0
        True

    Cost: O(n) where n is the number of constants.
    """
    _MATH_KEYS = {
        "pi", "e", "tau", "phi", "sqrt2", "sqrt3", "ln2", "ln10",
        "log2e", "log10e", "inf", "euler_mascheroni",
    }
    _PHYSICAL_KEYS = {
        "speed_of_light", "c", "planck", "h", "hbar", "boltzmann",
        "avogadro", "gas_constant", "gravitational", "gravity", "g",
        "elementary_charge", "electron_mass", "proton_mass",
        "stefan_boltzmann", "vacuum_permittivity", "vacuum_permeability",
        "absolute_zero",
    }

    if category is not None:
        cat = category.strip().lower()

        if cat not in ("math", "physical"):
            raise ValueError("Category must be 'math', 'physical', or None.")

        allowed = _MATH_KEYS if cat == "math" else _PHYSICAL_KEYS
    else:
        allowed = None

    result = []

    for key, (value, symbol, description) in sorted(_CONSTANTS.items()):

        if allowed is not None and key not in allowed:
            continue

        result.append({
            "name": key,
            "symbol": symbol,
            "value": str(value),
            "description": description,
        })

    return result