38 lines
862 B
Plaintext

; Stock items and prices kept in cents, imported by ../inventory.fln.
struct Item
name: str
cents: i64
count: i32
; A price read as a float without a division, by its bits.
union Bits
f: f32
u: u32
const cents-per-unit = 100
fn- whole(cents: i64) -> i64 = cents / cents-per-unit
fn- part(cents: i64) -> i64 = cents % cents-per-unit
fn item(name: str, cents: i64, count: i32) -> Item
Item{.name name, .cents cents, .count count}
fn value(it: Item) -> i64 = it.cents * i64(it.count)
; 1234 as "12.34", into a buffer the caller frees.
fn money(cents: i64) -> Vec(u8)
let out = vec-new(u8)
append-i64(addr(out), whole(cents))
append(addr(out), bytes-view("."))
if part(cents) < 10
append(addr(out), bytes-view("0"))
append-i64(addr(out), part(cents))
out
fn sign-bit(x: f32) -> u32
let b: Bits = zeroed()
b.f = x
b.u >> 31