Skip to contents

stm_digits_from_tol() calculates the number of decimal digits required either based on a specified numeric tolerance or by analyzing the maximum number of decimal places in a given numeric vector.

Usage

stm_digits_from_tol(vec, tol = NULL)

Arguments

vec

A numeric vector from which to calculate the number of decimal digits. This vector is analyzed to determine the maximum number of decimal places if tol is NULL or invalid.

tol

Numeric tolerance used to determine the number of decimal digits. Default is 1e-16, which corresponds to 16 decimal points. If tol is invalid (e.g., negative, non-numeric, or NULL), the function will analyze the vector directly to get the maximum number of decimal points.

This function is particularly useful in numerical computations where precise control over decimal representation is needed, such as in rounding or formatting operations.

If a tolerance value is provided, the number of decimal digits is determined as the ceiling of the negative logarithm (base 10) of the tolerance. If the tolerance is NULL or invalid, the function computes the maximum decimal places directly from the numeric vector.

Value

An integer representing the number of decimal digits required. This value is based on the tolerance if provided, or on the vector's maximum decimal places if not.

Examples

vec1 <- c(1.234, 2.56, 3.4859)
print(vec1)
#> [1] 1.2340 2.5600 3.4859
stm_digits_from_tol(vec1)
#> [1] 4

vec2 <- c(1.01, 2.001, 3.0001)
print(vec2)
#> [1] 1.0100 2.0010 3.0001
stm_digits_from_tol(vec2, tol = 1e-4)
#> [1] 4

vec3 <- c(10, 20, 30)
print(vec3)
#> [1] 10 20 30
stm_digits_from_tol(vec3)
#> [1] 0

vec4 <- c(0.123456789, 0.987654321)
print(vec4)
#> [1] 0.1234568 0.9876543
stm_digits_from_tol(vec4, tol = 1e-10)
#> [1] 10

vec5 <- c(100, 200.5867, 300.000000)
print(vec5)
#> [1] 100.0000 200.5867 300.0000
stm_digits_from_tol(vec5)
#> [1] 4