Fun with dc
dc
, also known as as the Unix desk calculator, is indubitably the most useful command you can find on a Unix system. I can’t imagine a single working day without using dc
(especially with one dollar being (roughly) 6.2931236763 French Francs).1
How desperate I was when I discovered that dc
doesn’t work correctly on SunOS 5.6!
Just try a simple division like, say, 1083221002471 / 999927. On a SunOS 5.6 workstation, I get 1083301. On a SunOS 4 machine or a linux machine, I get 1083300, which is the correct answer. I have other examples of incorrect divisions, but this is the smallest one (!). Don’t ask me how I found it…
Another problem with dc
is that it sometimes refuses to write ASCII
codes greater than 99 with the P
command, especially on SunOS systems (including SunOS 4). I don’t know if it is a bug (since I don’t know exactly what the P
command is supposed to do), but I think 99 is a pretty strange limit… In any case, it prints those characters correctly on a Silicon Graphics computer (free ad) and on my linux machine (another free ad).
To test your version of dc
, try to type in:
dc
then
3808936911278909389359514998203616329962227439192148[d256%P256/d0<.]ds.x
If it works, you can then try:
970313512859254401102697436301630872058487784302540104[d256%P256/d0<.]ds.x
If your dc
is able to print ASCII characters over 99, you can try this one. Be sure to type a single line:
22208085721160815839326341402121883690522320029255552164456082082121507399131784888796775404393238740752073581292176033192975608648980579511790754074629932624904826033926771561352802702304711849306991432140783663141097741410673769879986682209343466281135283290275369909264160273176470124389744289584917009576579173596679560702849116170806711176042540325188738564140628164804618257135693047847192850818282310363304691273054958318318136191456275934310979155326791959077883445003166261104834773132040707456928546349673980840313948708349674193786434159922651000968311352206702569130007195445654084933953345626389764219705628257612293204376976748352217929337471880882231223685405877498688770919610871973453341673913923792234893241993439245115105190893660465508673064373840268054184063151225377582060655786260788736204677135397194609418373902725670020397378638535454535982247377168952102335040177207305844444366209308523996849245976720417612176719067483022960817121007244452665449784273243174997760034496193814960150784537785113589011725985900508524882770853637523676303240173952011467376200157457202234158542518689678707600958087139914974630505378072055818423497887022672894966264722482896163204923500950712750800537457664585469095374214293974296229117403664516032413259929513310658563843081832487569416683206341446732898062734501513714984552459725473478529038715898310732843126214088302700527765333188909878114118156328607905954179948741014531467629112351746391357169021505149742077359053197219166630939696390194435167157513452231104227665811363415438461304936968404285629177347016678518931947203796484167545101613670772251307345495413035233907657705241691280430994746768012380728375146653630623831838222282318165089162386341671976737726353273451886027886364291170031129520721957416004799824490011955162466021287535364309928388542132049900161052116530089934228815775998215778916104959359698408064987438386886161480778575982275751968826631954742887974749292068020577501261842082809483702986629865701617200056158514119921272726266484084142349717069035717802916618873448175705514451223911583066310022020808646584937752763866798004057371097500872885733222570967153949159920525516838117787583988167897342360101235912871520761154734770678309671549603175730098764310659277164535040418340982101759825495735066065501823775306294622701628580346350888711132078451931296748345416719473558304071712[d256%P256/d0<.]ds.x
Alternatively, you can download the line as a jabberwocky.dc file and try:
dc < jabberwocky.dc
And that’s how dc
can recite poetry…
Can you guess what this dc
program is doing?
2p3p[dl!d2+s!%0=@l!l^!<#]s#[s/0ds^]s@[p]s&[ddvs^3s!l#x0<&2+l.x]ds.x
I’ll give you a hint: It prints 2, then 3, then more numbers… (There should be an International Obfuscated DC Code Contest…)
But dc
can also be useful. For instance:
echo "$1[p]s2[lip/dli%0=1dvsr]s12sid2%0=13sidvsr[dli%0=1lrli2+dsi!>.]ds.xd1<2" | dc
is a small shell-script2 that can replace the factor
program you can find on some linux systems. It has no limit on the size of the numbers and can tell you that 12345678901 is 857 x 14405693 while the factor
program cannot. (If you don’t call that a useful fact…)
-
This was written shortly before the advent of the Euro. ↩
-
This program somehow found its way into the Advanced Bash Scripting Guide and brought me fame and fortune. Well, maybe not fortune, but fame. Well, maybe not quite fame but… ↩