mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-04-25 20:41:15 -03:00
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:
@@ -8,7 +8,7 @@ Synopsis
|
||||
|
||||
::
|
||||
|
||||
math [-sN | --scale=N] [--] EXPRESSION
|
||||
math [-sN | --scale=N] [-bBASE | --base=BASE] [--] EXPRESSION
|
||||
|
||||
|
||||
Description
|
||||
@@ -26,6 +26,8 @@ The following options are available:
|
||||
|
||||
- ``-sN`` or ``--scale=N`` sets the scale of the result. ``N`` must be an integer or the word "max" for the maximum scale. A scale of zero causes results to be rounded down to the nearest integer. So ``3/2`` returns ``1`` rather than ``2`` which ``1.5`` would normally round to. This is for compatibility with ``bc`` which was the basis for this command prior to fish 3.0.0. Scale values greater than zero causes the result to be rounded using the usual rules to the specified number of decimal places.
|
||||
|
||||
- ``-b BASE`` or ``--base BASE`` sets the numeric base used for output (``math`` always understands hexadecimal numbers as input). It currently understands "hex" or "16" for hexadecimal and "octal" or "8" for octal and implies a scale of 0 (other scales cause an error), so it will truncate the result down to an integer. This might change in the future. Hex numbers will be printed with a ``0x`` prefix. Octal numbers will have a prefix of ``0`` and aren't understood by ``math`` as input.
|
||||
|
||||
Return Values
|
||||
-------------
|
||||
|
||||
@@ -119,6 +121,8 @@ Examples
|
||||
|
||||
``math "bitor(9,2)"`` outputs 11.
|
||||
|
||||
``math --base=hex 192`` prints ``0xc0``.
|
||||
|
||||
Compatibility notes
|
||||
-------------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user