Skip to contents

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)

Arguments

num

A numeric value to check.

min

The minimum value of the interval.

max

The maximum value of the interval.

par_name

Optional name of the parameter to display in error messages.

Value

  • check_within_interval(): Returns the original num value if it lies within the interval; otherwise, raises an error.

  • check_is_within_interval(): Returns TRUE if num 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