Check if a number lies within a specified interval
Source:R/check_within_interval.R
check-interval.Rd
check_within_interval()
verifies whether a given numeric value lies
within the specified interval [min
, max
] and raises an error
if the value does not satisfy this condition.
Usage
check_within_interval(num, min, max, par_name = "num")
check_is_within_interval(num, min, max)
Value
check_within_interval()
: Returns the originalnum
value if it lies within the interval; otherwise, raises an error.check_is_within_interval()
: ReturnsTRUE
ifnum
lies within the interval; otherwise,FALSE
.
Details
check_is_within_interval()
performs a similar check but returns a
logical result (TRUE
or FALSE
) without raising an error.
Examples
# Examples for check_within_interval()
# ------------------------------------
check_within_interval(5, 1, 10)
#> [1] 5
check_within_interval(3.5, 3, 4)
#> [1] 3.5
try(check_within_interval(15, 1, 10))
#> Error in check_within_interval(15, 1, 10) :
#> Expected 'num' to be between 1 and 10 but got 15
try(check_within_interval(-2, -1, 1))
#> Error in check_within_interval(-2, -1, 1) :
#> Expected 'num' to be between -1 and 1 but got -2
check_within_interval(0, -10, 10)
#> [1] 0
# Examples for check_is_within_interval()
# ---------------------------------------
check_is_within_interval(5, 1, 10)
#> [1] TRUE
check_is_within_interval(3.5, 3, 4)
#> [1] TRUE
check_is_within_interval(15, 1, 10)
#> [1] FALSE
check_is_within_interval(-2, -1, 1)
#> [1] FALSE
check_is_within_interval(0, -10, 10)
#> [1] TRUE