-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadratic_equation.brz
45 lines (37 loc) · 1.47 KB
/
quadratic_equation.brz
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
define quadratic
# Expect: [a, b, c] to be defined
4 a c * * # Stack: 4*a*c
b 2 ** # Stack: 4*a*c, b**2
swap - # Stack: (b**2 - 4*a*c)
if 0 over > do # Stack: (b**2 - 4*a*c), 0 > (b**2 - 4*a*c)
"There's no real root." show
else
if 0 over = do # Stack: (b**2 - 4*a*c), 0 == (b**2 - 4*a*c)
0.5 ** # Stack: sqrt(b**2 - 4*a*c)
b -1 * # Stack: sqrt(b**2 - 4*a*c), -b
swap + # Stack: -b + sqrt(b**2 - 4*a*c)
a 2 * # Stack: -b + sqrt(b**2 - 4*a*c), 2*a
/ # Stack: (-b + sqrt(b**2 - 4*a*c)) / 2*a
"Root: $0" show
else
0.5 ** # Stack: sqrt(b**2 - 4*a*c)
b -1 * # Stack: sqrt(b**2 - 4*a*c), -b
over over # Stack: sqrt(b**2 - 4*a*c), -b, sqrt(b**2 - 4*a*c), -b
swap - # Stack: sqrt(b**2 - 4*a*c), -b, (-b - sqrt(b**2 - 4*a*c))
a 2 * # Stack: sqrt(b**2 - 4*a*c), -b, (-b - sqrt(b**2 - 4*a*c)), 2*a
/ # Stack: sqrt(b**2 - 4*a*c), -b, ((-b - sqrt(b**2 - 4*a*c)) / 2*a)
"Root 1: $0" show
over over # Stack: sqrt(b**2 - 4*a*c), -b, sqrt(b**2 - 4*a*c), -b
swap + # Stack: sqrt(b**2 - 4*a*c), -b, (-b - sqrt(b**2 - 4*a*c))
a 2 * # Stack: sqrt(b**2 - 4*a*c), -b, (-b - sqrt(b**2 - 4*a*c)), 2*a
/ # Stack: sqrt(b**2 - 4*a*c), -b, ((-b - sqrt(b**2 - 4*a*c)) / 2*a)
"Root 2: $0" show
end
end
end
# You need to specify the args using the `define` keyword.
# It's a way to define named arguments for a function.
define a 1 end
define b 5 end
define c 6 end
quadratic