/
math.sh
159 lines (145 loc) · 4.46 KB
/
math.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# @package: util/math
# Routines for common math operations
_bb_onfirstload "bb_util_math" || return
################################################################################
# Globals
################################################################################
################################################################################
# Functions
################################################################################
# function: bb_sum [-v VAR] NUM ...
# Returns the sum of the given numbers
# @arguments:
# - VAR: variable to store result (if not given, prints to stdout)
# - NUM: a valid number
function bb_sum () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local sum=0
local n
for n in "$@"; do
(( sum += n ))
done
_bb_result "$sum"
}
# function: bb_min [-v VAR] NUM ...
# Returns the minimum of the given numbers
# @arguments:
# - VAR: variable to store result (if not given, prints to stdout)
# - NUM: a valid number
function bb_min () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local min="$1"
local n
for n in "${@:2}"; do
[[ $n -lt $min ]] && min="$n"
done
_bb_result "$min"
}
# function: bb_max [-v VAR] NUM ...
# Returns the maximum of the given numbers
# @arguments:
# - VAR: variable to store result (if not given, prints to stdout)
# - NUM: a valid number
function bb_max () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local max="$1"
local n
for n in "${@:2}"; do
[[ $n -gt $max ]] && max="$n"
done
_bb_result "$max"
}
# function: bb_abs [-v VAR] NUM
# Returns the absolute value of a given number
# @arguments:
# - VAR: variable to store result (if not given, prints to stdout)
# - NUM: a valid number
function bb_abs () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local abs="$1"
[[ $abs -lt 0 ]] && abs="${abs#-}"
_bb_result "$abs"
}
# function: bb_isint NUM ...
# Checks if all the given numbers are valid integers
# @arguments:
# - NUM: a number to check
# @returns: 0 if all arguments are integers, 1 otherwise
function bb_isint () {
local re='^-*[0-9]+$'
local n
for n in "$@"; do
[[ $n =~ $re ]] || return "$__bb_false"
done
return "$__bb_true"
}
# function: bb_hex2dec [-V LISTVAR] NUM ...
# Converts numbers from hexademical (base 16) to decimal (base 10)
# @arguments:
# - LISTVAR: list variable to store result (if not given, prints to stdout)
# - NUM: a number to convert
# @returns: 1 if any number is invalid hexadecimal, 0 otherwise
function bb_hex2dec () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local n
local res=()
for n in "$@"; do
[[ $n == '0' ]] && { res+=("$n"); continue; }
(( n=16#$n )) 2>/dev/null || return "$__bb_false"
res+=( "$n" )
done
_bb_result "${res[@]}"
return "$__bb_true"
}
# function: bb_dec2hex [-V LISTVAR] NUM ...
# Converts numbers from decimal (base 10) to hexademical (base 16)
# @arguments:
# - LISTVAR: list variable to store result (if not given, prints to stdout)
# - NUM: a number to convert
# @returns: 1 if any number is invalid decimal, 0 otherwise
function bb_dec2hex () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local n
local res=()
for n in "$@"; do
printf -v n '%x' "$n" 2>/dev/null || return "$__bb_false"
res+=( "$n" )
done
_bb_result "${res[@]}"
return "$__bb_true"
}
# function: bb_oct2dec [-V LISTVAR] NUM ...
# Converts numbers from octal (base 8) to decimal (base 10)
# @arguments:
# - LISTVAR: list variable to store result (if not given, prints to stdout)
# - NUM: a number to convert
# @returns: 1 if any number is invalid octal, 0 otherwise
function bb_oct2dec () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local n
local res=()
for n in "$@"; do
[[ $n == '0' ]] && { res+=("$n"); continue; }
(( n=8#$n )) 2>/dev/null || return "$__bb_false"
res+=( "$n" )
done
_bb_result "${res[@]}"
return "$__bb_true"
}
# function: bb_dec2oct [-V LISTVAR] NUM ...
# Converts numbers from decimal (base 10) to octal (base 8)
# @arguments:
# - LISTVAR: list variable to store result (if not given, prints to stdout)
# - NUM: a number to convert
# @returns: 1 if any number is invalid decimal, 0 otherwise
function bb_dec2oct () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local n
local res=()
for n in "$@"; do
printf -v n '%o' "$n" || return "$__bb_false"
res+=( "$n" )
done
_bb_result "${res[@]}"
return "$__bb_true"
}