-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathQuadraticSolver.rvb
45 lines (39 loc) · 1.26 KB
/
QuadraticSolver.rvb
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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' QuadraticSolver.rvb -- September 2009
' If this code works, it was written by Dale Fugier.
' If not, I don't know who wrote it.
' Works with Rhino 4.0.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' QuadraticSolver
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function QuadraticSolver(a, b, c)
Dim d, s0, s1
d = b * b - 4 * a * c
If d < 0 Then
' No real solution
QuadraticSolver = Null
Else
s0 = (-b - Sqr(d)) / (2 * a)
s1 = (-b + Sqr(d)) / (2 * a)
If Abs(s0) < Abs(s1) Then s0 = s1
s1 = c / (a * s0)
QuadraticSolver = Array(s0,s1)
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Tester
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub TestQuadraticSolver
Dim rc
rc = QuadraticSolver(2, 5, -12)
If IsArray(rc) Then
Rhino.Print rc(0) & ", " & rc(1)
End If
rc = QuadraticSolver(1, -10000000.0000001, 1)
If IsArray(rc) Then
Rhino.Print rc(0) & ", " & rc(1)
End If
End Sub
TestQuadraticSolver