Results 1 to 3 of 3

Thread: Problems with calculating value

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    Slovenia
    Posts
    16

    Problems with calculating value

    I have a problem...
    I want clculate value of polynomial at some x (number). And if value of some polynomial is bigger than 0 then print x to text2.text if value is smaller than 0 then print x to text3.text.

    Code:
    Private Sub Command1_Click()
    Dim x As Single
    Dim p As Single
    Dim a, b, c, d, e, f, g, h, i, j, k
    
    a = Text1(0).Text
    b = Text1(1).Text
    c = Text1(2).Text
    d = Text1(3).Text
    e = Text1(4).Text
    f = Text1(5).Text
    g = Text1(6).Text
    h = Text1(7).Text
    i = Text1(8).Text
    j = Text1(9).Text
    k = Text1(10).Text
    
    p = (k * x ^ 10 + j * x ^ 9 + i * x ^ 8 + h * x ^ 7 + g * x ^ 6 + f * x ^ 5 + e * x ^ 4 + d * x ^ 3 + c * x ^ 2 + b * x + a)
    
    For x = -1 To 1 Step 0.1
    If p < 0 Then
    Text2.Text = x
    End If
    If p > 0 Then
    Text3.Text = x
    End If
    Next x
    
    End Sub
    What is wrong?

    Btw: Does currency mean that it calculate on 4 decimals?

    Thank you

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    For starters, your calculation is outside of the loop and will only be executed 1 time. In addition, x will = 0 at the time of execution as it has not yet been initialized.

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    I assume you are trying to calculate for p base on the value of x.

    Try this.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim x As Single
    5. Dim p As Single
    6.  
    7. For x = -1 To 1 Step 0.1
    8.     p = DoMath(x)
    9.     Select Case p
    10.    
    11.     Case Is < 0
    12.         Text2.Text = Text2.Text & x & ", "
    13.     Case Is > 0
    14.         Text3.Text = Text3.Text & x & ", "
    15.     End Select
    16. Next x
    17. End Sub
    18.  
    19. Private Function DoMath(ByVal Var As Single) As Single
    20. Dim a As Single, b As Single
    21. Dim c As Single, d As Single
    22. Dim e As Single, f As Single
    23. Dim g As Single, h As Single
    24. Dim i As Single, j As Single
    25. Dim k As Single
    26.  
    27. a = CSng(Text1(0).Text)
    28. b = CSng(Text1(1).Text)
    29. c = CSng(Text1(2).Text)
    30. d = CSng(Text1(3).Text)
    31. e = CSng(Text1(4).Text)
    32. f = CSng(Text1(5).Text)
    33. g = CSng(Text1(6).Text)
    34. h = CSng(Text1(7).Text)
    35. i = CSng(Text1(8).Text)
    36. j = CSng(Text1(9).Text)
    37. k = CSng(Text1(10).Text)
    38.  
    39. DoMath = (k * (Var ^ 10)) + _
    40.          (j * (Var ^ 9)) + _
    41.          (i * (Var ^ 8)) + _
    42.          (h * (Var ^ 7)) + _
    43.          (g * (Var ^ 6)) + _
    44.          (f * (Var ^ 5)) + _
    45.          (e * (Var ^ 4)) + _
    46.          (d * (Var ^ 3)) + _
    47.          (c * (Var ^ 2)) + _
    48.          (b * Var) + a
    49. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width