27 lines
723 B
Plaintext
27 lines
723 B
Plaintext
|
#!/bin/sh
|
||
|
|
||
|
! eq $# 2 && printf "Usage: $0 [base] [exponent]\n" >/dev/stderr && exit 1
|
||
|
|
||
|
! stris int "$1" && printf -- "%b: %b: Not an integer\n" "$0" "$1" >/dev/stderr && exit 1
|
||
|
! stris int "$2" && printf -- "%b: %b: Not an integer\n" "$0" "$2" >/dev/stderr && exit 1
|
||
|
|
||
|
base=$1
|
||
|
exponent=$2
|
||
|
|
||
|
result=1
|
||
|
|
||
|
while gt $exponent 0; do
|
||
|
result=$(multiply $result $base)
|
||
|
exponent=$(-- $exponent)
|
||
|
done
|
||
|
|
||
|
if ! eq $result $(abs $result) && eq $base $(abs $base); then
|
||
|
printf "$0: Result is unexpectedly negative, possible integer overflow\n" >/dev/stderr && exit 1
|
||
|
elif eq $result 0 && ! eq $base 0; then
|
||
|
printf "$0: Result is unexpectedly zero, possible integer overflow\n" >/dev/stderr && exit 1
|
||
|
else
|
||
|
printf "%b\n" $result
|
||
|
fi
|
||
|
|
||
|
exit 0
|