math: Add --base option

Currently a bit limited, unfortunately printf's `%a` specifier is
absolutely unreadable.

So we add `hex` and `octal` with `0x` and `0` prefixes respectively,
and also take a number but currently only allow 16 and 8.

The output is truncated to integer, so scale values other than 0 are
invalid and 0 is implied.

The docs mention this may change.
This commit is contained in:
Fabian Homborg
2020-10-07 19:03:19 +02:00
parent 30c7a17302
commit 5872f4522d
3 changed files with 52 additions and 2 deletions

View File

@@ -173,3 +173,16 @@ math 'log(16'
# CHECKERR: math: Error: Missing closing parenthesis
# CHECKERR: 'log(16'
# CHECKERR: ^
math --base=16 255 / 15
# CHECK: 0x11
math -bhex 16 x 2
# CHECK: 0x20
math --base hex 12 + 0x50
# CHECK: 0x5c
math --base octal --scale=0 55
# CHECK: 067
math --base notabase
# CHECKERR: math: 'notabase' is not a valid base value
echo $status
# CHECK: 2