mirror of
https://github.com/fish-shell/fish-shell
synced 2024-11-10 07:04:29 +00:00
math.rst: use definition lists.
Also, follow the one-line per sentence rule as that has semantic meaning for man/groff.
This commit is contained in:
parent
a16fd0baa4
commit
4efb1b1644
1 changed files with 103 additions and 47 deletions
|
@ -12,22 +12,39 @@ Synopsis
|
|||
Description
|
||||
-----------
|
||||
|
||||
``math`` performs mathematical calculations. It supports simple operations such as addition, subtraction, and so on, as well as functions like ``abs()``, ``sqrt()`` and ``ln()``.
|
||||
``math`` performs mathematical calculations.
|
||||
It supports simple operations such as addition, subtraction, and so on, as well as functions like ``abs()``, ``sqrt()`` and ``ln()``.
|
||||
|
||||
By default, the output is a floating-point number with trailing zeroes trimmed. To get a fixed representation, the ``--scale`` option can be used, including ``--scale=0`` for integer output.
|
||||
By default, the output is a floating-point number with trailing zeroes trimmed.
|
||||
To get a fixed representation, the ``--scale`` option can be used, including ``--scale=0`` for integer output.
|
||||
|
||||
Keep in mind that parameter expansion happens before expressions are evaluated. This can be very useful in order to perform calculations involving shell variables or the output of command substitutions, but it also means that parenthesis (``()``) and the asterisk (``*``) glob character have to be escaped or quoted. ``x`` can also be used to denote multiplication, but it needs to be followed by whitespace to distinguish it from hexadecimal numbers.
|
||||
Keep in mind that parameter expansion happens before expressions are evaluated.
|
||||
This can be very useful in order to perform calculations involving shell variables or the output of command substitutions, but it also means that parenthesis (``()``) and the asterisk (``*``) glob character have to be escaped or quoted.
|
||||
``x`` can also be used to denote multiplication, but it needs to be followed by whitespace to distinguish it from hexadecimal numbers.
|
||||
|
||||
Parentheses for functions are optional - ``math sin pi`` prints ``0``. However, a comma will bind to the inner function, so ``math pow sin 3, 5`` is an error because it tries to give ``sin`` the arguments ``3`` and ``5``. When in doubt, use parentheses.
|
||||
|
||||
``math`` ignores whitespace between arguments and takes its input as multiple arguments (internally joined with a space), so ``math 2 +2`` and ``math "2 + 2"`` work the same. ``math 2 2`` is an error.
|
||||
Parentheses for functions are optional - ``math sin pi`` prints ``0``.
|
||||
However, a comma will bind to the inner function, so ``math pow sin 3, 5`` is an error because it tries to give ``sin`` the arguments ``3`` and ``5``.
|
||||
When in doubt, use parentheses.
|
||||
|
||||
``math`` ignores whitespace between arguments and takes its input as multiple arguments (internally joined with a space), so ``math 2 +2`` and ``math "2 + 2"`` work the same.
|
||||
``math 2 2`` is an error.
|
||||
|
||||
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.
|
||||
**-s** *N* 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.
|
||||
**-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`` but aren't understood by ``math`` as input.
|
||||
|
||||
Return Values
|
||||
-------------
|
||||
|
@ -39,22 +56,30 @@ Syntax
|
|||
|
||||
``math`` knows some operators, constants, functions and can (obviously) read numbers.
|
||||
|
||||
For numbers, ``.`` is always the radix character regardless of locale - ``2.5``, not ``2,5``. Scientific notation (``10e5``) and hexadecimal (``0xFF``) are also available.
|
||||
For numbers, ``.`` is always the radix character regardless of locale - ``2.5``, not ``2,5``.
|
||||
Scientific notation (``10e5``) and hexadecimal (``0xFF``) are also available.
|
||||
|
||||
Operators
|
||||
---------
|
||||
|
||||
``math`` knows the following operators:
|
||||
|
||||
- ``+`` for addition and ``-`` for subtraction.
|
||||
|
||||
- ``*`` or ``x`` for multiplication, ``/`` for division. (Note that ``*`` is the glob character and needs to be quoted or escaped, ``x`` needs to be followed by whitespace or it looks like ``0x`` hexadecimal notation.)
|
||||
|
||||
- ``^`` for exponentiation.
|
||||
|
||||
- ``%`` for modulo.
|
||||
|
||||
- ``(`` and ``)`` for grouping. (These need to be quoted or escaped because ``()`` denotes a command substitution.)
|
||||
``+``
|
||||
for addition
|
||||
``-``
|
||||
for subtraction
|
||||
``*`` or ``x``
|
||||
for multiplication
|
||||
``/``
|
||||
for division
|
||||
(Note that ``*`` is the glob character and needs to be quoted or escaped, ``x`` needs to be followed by whitespace or it looks like ``0x`` hexadecimal notation.)
|
||||
``^``
|
||||
for exponentiation
|
||||
``%``
|
||||
for modulo
|
||||
``(`` or ``)``
|
||||
for grouping.
|
||||
(These need to be quoted or escaped because ``()`` denotes a command substitution.)
|
||||
|
||||
They are all used in an infix manner - ``5 + 2``, not ``+ 5 2``.
|
||||
|
||||
|
@ -63,9 +88,13 @@ Constants
|
|||
|
||||
``math`` knows the following constants:
|
||||
|
||||
- ``e`` - Euler's number.
|
||||
- ``pi`` - π. You know this one. Half of Tau.
|
||||
- ``tau``. Equivalent to 2π, or the number of radians in a circle.
|
||||
``e``
|
||||
Euler's number
|
||||
``pi``
|
||||
π, you know this one.
|
||||
Half of Tau
|
||||
``tau``
|
||||
Equivalent to 2π, or the number of radians in a circle
|
||||
|
||||
Use them without a leading ``$`` - ``pi - 3`` should be about 0.
|
||||
|
||||
|
@ -74,32 +103,59 @@ Functions
|
|||
|
||||
``math`` supports the following functions:
|
||||
|
||||
- ``abs`` - the absolute value, with positive sign
|
||||
- ``acos`` - arc cosine
|
||||
- ``asin`` - arc sine
|
||||
- ``atan`` - arc tangent
|
||||
- ``atan2`` - arc tangent of two variables
|
||||
- ``bitand``, ``bitor`` and ``bitxor`` to perform bitwise operations. These will throw away any non-integer parts and interpret the rest as an int.
|
||||
- ``ceil`` - round number up to nearest integer
|
||||
- ``cos`` - the cosine
|
||||
- ``cosh`` - hyperbolic cosine
|
||||
- ``exp`` - the base-e exponential function
|
||||
- ``fac`` - factorial - also known as ``x!`` (``x * (x - 1) * (x - 2) * ... * 1``)
|
||||
- ``floor`` - round number down to nearest integer
|
||||
- ``ln`` - the base-e logarithm
|
||||
- ``log`` or ``log10`` - the base-10 logarithm
|
||||
- ``log2`` - the base-2 logarithm
|
||||
- ``max`` - returns the larger of two numbers
|
||||
- ``min`` - returns the smaller of two numbers
|
||||
- ``ncr`` - "from n choose r" combination function - how many subsets of size r can be taken from n (order doesn't matter)
|
||||
- ``npr`` - the number of subsets of size r that can be taken from a set of n elements (including different order)
|
||||
- ``pow(x,y)`` returns x to the y (and can be written as ``x ^ y``)
|
||||
- ``round`` - rounds to the nearest integer, away from 0
|
||||
- ``sin`` - the sine function
|
||||
- ``sinh`` - the hyperbolic sine
|
||||
- ``sqrt`` - the square root - (can also be written as ``x ^ 0.5``)
|
||||
- ``tan`` - the tangent
|
||||
- ``tanh`` - the hyperbolic tangent
|
||||
``abs``
|
||||
the absolute value, with positive sign
|
||||
``acos``
|
||||
arc cosine
|
||||
``asin``
|
||||
arc sine
|
||||
``atan``
|
||||
arc tangent
|
||||
``atan2``
|
||||
arc tangent of two variables
|
||||
``bitand``, ``bitor`` and ``bitxor``
|
||||
perform bitwise operations.
|
||||
These will throw away any non-integer parts andd interpret the rest as an int.
|
||||
``ceil``
|
||||
round number up to nearest integer
|
||||
``cos``
|
||||
the cosine
|
||||
``cosh``
|
||||
hyperbolic cosine
|
||||
``exp``
|
||||
the base-e exponential function
|
||||
``fac``
|
||||
factorial - also known as ``x!`` (``x * (x - 1) * (x - 2) * ... * 1``)
|
||||
``floor``
|
||||
round number down to nearest integer
|
||||
``ln``
|
||||
the base-e logarithm
|
||||
``log`` or ``log10``
|
||||
the base-10 logarithm
|
||||
``log2``
|
||||
the base-2 logarithm
|
||||
``max``
|
||||
returns the larger of two numbers
|
||||
``min``
|
||||
returns the smaller of two numbers
|
||||
``ncr``
|
||||
"from n choose r" combination function - how many subsets of size r can be taken from n (order doesn't matter)
|
||||
``npr``
|
||||
the number of subsets of size r that can be taken from a set of n elements (including different order)
|
||||
``pow(x,y)``
|
||||
returns x to the y (and can be written as ``x ^ y``)
|
||||
``round``
|
||||
rounds to the nearest integer, away from 0
|
||||
``sin``
|
||||
the sine function
|
||||
``sinh``
|
||||
the hyperbolic sine
|
||||
``sqrt``
|
||||
the square root - (can also be written as ``x ^ 0.5``)
|
||||
``tan``
|
||||
the tangent
|
||||
``tanh``
|
||||
the hyperbolic tangen
|
||||
|
||||
All of the trigonometric functions use radians (the pi-based scale, not 360°).
|
||||
|
||||
|
|
Loading…
Reference in a new issue