R/squeeze_bits.R

Defines functions clamp squeeze_bits bits_as_string

Documented in bits_as_string squeeze_bits

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)
  )
}

Try the bitsqueezr package in your browser

Any scripts or data that you put into this service are public.

bitsqueezr documentation built on March 26, 2020, 8:42 p.m.