This function examines a numeric value (typically with numbers after the decimal place) and estimates either:
The number of significant digits of the numerator and denominator of a fraction that would (approximately) result in the given value.
The number of digits to which an integer may have been rounded, depending on whether the input has values after the decimal place or is an integer. Negative values are treated as positive so the negative of a number will returns the same value as its positive version. See Details for more details. Obviously, values can appear to be rounded or repeating even when they are not!
Arguments
- x
Numeric or numeric vector.
- minReps
Integer. Number of times a digit or sequence of digits that occur after a decimal place needs to be repeated to assume it represents a repeating series and thus is assumed to arise from using decimal places to represent a fraction. Default is 3. For example, if
minReps
is 3 then 0.111 would be assumed to represent a repeating value because 1 occurs three times, so -1 would be returned since this decimal can be represented by 1/9 (i.e., division of 1 by a single-digit number). However, ifminReps
is 4 then the function would assume that if the value had had four digits, the next digit would not have been a 1, so returns -3 because there are three significant values after the decimal place. When the penultimate digit is >5 and the last digit is equal to the penultimate digit plus 1, then the last digit counts as a repeat of the penultimate digit. So 0.067 is assumed to have two repeating 6s. IfminReps
is 0 or 1 then the function will (usually) return the negative of the total number of decimal places in the value.
Details
For values with at least one non-zero digit after a decimal place with no repeated series of digits detected, the function simply returns the total number of digits (ignoring trailing zeros) times -1. For example:
0.3 returns -1 because there is just one value after the decimal.
0.34567 returns -5 because there are no repeats up to the 5th decimal place.
0.1212125 returns -7 because there are no repeats (starting from the right) up to the 7th decimal place.
0.111117 returns -6 because there are no repeats (starting from the right) up to the 7th decimal place.
The function takes account of rounding up:
0.666 might be a truncated version of 2/3. Two and three each have 1 significant digit, so the function returns -1 (1 value after the decimal place).
0.667 also returns -1 because this might represent a rounding of 2/3 and it is customary to round digits up if the next digit would have been >5.
0.3334 returns -4 because it is inappropriate to round 3 up to 4 if the next digit would have been 5 or less.
Repeating series are accounted for. For example:
0.121212 returns -2 because "12" starts repeating after the second decimal place.
0.000678678678 returns -6 because "678" starts repeating after the 6th place.
0.678678678 returns -3.
0.678678679 also returns -3 because 678 could be rounded to 679 if the next digit were 6.
Note that you can set the minimum number of times a digit or series needs to be repeated to count as being repeated using the argument minReps
. The default is 3, so digits or series of digits need to be repeated at least 3 times to count a repetition, but this can be changed:
0.1111 returns -1 using the default requirement for 3 repetitions but -4 if the number of minimum repetitions is 5 or more.
0.121212 returns -2 using the default requirement for 3 repetitions but -6 if the number of minimum repetitions is 4 or more.
Trailing zeros are ignored, so 0.12300 returns -3. When values do not have digits after a decimal place the location of the first non-zero digit from the right is returned as a positive integer. For example:
234 returns 1 because the first non-zero digit from the right is in the 1s place.
100 return 3 because the first non-zero digit from the right is in the 100s place.
70001 returns 1 because the first non-zero digit from the right is in the 1s place.
However, note a few oddities:
4E5 returns 6 but 4E50 probably will not return 51 because many computers have a hard time internally representing numbers that large.
4E-5 returns -5 but probably will not return -50 because many computers have a hard time internally representing numbers that small.
-100 and 100 return 3 and -0.12 and 0.12 return -2 because the negative sign is ignored.
0 returns 0.
NA
andNaN
returnsNA
.
Examples
roundedSigDigits(0.3)
#> [1] -1
roundedSigDigits(0.34567)
#> [1] -5
roundedSigDigits(0.1212125)
#> [1] -7
roundedSigDigits(0.111117)
#> [1] -6
roundedSigDigits(0.666)
#> [1] -1
roundedSigDigits(0.667)
#> [1] -1
roundedSigDigits(0.3334)
#> [1] -4
roundedSigDigits(0.121212)
#> [1] -2
roundedSigDigits(0.000678678678)
#> [1] -6
roundedSigDigits(0.678678678)
#> [1] -3
roundedSigDigits(0.678678679)
#> [1] -3
roundedSigDigits(0.1111)
#> [1] -1
roundedSigDigits(0.1111, minReps=5)
#> [1] -4
roundedSigDigits(0.121212)
#> [1] -2
roundedSigDigits(0.121212, minReps=4)
#> [1] -6
roundedSigDigits(234)
#> [1] 1
roundedSigDigits(100)
#> [1] 3
roundedSigDigits(70001)
#> [1] 1
roundedSigDigits(4E5)
#> [1] 6
roundedSigDigits(4E50)
#> [1] 2
roundedSigDigits(4E-5)
#> [1] -5
roundedSigDigits(4E-50)
#> [1] -50
roundedSigDigits(0)
#> [1] 0
roundedSigDigits(NA)
#> [1] NA
x <- c(0.0667, 0.0667, 0.067)
roundedSigDigits(x)
#> [1] -2 -2 -3