RogerBW's Blog

The units program as a command-line calculator 28 July 2017

The units(1) program is available on many Unix systems (though rarely installed by default). Most people think of it as a simple interactive converter. It's actually much more powerful than that.

This is how people expect to use it, entering things at an interactive prompt:

$ units
You have: mile
You want: km
        * 1.609344
        / 0.62137119

That's useful but very dull. Functions are slightly more promising:

$ units
You have: tempF(98.4)
You want: tempC
    36.888889

But it also works with pure command-line arguments, potentially of some complexity - which of course allows it to be a handy command-line calculator too. Atmospheric pressure in psi:

$ units atmosphere gravity*lb/inch^2
        * 14.695949
        / 0.068045964

What time will my 3d print finish if I start it now?

$ units $(date +"%Hhour+%Mminute+%Ssecond")+8hour+19minute "hour;minute;second"
        19 hour + 31 minute + 9 second

The blackbody temperature of a 100m-radius sphere radiating at 1MW (yes, lots of physical constants are here too):

$ units "(1MW/stefanboltzmann/((100m^2)*4pi))^(1/4)" tempC
        71.037224

If you're feeding the output into another programme (because this is Unix and you can do that sort of thing), use --compact to strip off the decoration:

$ units --compact "(1MW/stefanboltzmann/((100m^2)*4pi))^(1/4)" tempC
71.037224

Of course it's extensible. As someone who plays a fair bit of GURPS, I find it useful to implement that game's logarithmic range scale, so I add to my local .units file:

gurpsrange(x) units=[1;yard] range=[0,) \
10^((x+2)/6)*yard ; log(gurpsrange/yard)*6-2

and now I can easily calculate ranges for a space game:

$ units 5astronomicalunit gurpsrange
        69.476555
$ units "gurpsrange(40)" mile
        * 5681.8182
        / 0.000176

For any Torg players:

torgdistance(x) units=[1;metre] range=[0,) \
10^(x/5)*metre ; log(torgdistance/metre)*5
torgtime(x) units=[1;second] range=[0,) \
10^(x/5)*second ; log(torgtime/second)*5
torgweight(x) units=[1;kg] range=[0,) \
10^(x/5)*kg ; log(torgweight/kg)*5
torgspeed(x) units=[1;m/s] range=[0,) \
10^((x-5)/5)*m/s ; log(torgspeed*s/m)*5+5

Note that speed is not distance minus time as it should have been in a cleanly-designed system, but based on metres per 10s combat round; thus the fudge factor. What a pity.

Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2022 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1