どうすれば次のようになりますか:
ceiling(N/500)
数値を表す N。
しかし、Linux Bash スクリプトでは
外部スクリプト言語を使用する理由 デフォルトでフロアを取得します。ceil を取得するには、次のようにします。
$ divide=8; by=3; (( result=(divide+by-1)/by )); echo $result
3
$ divide=9; by=3; (( result=(divide+by-1)/by )); echo $result
3
$ divide=10; by=3; (( result=(divide+by-1)/by )); echo $result
4
$ divide=11; by=3; (( result=(divide+by-1)/by )); echo $result
4
$ divide=12; by=3; (( result=(divide+by-1)/by )); echo $result
4
$ divide=13; by=3; (( result=(divide+by-1)/by )); echo $result
5
....
負の数を考慮に入れるには、少し強化することができます。おそらくもっときれいな方法がありますが、初心者向けです
$ divide=-10; by=10; neg=; if [ $divide -lt 0 ]; then (( divide=-divide )); neg=1; fi; (( result=(divide+by-1)/by )); if [ $neg ]; then (( result=-result )); fi; echo $result
-1
$ divide=10; by=10; neg=; if [ $divide -lt 0 ]; then (( divide=-divide )); neg=1; fi; (( result=(divide+by-1)/by )); if [ $neg ]; then (( result=-result )); fi; echo $result
1
( に切り替えるように編集しlet ...
ました(( ... ))
。)
ceil 関数を使用してスクリプト言語を呼び出します。与えられた$NUMBER
:
python -c "from math import ceil; print ceil($NUMBER/500.0)"
また
perl -w -e "use POSIX; print ceil($NUMBER/500.0), qq{\n}"
bc を使用したソリューションを次に示します (これはほぼすべての場所にインストールする必要があります)。
ceiling_divide() {
ceiling_result=`echo "($1 + $2 - 1)/$2" | bc`
}
これは純粋にbashの別のものです:
# Call it with two numbers.
# It has no error checking.
# It places the result in a global since return() will sometimes truncate at 255.
# Short form from comments (thanks: Jonathan Leffler)
ceiling_divide() {
ceiling_result=$((($1+$2-1)/$2))
}
# Long drawn out form.
ceiling_divide() {
# Normal integer divide.
ceiling_result=$(($1/$2))
# If there is any remainder...
if [ $(($1%$2)) -gt 0 ]; then
# rount up to the next integer
ceiling_result=$((ceiling_result + 1))
fi
# debugging
# echo $ceiling_result
}
awkを使用できます
#!/bin/bash
number="$1"
divisor="$2"
ceiling() {
awk -vnumber="$number" -vdiv="$divisor" '
function ceiling(x){return x%1 ? int(x)+1 : x}
BEGIN{ print ceiling(number/div) }'
}
ceiling
出力
$ ./shell.sh 1.234 500
1
または、選択肢がある場合は、Zsh などの浮動小数点を処理するより優れたシェルを使用できます。
integer ceiling_result
ceiling_divide() {
ceiling_result=$(($1/$2))
echo $((ceiling_result+1))
}
ceiling_divide 1.234 500
数学的には、天井の機能は床、天井(x) = -床(-x)で定義できます。また、正の浮動小数点数を整数に変換するときは、floor がデフォルトです。
if [ $N -gt 0 ]; then expr 1 - $(expr $(expr 1 - $N) / 500); else expr $N / 500; fi
参考文献 https://en.wikipedia.org/wiki/Floor_and_ceiling_functions
Kalle's great answerを少し拡張すると、関数にうまくパックされたアルゴリズムが次のようになります。
ceildiv() {
local num=$1
local div=$2
echo $(( (num + div - 1) / div ))
}
またはワンライナーとして:
ceildiv(){ echo $((($1+$2-1)/$2)); }
凝ったものにしたい場合は、より堅牢なバージョンを使用して入力を検証し、入力が数値であるかどうかを確認し、負の数も処理できます。
ceildiv() {
local num=${1:-0}
local div=${2:-1}
if ! ((div)); then
return 1
fi
if ((num >= 0)); then
echo $(( (num + div - 1) / div ))
else
echo $(( -(-num + div - 1) / div ))
fi
}
これは、負の数に「偽の」ceil を使用して、最大の絶対整数、つまり -10 / 3 = -4 を使用し、-3 > -4 のように本来あるべき -3 ではありません。「真の」天井が必要な場合は、$(( num / div ))
代わりにelse
そして、次のように使用します。
$ ceildiv 10 3 4 $ceildiv 501 500 2 $ceildiv 0 3 0 $ ceildiv -10 1 -10 $ ceildiv -10 3 -4
これは、Awkを使用した簡単なソリューションです。
($ a / $ b)の天井が必要な場合は
echo "$a $b" | awk '{print int( ($1/$2) + 1 )}'
と床の使用
echo "$a $b" | awk '{print int($1/$2)}'
行の最初のフィールドとして被除数「$a」をawkにエコーし、2番目のフィールドとして除数「$b」をエコーすることに注意してください。
除算が浮動小数点以外の数値を返す場合、この関数は 1 を追加しません。
function ceiling {
DIVIDEND=${1}
DIVISOR=${2}
if [ $(( DIVIDEND % DIVISOR )) -gt 0 ]; then
RESULT=$(( ( ( $DIVIDEND - ( $DIVIDEND % $DIVISOR ) ) / $DIVISOR ) + 1 ))
else
RESULT=$(( $DIVIDEND / $DIVISOR ))
fi
echo $RESULT
}
次のように使用します。
echo $( ceiling 100 33 )
> 4
より簡潔な Awk ロジック
awk '
function ceil(ip) {
print ip%1 ? int(ip)+1 : ip
}
BEGIN {
ceil(1000/500)
ceil(1001/500)
}
'
結果
2 3
より簡潔な Awk ロジック
awk '
function ceil(ip) {
print ip%1 ? int(ip)+1 : ip
}
BEGIN {
ceil(1000/500)
ceil(1001/500)
}
'
結果
2 3