Debian Codegraph @

alt-bc.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # Invoking 'bc' using command substitution
3 # in combination with a 'here document'.
4
5
6 var1=`bc << EOF
7 18.33 * 19.78
8 EOF
9 `
10 echoscip-shell system . . echo. $var1 # 362.56
11
12
13 # $( ... ) notation also works.
14 v1=23.53
15 v2=17.881
16 v3=83.501
17 v4=171.63
18
19 var2=$(bc << EOF
20 scale = 4
21 a = ( $v1 + $v2 )
22 b = ( $v3 * $v4 )
23 a * b + 15.35
24 EOF
25 )
26 echoscip-shell system . . echo. $var2 # 593487.8452
27
28
29 var3=$(bc -l << EOF
30 scale = 9
31 s ( 1.7 )
32 EOF
33 )
34 # Returns the sine of 1.7 radians.
35 # The "-l" option calls the 'bc' math library.
36 echoscip-shell system . . echo. $var3 # .991664810
37
38
39 # Now, try it in a function...
40 hypotenusescip-shell shell abs-guide 10-4 hypotenuse(). () # Calculate hypotenuse of a right triangle.
41 { # c = sqrt( a^2 + b^2 )
42 hyp=$(bc -l << EOF
43 scale = 9
44 sqrt ( $1 * $1 + $2 * $2 )
45 EOF
46 )
47 # Can't directly return floating point values from a Bash function.
48 # But, can echo-and-capture:
49 echoscip-shell system . . echo. "$hyp"
50 }
51
52 hyp=$(hypotenusescip-shell shell abs-guide 10-4 hypotenuse(). 3.68 7.31)
53 echoscip-shell system . . echo. "hypotenuse = $hyp" # 8.184039344
54
55
56 exit 0