Coverage for python/lsst/images/utils.py: 91%
18 statements
« prev ^ index » next coverage.py v7.14.3, created at 2026-06-24 08:46 +0000
« prev ^ index » next coverage.py v7.14.3, created at 2026-06-24 08:46 +0000
1# This file is part of lsst-images.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12from __future__ import annotations
14__all__ = ("is_none", "round_half_away_from_zero", "round_half_down", "round_half_up")
16import math
17import operator
18import sys
19from typing import TYPE_CHECKING
21if TYPE_CHECKING:
22 from typing_extensions import TypeIs
24if sys.version_info >= (3, 14): 24 ↛ 25line 24 didn't jump to line 25 because the condition on line 24 was never true
25 is_none = operator.is_none
26else:
28 def is_none(x: object) -> TypeIs[None]:
29 """Test whether an object is None."""
30 return x is None
33def round_half_up(x: float) -> int:
34 """Round a `float` to an `int`, always rounding half up.
36 Note that Python's built-in `round` implements the "round half to even"
37 strategy. This function implements the strategy used in `lsst.geom.Point`
38 conversions.
39 """
40 return math.floor(x + 0.5)
43def round_half_down(x: float) -> int:
44 """Round a `float` to an `int`, always rounding half down.
46 Note that Python's built-in `round` implements the "round half to even"
47 strategy.
48 """
49 return math.ceil(x - 0.5)
52def round_half_away_from_zero(x: float) -> int:
53 """Round a `float` to an `int`, always rounding away from zero.
55 Note that Python's built-in `round` implements the "round half to even"
56 strategy. This function implements the C/C++ standard strategy.
57 """
58 if x > 0:
59 return math.floor(x + 0.5)
60 else:
61 return math.ceil(x - 0.5)