///|
fn float_pcm_width(record : @mseed.Record) -> Int32? raise WaveformError {
  if record.sample_count == 0 {
    return None
  }
  let width = match record.encoding {
    4 => 4
    5 => 8
    _ => return None
  }
  check_pcm_extent(record, width, "floating-point")
  Some(width)
}

///|
/// Read signed PCM, Steim, or IEEE binary32/binary64 samples as doubles.
/// Integers and finite binary32 values widen exactly. Floating-point
/// samples retain signed zero, infinities and NaN.
pub fn samples_f64(record : @mseed.Record) -> Array[Double] raise WaveformError {
  samples_f64_range(record, 0, record.sample_count)
}

///|
/// Read indices in `[start, end)` as doubles. The entire PCM extent or
/// Steim integration through Xn is checked, including for an empty range.
pub fn samples_f64_range(
  record : @mseed.Record,
  start : Int32,
  end : Int32,
) -> Array[Double] raise WaveformError {
  guard float_pcm_width(record) is Some(width) else {
    return samples_range(record, start, end).map(Int32::to_double)
  }
  check_sample_range(start, end, record.sample_count)
  let data = record.payload
  // The full PCM extent and the requested indices have both been checked.
  Array::makei(end - start, i => {
    let offset = (start + i) * width
    if width == 4 {
      let bits = if record.little_endian {
        data.unsafe_read_uint32_le(offset)
      } else {
        data.unsafe_read_uint32_be(offset)
      }
      Float::reinterpret_from_uint(bits).to_double()
    } else {
      let bits = if record.little_endian {
        data.unsafe_read_uint64_le(offset)
      } else {
        data.unsafe_read_uint64_be(offset)
      }
      bits.reinterpret_as_double()
    }
  })
}