generated from bssw-tutorial/hello-numerical-world
-
Notifications
You must be signed in to change notification settings - Fork 8
/
check.sh
executable file
·54 lines (51 loc) · 1.25 KB
/
check.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
#!/bin/bash
if [[ -z "$1" ]]; then
echo "Specify input file as 1rst arg"
exit 1
fi
rdfile=$1
#
# Use absolute error by default. Relative
# error if specified tolerance is negative.
#
relerr=0
errbnd=1e-7
if [[ -n "$2" ]]; then
errbnd=$2
t=$(perl -e "print $2<0")
if [[ "$t" == "1" ]]; then
errbnd="-$2"
relerr=1
fi
fi
#
# Loop over lines. Use perl's arithmetic functions to
# do needed arithmetic operations. Handle absolute and
# relative diffs and special case at zero.
#
while IFS= read line; do
if [[ "$line" =~ "#" ]]; then
continue
fi
xval=$(echo $line | tr -s ' ' | cut -d' ' -f1)
yval=$(echo $line | tr -s ' ' | cut -d' ' -f2)
if [[ "$xval" == "0" ]]; then
t=$(perl -e "print $yval>($2)")
if [[ "$t" -eq 1 ]]; then
echo "Check failed at $xval $yval"
exit 1
fi
continue
fi
ratio=$(perl -e "print abs(1-($yval/$xval))<=($errbnd)")
if [[ $relerr -eq 1 ]]; then
diff=$(perl -e "print abs(2*($yval-$xval)/($yval+$xval))<=($errbnd)")
else
diff=$(perl -e "print abs($yval-$xval)<=($errbnd)")
fi
if [[ $diff -ne 1 ]]; then
echo "Check failed at $xval $yval"
exit 1
fi
done < $rdfile
exit 0