dc is the RPN arbitrary precision calculator that's long been a standard on UNIX systems (but not actually standardized by POSIX)
Yesterday a friend was noticing how .. weird .. it is with fractions in non-decimal radixes. gnu dc is particularly spectacularly broken, while bsd dc is somewhat better.
gnu dc failing: 16dio 8k .02p (set precision to 8 places, input and output radix to 16, and print 1/256(decimal)) prints "0". Ideally, it would print ".02". bsd dc prints ".020000".
bsd dc failing: 99k 3o 1 3 / p (set precision to 99 places, output radix to 3, compute 1/3, and print it). Ideally it would print ".1" or ".1000..." but bsd dc prints ".022....21"
This is because internally all dc arithmetic takes place in decimal; additionally, gnu dc incorrectly counts how many decimal digits of precision are needed.
So of course .. I wrote my own incomplete dc implementation. It uses Python and infinite precision fractions. Only the very basic operations are implemented, but at least you can calculate in hexadecimal floating point notation and get accurate results.
pydc> 8k 16dio
pydc> .8 .FFE *p
0.7FF
pydc> 1 3 / p
0.55555555
For now the code lives in my junk drawer and has no explicit license, but you can use it if you want. https://codeberg.org/jepler/junkdrawer/src/branch/main/bgj9ls82/dc.py
#python #unix #rpn #calculator