-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.sh
executable file
·44 lines (37 loc) · 1002 Bytes
/
float.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
# > float.sh <
# Support for floating point math in bash.
# The original code appears on linuxjournal.com, July 30, 2008:
# http://www.linuxjournal.com/content/floating-point-math-bash
# These were written by Mitch Frazier (I think) and are covered by the GNU Public License, version 2.
# Default scale used by bc.
float_scale=2
# Evaluate a floating point number expression.
function float_eval () {
local stat=0
local result=0.0
if [[ $# -gt 0 ]]; then
result=$(echo "scale=$float_scale; $*" | bc -q 2>/dev/null)
stat=$?
if [[ $stat -eq 0 && -z "$result" ]]; then
stat=1;
fi
fi
echo $result
return $stat
}
# Evaluate a floating point number conditional expression.
function float_cond () {
local cond=0
if [[ $# -gt 0 ]]; then
cond=$(echo "$*" | bc -q 2>/dev/null)
if [[ -z "$cond" ]];
then cond=0;
fi
if [[ "$cond" != 0 && "$cond" != 1 ]]; then
cond=0;
fi
fi
local stat=$((cond == 0))
return $stat
}