draftply Download

Digital Storage & Data Rate

← All samples

data-units.dplylibEditor
1---
2app_version: 0.5.0
3---
4# data-units.dplylib
5// Digital storage and data-rate units — decimal (SI) and binary, kept
6// strictly separate.
7//
8// Import this library and use the units inline (500 GB, 100 Mbps) or with
9// convert(): convert(1, "GiB", "GB"), ...
10//
11// "B" (byte) is decimal: kB/MB/GB/TB compose automatically from the same
12// SI-prefix machinery as any other custom unit (1000x per prefix letter),
13// matching how manufacturers and network speeds count. "KiB"/"MiB"/"GiB"/
14// "TiB" are binary (1024x per step) and defined explicitly below, since a
15// prefix letter alone never means 1024 here — "Ki" is not a recognised
16// prefix, so each binary step needs its own defunit, the same reason
17// electronics-units.dplylib spells out µF/µV/µA/µH by hand.
18
19# Decimal — byte and bit, SI prefixes compose automatically
20defunit("B") // byte — base unit
21defunit("bit", 1 / 8, "B") // 8 bit = 1 B
22defunit("bps", "bit/s") // bit per second — Mbps/Gbps compose automatically
23
24# Binary — each step is its own unit, not a composed prefix
25defunit("KiB", 1024, "B")
26defunit("MiB", 1024, "KiB")
27defunit("GiB", 1024, "MiB")
28defunit("TiB", 1024, "GiB")
29
30# Quick examples (visible when you open this file directly)
31convert(1, "GiB", "GB") // 1.073741824 GB — binary vs. decimal drift
32convert(1, "TB", "GB") // 1000 GB
33convert(100, "Mbps", "MB/s") // a 100 Mbps link is 12.5 MB/s, not 100
34