tolerance#
- class brainstate.environ.tolerance(*, env=None)[source]#
Get numerical tolerance based on current precision.
This function returns an appropriate tolerance value for numerical comparisons based on the current precision setting.
- Parameters:
env (
EnvironmentState|None) – The environment state to query. If None, uses the global environment.- Returns:
Tolerance value as a scalar array.
- Return type:
Array
Examples
>>> import brainstate.environ as env >>> import jax.numpy as jnp >>> >>> # Different tolerances for different precisions >>> env.set(precision=64) >>> tol64 = env.tolerance() >>> print(f"64-bit tolerance: {tol64}") # 1e-12 >>> >>> env.set(precision=32) >>> tol32 = env.tolerance() >>> print(f"32-bit tolerance: {tol32}") # 1e-5 >>> >>> # Use in numerical comparisons >>> def are_close(a, b): ... return jnp.abs(a - b) < env.tolerance()
Using custom environment:
>>> import brainstate.environ as env >>> >>> custom_env = env.EnvironmentState() >>> env.set(precision=64, env=custom_env) >>> print(env.tolerance(env=custom_env)) # 1e-12
Notes
Tolerance values: - 64-bit: 1e-12 - 32-bit: 1e-5 - 16-bit and below: 1e-2