Source code for phantom.predicates.numeric

from .base import Predicate
from .boolean import negate
from .generic import equal


[docs]def less(n: float) -> Predicate[float]: """ Create a new predicate that succeeds when its argument is strictly less than ``n``. """ def check(value: float, /) -> bool: return value < n return check
[docs]def le(n: float) -> Predicate[float]: """ Create a new predicate that succeeds when its argument is less than or equal to ``n``. """ def check(value: float, /) -> bool: return value <= n return check
[docs]def greater(n: float) -> Predicate[float]: """ Create a new predicate that succeeds when its argument is strictly greater than ``n``. """ def check(value: float, /) -> bool: return value > n return check
[docs]def ge(n: float) -> Predicate[float]: """ Create a new predicate that succeeds when its argument is greater than or equal to ``n``. """ def check(value: float) -> bool: return value >= n return check
[docs]def positive(n: float, /) -> bool: """Return :py:const:`True` when ``n`` is strictly greater than zero.""" return greater(0)(n)
[docs]def non_positive(n: float, /) -> bool: """Return :py:const:`True` when ``n`` is less than or equal to zero.""" return le(0)(n)
[docs]def negative(n: float, /) -> bool: """Return :py:const:`True` when ``n`` is strictly less than zero.""" return less(0)(n)
[docs]def non_negative(n: float, /) -> bool: """Return :py:const:`True` when ``n`` is greater than or equal to zero.""" return ge(0)(n)
[docs]def modulo(n: float, p: Predicate[float]) -> Predicate[float]: """ Create a new predicate that succeeds when its argument modulo ``n`` satisfies the given predicate ``p``. """ def check(value: float, /) -> bool: return p(value % n) return check
[docs]def even(n: int, /) -> bool: """Return :py:const:`True` when ``n`` is even.""" return modulo(2, equal(0))(n)
[docs]def odd(n: int, /) -> bool: """Return :py:const:`True` when ``n`` is odd.""" return negate(even)(n)