Public Const Dpi As Double = 6.28318530717959 '360 degrees
Public Const Pi As Double = 3.14159265358979 '180 degrees
Public Const Hpi As Double = 1.5707963267949 ' 90 degrees
Public Const Qpi As Double = 0.785398163397448 ' 45 degrees
Public Angle72 As Double 'Angles in radians
Public Angle120 As Double 'for root convergence tests
Public Angle144 As Double '90, 180, & 360 constants also used
Public Angle216 As Double
Public Angle240 As Double
Public Angle270 As Double
Public Angle288 As Double
[b]. . .[/b]
Angle72 = Dpi / 5#
Angle144 = 2# * Angle72
Angle216 = 3# * Angle72
Angle288 = 4# * Angle72
Angle120 = Dpi / 3
Angle240 = 2 * Angle120
Angle270 = 3# * Dpi / 4
ComplexOne.Real = 1 'Used with Quotient Function for reciprocals
ComplexOne.Imaginary = 0
[b]. . .[/b]
Public Function Root(N As Complex) As Byte
Dim CurrentP As Complex 'Current & next for Newton iteration
Dim NextP As Complex
Dim Power As Complex 'Divisor for Newton iteration
'Use Polar form of NextP for convergence test
Dim Rsquare As Double 'Square of magnitude
Dim Angle As Double 'Angle
Dim J As Integer
Dim TintNumber As Byte
Dim NotYet As Boolean
CurrentP = N
For J = 1 To 50
Power = Product(CurrentP, CurrentP)
Power = Product(Power, Power)
CurrentP.Real = 4# * CurrentP.Real
CurrentP.Imaginary = 4# * CurrentP.Imaginary
NextP = Sum(CurrentP, Quotient(ComplexOne, Power))
NextP.Real = NextP.Real / 5#
NextP.Imaginary = NextP.Imaginary / 5#
Rsquare = NextP.Real * NextP.Real + NextP.Imaginary * NextP.Imaginary
Angle = Longitude(NextP.Imaginary, NextP.Real)
Select Case True
Case Abs(Rsquare - 1) > 0.00000001 'Check for radius near one
NotYet = True
Case Abs(Angle) < 0.0000001 'Check for angle near zero
Root = 0
NotYet = False
Case Abs(Angle - Dpi) < 0.0000001 'Check for angle near 360
Root = 0
NotYet = False
Case Abs(Angle - Angle72) < 0.0000001 'check for angle near 72
Root = 1
NotYet = False
Case Abs(Angle - Angle144) < 0.0000001 'check for angle near 144
Root = 2
NotYet = False
[b]. . .[/b]
Case Else
NotYet = True
End Select
Select Case True
Case NotYet
CurrentP = NextP
Case J < BlackTally
Root = BlackNumber
Exit Function
Case Else
Exit Function
End Select
Next J
Root = GrayNumber
End Function