I am trying to convert some old code into Basic4ppc (I have working versions in QuickBasic and VBdos). The program deals with the Standard Normal Distribution and will compute the area corresponding to a Z score, or a Z score corresponding to an area.
After much tweaking of the code, I am almost there. Going from Z score to area works fine. But going from area to Z score generates a strange error message. The really strange part is the point in the program at which the error message occurs. Here is the relevant section of code (the full program is attached):
35 Private Sub CalcZ()
36 Form1.Text = "Calculate Z for Given Area"
37
38 target = AreaText.Text
39 If target = 0 Then Return
40
41 If Option1.Value = True Then
42 target = target * 2
43 t$ = "Z (one-tailed) = "
44 Else
45 t$ = "Z (two-tailed) = "
46 End If
47
48 z = 0
49 OldTR = 0
50
51 For i = 1 To 10
52 z = z + 1 * 10 ^ (-i)
53
54
55 calcit:
56
57 tr = gammq(0.5, (z ^ 2) / 2)
58
59 ' prog hits infinate Loop when tr > target
60 ' AND tr-target less than machine precision.
61 ' This If block takes care of it.
62
63 If OldTR = tr Then
64 OldTR = 0
65 tr = target
66 Else: OldTR = tr
67 End If
68
69 If tr > target Then
70 z = z + 1 * 10 ^ (-i)
71 Goto calcit
72 End If
73 z = z - 1 * 10 ^ (-i)
74 Next i
75
76 ResultsText.Text = t$ + Format(z, "N8")
77
78 End Sub
79
80 Private Sub Command1_Click()
81 PrecLev = 12
82 PrecLev = PrecLev + 1
83
84 CalcArea
85
86 End Sub
87
88 Private Sub Command2_Click()
89 PrecLev = 12
90 PrecLev = PrecLev + 1
91
92 CalcZ
93 End Sub
The subroutine CalcZ (starting at line 35) is called at line 92. I put a breakpoint on line 74 and checked the values at that point. Everything is correct, Z is 1.9 at that point and i = 1. When I press F8 what should happen is i increments to 2, control is passed to line 51, and the next significant digit of Z should be computed. Instead, when I press F8 I get the following error message:
==================
An error occured on sub main.command2_click.
Line number 92
CalcZ
Error description:
Length cannot be less than zero.
Parameter name: length
Continue?
==================
I don't understand how executing line 74 can cause an error on line 92, much less understand what the error is.
Can anyone help?
After much tweaking of the code, I am almost there. Going from Z score to area works fine. But going from area to Z score generates a strange error message. The really strange part is the point in the program at which the error message occurs. Here is the relevant section of code (the full program is attached):
35 Private Sub CalcZ()
36 Form1.Text = "Calculate Z for Given Area"
37
38 target = AreaText.Text
39 If target = 0 Then Return
40
41 If Option1.Value = True Then
42 target = target * 2
43 t$ = "Z (one-tailed) = "
44 Else
45 t$ = "Z (two-tailed) = "
46 End If
47
48 z = 0
49 OldTR = 0
50
51 For i = 1 To 10
52 z = z + 1 * 10 ^ (-i)
53
54
55 calcit:
56
57 tr = gammq(0.5, (z ^ 2) / 2)
58
59 ' prog hits infinate Loop when tr > target
60 ' AND tr-target less than machine precision.
61 ' This If block takes care of it.
62
63 If OldTR = tr Then
64 OldTR = 0
65 tr = target
66 Else: OldTR = tr
67 End If
68
69 If tr > target Then
70 z = z + 1 * 10 ^ (-i)
71 Goto calcit
72 End If
73 z = z - 1 * 10 ^ (-i)
74 Next i
75
76 ResultsText.Text = t$ + Format(z, "N8")
77
78 End Sub
79
80 Private Sub Command1_Click()
81 PrecLev = 12
82 PrecLev = PrecLev + 1
83
84 CalcArea
85
86 End Sub
87
88 Private Sub Command2_Click()
89 PrecLev = 12
90 PrecLev = PrecLev + 1
91
92 CalcZ
93 End Sub
The subroutine CalcZ (starting at line 35) is called at line 92. I put a breakpoint on line 74 and checked the values at that point. Everything is correct, Z is 1.9 at that point and i = 1. When I press F8 what should happen is i increments to 2, control is passed to line 51, and the next significant digit of Z should be computed. Instead, when I press F8 I get the following error message:
==================
An error occured on sub main.command2_click.
Line number 92
CalcZ
Error description:
Length cannot be less than zero.
Parameter name: length
Continue?
==================
I don't understand how executing line 74 can cause an error on line 92, much less understand what the error is.
Can anyone help?