math: Allow --scale=max

This commit is contained in:
Fabian Homborg 2019-02-13 12:54:19 +01:00
parent c5a6d0bfde
commit ca5b7c0ec4
5 changed files with 14 additions and 6 deletions

View File

@ -18,6 +18,7 @@
- The default escape delay (to differentiate between the escape key and an alt-combination) has been reduced to 30ms, down from 300ms for the default mode and 100ms for vi-mode (#3904).
- In the interest of consistency, `builtin -q` and `command -q` can now be used to query if a builtin or command exists (#5631).
- The `path_helper` on macOS now only runs in login shells, matching the bash implementation.
- `math` now accepts `--scale=max` for the maximum scale (#5579).
---

View File

@ -17,7 +17,7 @@ Keep in mind that parameter expansion takes before expressions are evaluated. Th
The following options are available:
- `-sN` or `--scale=N` sets the scale of the result. `N` must be an integer. 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.
- `-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.
\subsection return-values Return Values

View File

@ -48,11 +48,16 @@ static int parse_cmd_opts(math_cmd_opts_t &opts, int *optind, //!OCLINT(high nc
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
case 's': {
opts.scale = fish_wcstoi(w.woptarg);
if (errno || opts.scale < 0 || opts.scale > 15) {
streams.err.append_format(_(L"%ls: '%ls' is not a valid scale value\n"), cmd,
w.woptarg);
return STATUS_INVALID_ARGS;
// "max" is the special value that tells us to pick the maximum scale.
if (wcscmp(w.woptarg, L"max") == 0) {
opts.scale = 15;
} else {
opts.scale = fish_wcstoi(w.woptarg);
if (errno || opts.scale < 0 || opts.scale > 15) {
streams.err.append_format(_(L"%ls: '%ls' is not a valid scale value\n"), cmd,
w.woptarg);
return STATUS_INVALID_ARGS;
}
}
break;
}

View File

@ -8,6 +8,7 @@ math '10 % 6'
math -s0 '10 % 6'
math '23 % 7'
math --scale=6 '5 / 3 * 0.3'
math --scale=max '5 / 3'
math "7^2"
math -1 + 1
math '-2 * -2'

View File

@ -10,6 +10,7 @@
4
2
0.5
1.666666666666667
49
0
4