-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqueeze_bits.R
37 lines (34 loc) · 956 Bytes
/
squeeze_bits.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
clamp <- function(x, a, b) {
min(b, max(a, x))
}
squeeze_bits <- function(x, digits, method='trim', decimal=FALSE) {
stopifnot(is.numeric(x))
stopifnot(is.numeric(digits))
if (method == 'trim') {
if (decimal) {
.Call("C_trim_bits_dd", as.double(x), as.integer(digits))
} else {
.Call("C_trim_bits_sd", as.double(x), as.integer(digits))
}
} else if (method == 'pad') {
if (decimal) {
.Call("C_pad_bits_dd", as.double(x), as.integer(digits))
} else {
.Call("C_pad_bits_sd", as.double(x), as.integer(digits))
}
} else if (method == 'groom') {
if (decimal) {
.Call("C_groom_bits_dd", as.double(x), as.integer(digits))
} else {
.Call("C_groom_bits_sd", as.double(x), as.integer(digits))
}
}
}
bits_as_string <- function(d) {
bits <- .Call("C_double_bits", as.double(d))
paste0(
substr(bits, 1, 1), " ",
substr(bits, 2, 12), " ",
substr(bits, 13, 64)
)
}