473,545 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to calculate the number of looping?

19 New Member
i use vb 6.0


Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Explicit
  3.  
  4. Dim Error As Double
  5. Dim x As Integer
  6. Dim y As Double
  7. Dim z As Integer
  8. Dim eps As Double
  9. Dim N As Integer
  10.  
  11. Private Sub cmdCalculate_Click()
  12.  
  13. 'user input
  14.  
  15. If IsNumeric(txtN) And IsNumeric(txtEps) And IsNumeric(txtX) Then
  16. N = txtN.Text
  17. eps = txtEps.Text
  18. x = txtX.Text
  19. Else
  20.     MsgBox "all input must be numeric", vbExclamation, "numeric test"
  21.     txtN.SetFocus
  22. End If
  23.  
  24. 'calculate
  25.  
  26. Do
  27.     y = (x) - (x - (N ^ (1 / 2)))
  28.  
  29.     Error = y - x
  30.     If Error < 0 Then
  31.         Error = Error * -1
  32.     End If
  33.  
  34.   z = z + 1
  35.  
  36. Loop Until Error < eps
  37.  
  38. 'display the result
  39. lblDisplay.Caption = "root : " & y & "  iteration : " & z
  40.  
  41.  
  42.  
  43. End Sub
  44.  
  45. Private Sub cmdExit_Click()
  46. Unload Me
  47. End Sub
  48.  
the problems..

the 'eps' is the decimal places.in my program above it need user to insert the number of decimal places like this '0.001'.. but actually i just want the user put the value as '3'...

the iteration value must be in integer data type as the result of number of looping it takes to complete the loop.. the number of looping (iteration) didn't match its actually value..
Feb 26 '08 #1
6 2315
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi vbwire. It would be helpful to know what the loop calculation is for. When I test it the calculated error value remains constant and the loop does not exit.

I wonder if your reference to root N (N ^ (1/2)) in the loop is correct? N is not changing after it is entered by the user, and I guess there should a use of the loop counter z somewhere to change the value of N if the iterative calculation is intended to reduce the error value below the loop threshold. This is just a guess, though.

To refer to the number of decimal places for the loop threshold as an integer value you could define a variable NDecimals as an integer, get the value of NDecimals from the user instead of the double eps, then replace your loop exit test with
Expand|Select|Wrap|Line Numbers
  1. Loop Until Error < 10^(-NDecimals)
Another change is to use the Abs (absolute value) function to replace
Expand|Select|Wrap|Line Numbers
  1. Error = y - x
  2. If Error < 0 Then 
  3. Error = Error * -1
  4. End If
with
Expand|Select|Wrap|Line Numbers
  1. Error = abs(y-x)
Regards

Stewart
Feb 26 '08 #2
Killer42
8,435 Recognized Expert Expert
As far as I could see, z should correctly return the number of iterations.

As for the calculation, I haven't looked into it in detail. But if you have any Integer data types in there, VB may convert interim results to Integer, thus losing precision. Try using all Double data types in there. And that includes for the literal values. If you say "1#" instead of just "1", you will force VB to use double data type for the value. In any case, why use (1/2), when you could just use the actual value, 0.5?

Oh, one other thing. Between lines 20 and 21, I believe you should Exit Sub.
Feb 26 '08 #3
vbwire
19 New Member
Hi vbwire. It would be helpful to know what the loop calculation is for. When I test it the calculated error value remains constant and the loop does not exit.
the loop calculate the value for y until the different between y and x is smaller than 'eps' ..

z is the number of looping it takes to complete the loop..

the value of x will take the value of y for the next looping.. for example in the first looping, x=2, then get the value of y.. then it will loop,use the value of y we get to calculate the new y. its continue give a new y until the different of the new y and y before it smaller than 'eps'
Feb 27 '08 #4
MikeTheBike
639 Recognized Expert Contributor
the loop calculate the value for y until the different between y and x is smaller than 'eps' ..

z is the number of looping it takes to complete the loop..

the value of x will take the value of y for the next looping.. for example in the first looping, x=2, then get the value of y.. then it will loop,use the value of y we get to calculate the new y. its continue give a new y until the different of the new y and y before it smaller than 'eps'
Hi
I started to look at this yesterday, but work got in the way (such a pain!).

Apart from the use of Abs() and changing the loop exit condition to

Loop Until Error < 0.1 ^ eps

there does not seem to be any iteration in the loop, ie. nothing changes from one loop to the next! So I've moded the code as follows:-

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim Error As Double
  4. 'Dim x As integer
  5. Dim x As Double
  6. Dim y As Double
  7. Dim z As Integer
  8. Dim eps As Double
  9. Dim N As Integer
  10.  
  11. Private Sub cmdCalculate_Click()
  12.  
  13.     Dim txtX As Integer
  14.     'Dim txtY As Double
  15.     Dim txtZ As Integer
  16.     Dim txtN As Integer
  17.     Dim txtEps As Integer
  18.  
  19.  
  20. 'user input
  21.     txtX = 3
  22.     txtZ = 5
  23.     txtN = 1000
  24.     txtEps = 3
  25.  
  26.     z = 0
  27.     If IsNumeric(txtN) And IsNumeric(txtEps) And IsNumeric(txtX) Then
  28.     N = txtN
  29.     eps = txtEps
  30.     x = txtX
  31.     Else
  32.         MsgBox "all input must be numeric", vbExclamation, "numeric test"
  33.         'txtN.SetFocus
  34.         Exit Sub
  35.     End If
  36.  
  37.     'calculate
  38.  
  39.     Do
  40.         y = (x) - (x - (N ^ (1 / 2)))
  41.  
  42.         Error = Abs(y - x)
  43.         x = y
  44.         z = z + 1
  45.  
  46.     Loop Until Error < 0.1 ^ eps
  47.  
  48.     'display the result
  49.     'lblDisplay.Caption = "root : " & y & "  iteration : " & z
  50.     MsgBox "root : " & y & "  iteration : " & z
  51.  
  52. End Sub
