-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash-calc.sh
executable file
·88 lines (72 loc) · 1.7 KB
/
bash-calc.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
#!/bin/bash
#use this when on star
#use regular when on hive
# This is intended to be an include file
# and not executed as a script
# dependencies bc 1.06
if [ -f ~cs9e-1/bin/arch/sun4u/bc ]; then
alias bc="~cs9e-1/bin/arch/sun4u/bc" #expecting bc 1.06
fi
# bashcalc <expression>
# This function simply passes in the given expressions to 'bc -l' and
#prints the result
. assert.sh
function bashcalc {
echo "$1" | bc -l
}
# sine <expression>
# This function prints the sine of the given expression
function sine {
echo "s ($1)" | bc -l
}
# cosine <expression>
# This function prints the cosine of the given expression
function cosine {
echo "c ($1)" | bc -l
}
# angle_reduce <angle>
# Prints the angle given expressed as a value between 0 and 2pi
function angle_reduce {
pipi=$(echo "8*a(1)" | bc -l)
integr=$(echo "scale = 0;$1/$pipi" | bc -l)
ans=$(echo "$1 - $integr*$pipi" | bc -l)
if [ $(float_lte $ans 0) -eq 1 ]; then
ans=$( echo "$ans + $pipi" | bc -l)
fi
echo "$ans"
}
function float_lte {
echo $1 '<=' $2 | bc -l
}
function float_lt {
echo $1 '<' $2 | bc -l
}
function float_eq {
echo $1 '==' $2 | bc -l
}
#additional functions not required
function float_gt {
echo $1 '>' $2 | bc -l
}
function float_gte {
echo $1 '>=' $2 | bc -l
}
function add {
echo $1 '+' $2 | bc -l
}
function subtract {
echo $1 '-' $2 | bc -l
}
function multiply {
echo $1 '*' $2 | bc -l
}
function divide {
echo $1 '/' $2 | bc -l
}
#=== Tests ===#
PI=$(echo "4*a(1)" | bc -l)
THREE_PI=$(echo "3*$PI" | bc -l)
NEG_THREE_PI=$( subtract 0 $THREE_PI )
cond="$(float_eq $(angle_reduce $THREE_PI) $PI) -eq 1"
cond="$(float_eq $(angle_reduce $NEG_THREE_PI) $PI) -eq 1"
assert "$cond" $LINENO