Floating point precision control.

ebcc.core.precision.astype(value, dtype)

Cast a value to the current floating point type.

Parameters:
  • value (T) –

    The value to cast.

  • dtype (Type[T]) –

    The type to cast to.

Returns:
  • T

    The value cast to the current floating point type.

Source code in ebcc/core/precision.py
def astype(value: T, dtype: Type[T]) -> T:
    """Cast a value to the current floating point type.

    Args:
        value: The value to cast.
        dtype: The type to cast to.

    Returns:
        The value cast to the current floating point type.
    """
    if BACKEND == "jax" and not TYPE_CHECKING:
        # Value may be traced, can't cast directly to the type
        return value.astype(types[dtype])  # type: ignore
    else:
        out: T = types[dtype](value)
        return out

ebcc.core.precision.set_precision(**kwargs)

Set the floating point type.

Parameters:
  • float

    The floating point type to use.

  • complex

    The complex type to use.

Source code in ebcc/core/precision.py
def set_precision(**kwargs: type) -> None:
    """Set the floating point type.

    Args:
        float: The floating point type to use.
        complex: The complex type to use.
    """
    types[float] = kwargs.get("float", types[float])
    types[complex] = kwargs.get("complex", types[complex])

ebcc.core.precision.precision(**kwargs)

Context manager for setting the floating point precision.

Parameters:
  • float

    The floating point type to use.

  • complex

    The complex type to use.

Source code in ebcc/core/precision.py
@contextmanager
def precision(**kwargs: type) -> Iterator[None]:
    """Context manager for setting the floating point precision.

    Args:
        float: The floating point type to use.
        complex: The complex type to use.
    """
    old = {
        "float": types[float],
        "complex": types[complex],
    }
    set_precision(**kwargs)
    yield
    set_precision(**old)

ebcc.core.precision.single_precision()

Context manager for setting the floating point precision to single precision.

Source code in ebcc/core/precision.py
@contextmanager
def single_precision() -> Iterator[None]:
    """Context manager for setting the floating point precision to single precision."""
    with precision(float=np.float32, complex=np.complex64):
        yield