Everyday
Kitchen, energy, fitness, and notes: units doing the work so you do not have to.
Scale a recipe and mint a unit for the butter
Section titled “Scale a recipe and mint a unit for the butter”Cooking mixes metric and US units, and every kitchen has a unit of its own. Declaring one takes a line.
## Cake for twelve, from a recipe for eight
scale = 12 / 8
flour = 250 g * scale
sugar = 175 g * scale
milk = 300 ml * scale to cups to 1 dp
unit stick = 113 g, also: sticks
butter = 175 g * scale
butter to sticks to 1 dp
oven = 180 C to F
to cups converts the scaled volume; unit stick = 113 g teaches smoot the
American butter stick, and to sticks works from then on.
What an appliance costs to run
Section titled “What an appliance costs to run”A tariff is a price per unit of energy. Power times time is energy, and the energy cancels against the tariff to leave money.
tariff = £0.28 / 1 kWh
kettle = 3 kW * 4 minutes
kettle to kWh
kettle * tariff
shower = 9 kW * 12 minutes * tariff
shower * 365 to 2 dp
standby = 15 W * 1 year to kWh
standby to 0 dp
standby * tariff to 2 dp
Nothing here is converted by hand. 3 kW * 4 minutes is an energy whatever
units you write it in, and * tariff turns it into money.
Race pace, and the same pace over other distances
Section titled “Race pace, and the same pace over other distances”Dividing a time by a distance gives a pace; multiplying the pace by another distance gives a time back.
race = 10 km
finish = 50 minute
pace = finish / race to minute/km
pace * 5 km to minute
pace * 42.195 km to hour, minute to 0 dp
pace to minute/mile to 2 dp
The dimension of pace is Time over Length, so pace * 42.195 km cancels
the length and leaves a time. to hour, minute shows it in two units, and
the trailing to 0 dp rounds the minutes.
BMI from either side of the Atlantic
Section titled “BMI from either side of the Atlantic”A function with dimension hints accepts any units of the right kind. The result displays in whatever units went in, so convert it once.
bmi(weight: Mass, height: Length) = weight / height^2 to kg/m^2 to 1 dp
bmi(72 kg, 1.78 m)
bmi(160 lb, 5 ft 10 inch)
verdict = if bmi(160 lb, 5 ft 10 inch) < 25 kg/m^2 then "in range" else "check"
5 ft 10 inch is one length. The comparison in verdict is dimension-checked
too: comparing against a bare 25 would be an error.
Ladder safety
Section titled “Ladder safety”Trigonometry takes real angles. sin(75 deg) is not the same as sin(75),
which would be radians.
ladder = 6 m
angle = 75 deg
height reached = ladder * sin(angle) to 2 dp
base distance = ladder * cos(angle) to 2 dp
solve for safe angle: 6 m * cos(safe angle) = 1.2 m
safe angle to deg to 1 dp
The solve goes through cos, whose inverse smoot knows, and hands back an
angle you can convert to degrees.
Notes that stay notes
Section titled “Notes that stay notes”A document can carry as much prose as you like. Plain lines with no known
names in them are ignored; lines with a # are always ignored. Use # when
a note mentions a unit or a name you have defined.
## Trip budget
Two nights, leaving Friday.
# Hotel is 140 a night, m is for metres so this line needs a hash
hotel = 2 * €140
trains = 2 * €38
food = 2 * €60
hotel + trains + food
The second line is a note because none of its words are defined. The third
would fail without the #, because m is a unit.