fish-shell/share/functions/math.fish
Fabian Homborg 11f4e64e45 Revert "Use BC_LINE_LENGTH=2 for bc."
This would fail on very long numbers, e.g.

`math "1 + 1233242342353453463458972349873489273984873289472914712894791824712941"`

would now return "42", where it previously returned the correct "1233242342353453463458972349873489273984873289472914712894791824712942".

This reverts commit 26e781ef5a.
2016-12-29 13:53:11 +01:00

42 lines
1.3 KiB
Fish

function math --description "Perform math calculations in bc"
set -l scale 0 # default is integer arithmetic
if set -q argv[1]
switch $argv[1]
case '-s*' # user wants to specify the scale of the output
set scale (string replace -- '-s' '' $argv[1])
if not string match -q -r '^\d+$' "$scale"
echo 'Expected an integer to follow -s' >&2
return 2 # missing argument is an error
end
set -e argv[1]
case -h --h --he --hel --help
__fish_print_help math
return 0
end
end
if not set -q argv[1]
return 2 # no arguments is an error
end
# Stitch lines together manually. We can't rely on BC_LINE_LENGTH because some systems don't
# have a new enough version of bc.
set -l out (echo "scale=$scale; $argv" | bc | string replace -r '\\\\$' '' | string join '')
switch "$out"
case ''
# No output indicates an error occurred.
return 3
case 0
# For historical reasons a zero result translates to a failure status.
echo 0
return 1
case '*'
# For historical reasons a non-zero result translates to a success status.
echo $out
return 0
end
end