Tin

Examples · 20 of 339

20-timestamps

i64 is a 64-bit integer, for values that outgrow int's 32 bits: nanosecond timestamps, byte counts, big counters.

examples/20-timestamps.tintin run examples/20-timestamps.tin
// i64 is a 64-bit integer, for values that outgrow int's 32 bits: nanosecond
// timestamps, byte counts, big counters. It is checked like int: overflow
// panics, and int(x) panics if an i64 doesn't fit. Conversions are explicit.

/// One reading from a joint encoder, stamped in nanoseconds.
type Reading = { at: i64, angle: float }

fn spanMs(rs: Reading[]): int => int((rs[rs.len - 1].at - rs[0].at) / 1_000_000)

fn main() {
  let start: i64 = 1_731_660_000_000_000_000
  var log: Reading[] = []
  for i in 0..5 {
    // 135Hz is a period of 7_407_407ns
    log.push(Reading { at: start + i64(i) * 7_407_407, angle: float(i) * 0.1 })
  }
  print("{log.len} readings over {spanMs(log)}ms")
  print("last at {log[log.len - 1].at}ns")

  let bytes: i64 = 6_000_000_000
  print("{float(bytes) / 1e9} GB is {bytes} bytes")
}
5 readings over 29ms
last at 1731660000029629628ns
6.0 GB is 6000000000 bytes