Skip to content

Commit d65a4e4

Browse files
committed
echo-math, bash.bash: support bash 5.3 fltexpr
1 parent a3a8a8a commit d65a4e4

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

commands/echo-math

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,44 @@ function echo_math_test() (
66
local inputs=('1 - 1/2' '1 - 1/3' '1 - 1/4' '123456789 / 0.0987654321')
77
local expected_six=$'0.500000\n0.666667\n0.750000\n1249999988.609375'
88
local expected_two=$'0.50\n0.67\n0.75\n1249999988.61'
9+
local expected_zero=$'0\n1\n1\n1249999989'
10+
local expected_zero_deno=$'1\n1\n1\n1249999989'
911
function __test {
12+
local expected
13+
1014
# this first one may install the tool, use --discard-tty to avoid dumping thousands of lines to the CI terminal, and --ignore-tty to avoid comparisons of the captured TTY data
15+
16+
# --precision=6
17+
# arguments
1118
eval-tester --discard-tty --ignore-tty --stdout="$expected_six" -- \
1219
echo-math "$@" -- "${inputs[@]}"
13-
20+
# stdin
1421
__print_lines "${inputs[@]}" |
1522
eval-tester --stdout="$expected_six" -- \
1623
echo-math "$@" --stdin
1724

25+
# --precision=2
26+
# arguments
1827
eval-tester --stdout="$expected_two" -- \
1928
echo-math --precision=2 "$@" -- "${inputs[@]}"
20-
29+
# stdin
2130
__print_lines "${inputs[@]}" |
2231
eval-tester --stdout="$expected_two" -- \
2332
echo-math --precision=2 "$@" --stdin
33+
34+
# --precision=0
35+
if [[ $* == *'--tool=deno'* ]]; then
36+
expected="$expected_zero_deno"
37+
else
38+
expected="$expected_zero"
39+
fi
40+
# arguments
41+
eval-tester --stdout="$expected" -- \
42+
echo-math --precision=0 "$@" -- "${inputs[@]}"
43+
# stdin
44+
__print_lines "${inputs[@]}" |
45+
eval-tester --stdout="$expected" -- \
46+
echo-math --precision=0 "$@" --stdin
2447
}
2548

2649
# run test with defaults
@@ -48,7 +71,11 @@ function echo_math_test() (
4871
)
4972
function echo_math() (
5073
source "$DOROTHY/sources/stdinargs.bash"
51-
local all_tools=(awk gawk deno)
74+
local all_tools=()
75+
if [[ $BASH_NATIVE_FLOATING_POINT == 'yes' ]]; then
76+
all_tools+=(bash)
77+
fi
78+
all_tools+=(awk gawk deno)
5279
# [bc] is removed, as it fails silently on debian systems
5380

5481
# =====================================
@@ -112,6 +139,7 @@ function echo_math() (
112139
# =====================================
113140
# Action
114141

142+
# bash: dynamic decimals by default
115143
# bc: 0 decimals by default
116144
# bc -l: 20 decimals by default
117145
# 0. becomes ., hence need for printf
@@ -125,34 +153,41 @@ function echo_math() (
125153
# echo-escape-command: Failed to compute: awk -v precision=6 BEGIN\ \{\ printf\ \"%.\"\ precision\ \"f\\n\"\,\ /\ \}
126154
__print_error 'Failed to compute: ' --code="$(echo-quote --always=no -- "$@" | echo-join --stdin || :)" --newline \
127155
"With $1 binary: " --code="$(type -P "$1" || :)" --newline \
128-
"With $1 version: " --code="$($1 --version 2>/dev/null || :)"
156+
"With $1 version: " --code="$($1 --version 2>/dev/null || :)" || :
129157
return "$exit_status"
130158
}
131159
}
132160

133161
# perl implementation (doesn't work on opensuse): https://gist.github.com/balupton/2a8ec0c004cb382895807ae71c6cef83
134-
if [[ $option_tool == 'bc' ]]; then
135-
function on_line {
162+
if [[ $option_tool == 'bash' ]]; then
163+
enable fltexpr
164+
function __on_line {
165+
local formula="$1" fodder_to_respect_exit_status
166+
fltexpr "fodder_to_respect_exit_status=$formula" || return
167+
printf "%.${option_precision}f\n" "$fodder_to_respect_exit_status" || return
168+
}
169+
elif [[ $option_tool == 'bc' ]]; then
170+
function __on_line {
136171
local formula="$1" fodder_to_respect_exit_status
137-
fodder_to_respect_exit_status="$(__wrap bc --mathlib --no-prompt --no-read-prompt --standard --warn --expression="$formula")"
138-
printf "%.${option_precision}f\n" "$fodder_to_respect_exit_status"
172+
fodder_to_respect_exit_status="$(__wrap bc --mathlib --no-prompt --no-read-prompt --standard --warn --expression="$formula")" || return
173+
printf "%.${option_precision}f\n" "$fodder_to_respect_exit_status" || return
139174
}
140175
elif [[ $option_tool == 'deno' ]]; then
141176
local deno_script
142177
deno_script="$(type -P echo-math.ts)"
143-
function on_line {
178+
function __on_line {
144179
local formula="$1"
145-
__wrap "$deno_script" "$option_precision" "$formula"
180+
__wrap "$deno_script" "$option_precision" "$formula" || return
146181
}
147182
elif [[ $option_tool == 'awk' ]]; then
148-
function on_line {
183+
function __on_line {
149184
local formula="$1"
150-
__wrap awk -v precision="$option_precision" 'BEGIN { printf "%." precision "f\n", '"$formula"' }'
185+
__wrap awk -v precision="$option_precision" 'BEGIN { printf "%." precision "f\n", '"$formula"' }' || return
151186
}
152187
elif [[ $option_tool == 'gawk' ]]; then
153-
function on_line {
188+
function __on_line {
154189
local formula="$1"
155-
__wrap gawk -v precision="$option_precision" 'BEGIN { printf "%." precision "f\n", '"$formula"' }'
190+
__wrap gawk -v precision="$option_precision" 'BEGIN { printf "%." precision "f\n", '"$formula"' }' || return
156191
}
157192
else
158193
return 19 # ENODEV 19 Operation not supported by device

docs/bash/versions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ From changelog:
307307
>
308308
> d. Changes to `set -e` exit behavior in posix mode, since POSIX now says to exit as if executing the `exit builtin with no arguments`.
309309
>
310-
> a. There is a new `fltexpr' loadable builtin to perform floating-point arithmetic similarly to `let'.
310+
> a. There is a new `fltexpr` loadable builtin to perform floating-point arithmetic similarly to `let'.
311311
312312
> This document details the changes between this version, `bash-5.3-beta`, and the previous version, `bash-5.3-alpha`.
313313
>

sources/bash.bash

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,13 @@ else
839839
}
840840
fi
841841

842+
# Bash >= 5.3
843+
if [[ $BASH_VERSION_MAJOR -eq 5 && $BASH_VERSION_MINOR -ge 1 ]]; then
844+
BASH_NATIVE_FLOATING_POINT='yes'
845+
else
846+
BASH_NATIVE_FLOATING_POINT='no'
847+
fi
848+
842849
# Bash >= 5.1, >= 4, < 4
843850
if [[ $BASH_VERSION_MAJOR -eq 5 && $BASH_VERSION_MINOR -ge 1 ]]; then
844851
# bash >= 5.1

0 commit comments

Comments
 (0)