Skip to content

Commit 8585373

Browse files
committed
Add a new math module with is_close_to_zero()
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent fe4551b commit 8585373

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/frequenz/core/math.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# License: MIT
2+
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Internal math tools."""
5+
6+
import math
7+
8+
9+
def is_close_to_zero(value: float, abs_tol: float = 1e-9) -> bool:
10+
"""Check if a floating point value is close to zero.
11+
12+
A value of 1e-9 is a commonly used absolute tolerance to balance precision
13+
and robustness for floating-point numbers comparisons close to zero. Note
14+
that this is also the default value for the relative tolerance.
15+
For more technical details, see https://peps.python.org/pep-0485/#behavior-near-zero
16+
17+
Args:
18+
value: the floating point value to compare to.
19+
abs_tol: the minimum absolute tolerance. Defaults to 1e-9.
20+
21+
Returns:
22+
whether the floating point value is close to zero.
23+
"""
24+
zero: float = 0.0
25+
return math.isclose(a=value, b=zero, abs_tol=abs_tol)

0 commit comments

Comments
 (0)