Skip to content

Mathematical Functions

Mathematical utility functions for number formatting and logarithm calculations.

rndsf

rndsf(x: Union[int, float], sf: int, zero: Optional[float] = None) -> str

Round number to specified significant figures.

Parameters:

  • x

    (Union[int, float]) –

    Number to round.

  • sf

    (int) –

    Number of significant figures (2-10).

  • zero

    (Optional[float], default: None ) –

    Optional zero threshold (default: 10^-sf).

Returns:

  • str

    Formatted string representation with specified significant figures.

Raises:

  • TypeError

    If arguments have invalid types.

  • ValueError

    If arguments have invalid values.

Examples:

>>> rndsf(1.234567, 3)
'1.23'
>>> rndsf(0.001234, 3)
'0.00123'
>>> rndsf(1234567, 3)
'1230000'

ln

ln(x: float, base: Union[int, str] = 'e') -> float

Return logarithm to specified base.

Parameters:

  • x

    (float) –

    Number to obtain logarithm of.

  • base

    (Union[int, str], default: 'e' ) –

    Base to use - 2, 10, or 'e' for natural logarithm.

Returns:

  • float

    Logarithm of x to the specified base.

Raises:

  • TypeError

    If arguments have invalid types.

  • ValueError

    If arguments have invalid values.

Example

ln(10, 10) 1.0 ln(8, 2) 3.0 ln(2.718281828459045) # e 1.0

Usage Examples

Significant Figure Rounding

from causaliq_core.utils import rndsf

# Basic rounding
rndsf(1.23456789, 4)  # Returns '1.235'

# Small numbers
rndsf(0.000123, 3)    # Returns '0.000123'

# Large numbers  
rndsf(1234567, 3)     # Returns '1230000.0'

# Custom zero threshold
rndsf(0.0001, 3, zero=0.001)  # Returns '0.0'

Logarithm Calculations

from causaliq_core.utils import ln

# Base 10 logarithm
ln(10, 10)  # Returns 1.0

# Base 2 logarithm
ln(8, 2)    # Returns 3.0

# Natural logarithm (default)
ln(2.718281828459045)  # Returns 1.0
ln(2.718281828459045, 'e')  # Same as above