ie have added x=y in the loop (as you indicted in the last post), but this always calculated the squre root of N in 2 iterations !!??

I still don't know what you are trying to do ?

Is this homework ?

ps. as this is not written in a form module I've hard coded values of txtN etc.



MTB
Feb 27 '08 #5
vbwire
19 New Member
thank to both of you..

this is the program after i corrected it..

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim Error As Double
  4. Dim x As Double
  5. Dim y As Double
  6. Dim z As Integer
  7. Dim eps As Double
  8. Dim a As Integer
  9. Dim N As Integer
  10.  
  11. Private Sub cmdCalculate_Click()
  12.  
  13. 'user input
  14.  
  15. If IsNumeric(txtN) And IsNumeric(txtEps) And IsNumeric(txtX) Then
  16. N = txtN.Text
  17. a = txtEps.Text
  18. x = txtX.Text
  19. Else
  20.     MsgBox "all input must be numeric", vbExclamation, "numeric test"
  21.     txtN.SetFocus
  22. End If
  23.  
  24. 'calculate
  25.  
  26. eps = (10 ^ (-a))
  27.  
  28. Do
  29.     y = x - (x - (N ^ (1 / 3)))
  30.  
  31.     Error = Abs(y - x)
  32.  
  33.     x = y
  34.  
  35.     z = z + 1
  36.  
  37. Loop Until Error < eps
  38.  
  39. 'display the result
  40. lblDisplay.Caption = "root : " & y & "             iteration : " & z
  41.  
  42. End Sub
  43.  
  44. Private Sub cmdExit_Click()
  45. Unload Me
  46. End Sub
  47.  
  48.  
please look at my code for displaying the result.. how to make it in two line but i want it in same label.

also.. i want to display the result for root in the decimal palaces chosen by user,'eps'..
Feb 27 '08 #6
vbwire
19 New Member

ie have added x=y in the loop (as you indicted in the last post), but this always calculated the squre root of N in 2 iterations !!??

I still don't know what you are trying to do ?

Is this homework ?

ps. as this is not written in a form module I've hard coded values of txtN etc.



MTB
thank you mike.. =)

i just realise my mistake in the calculation..

Expand|Select|Wrap|Line Numbers
  1. y = (x) - (x - (N ^ (1 / 2)))
  2.  
its actually..

Expand|Select|Wrap|Line Numbers
  1. y = x - (x - (N ^ (1 / 3)))
  2.  
and check my previous post.. i still have something for you..haha..
Feb 27 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3998
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum default is 4 letters). This is the Fed. Everything is a TLA (three-letter acronym). Therefore, since I'm building a PORTABLE web application,...
1
9972
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a number. That number will appear in a seperate box at the bottom. So basically whatever you choose has a corresponding number associated with it...
53
5660
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
7
2819
by: rick | last post by:
Can anyone help, I am try to create a simple form using a table, where a user can fill out quanty and price and have a total automatically calculated and inserted in another field. I stuck trying to figure out how expand this script to recalculate when rows are added or removed. My code so far. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML...
4
6218
by: Qwert | last post by:
Hello, I do: Debug.WriteLine("A: " & DateTime.Now.Ticks.ToString) REM Calculate a bunch of stuff. Some loops and math functions. Debug.WriteLine("B: " & DateTime.Now.Ticks.ToString) but both A and B have the exact the same value. Is this method incorrect? When I make the function wait for 1 millisecond, A en B differ. Is there
5
2216
by: milan.letic | last post by:
Hello, I believe that this has been answered somewhere but I cannot find it. I have 51 textboxes on page. In 50 I can enter value. 51st should tell me what is average of entered numbers. As user types I want script to iterate trough form count and sum non-empty fields and than divides this two numbers.I need to sum values from fields that...
20
2802
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1 then
6
1364
by: happyhondje | last post by:
Hello everyone, I've got a little issue, both programming and performance-wise. I have a set, containing objects that refer to other sets. For example, in a simple notation: (<a, b, c>, <d, e>) (or in a more object-like display: set(obj1.choices=set(a, b, c) ). There may be obj1..objN objects in the outer set, and the amount of items in the...
2
3366
by: AccessHunter | last post by:
Hi, I have a table (Admittance) that has birth dates of all cases admitted, fields being (Case Number and Birth Date). I also have a form that asks the user to select a year and click the 'Find' button to get a list of all the Cases that were 18 yrs old on the year selected. The way I want to do this is, invoke an event on 'Find' button...
0
7432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7689
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.