473,382 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

a missed IF statement

19
Hi

I have a loop that is iterating at approx 1000 times per second.

There is an IF statement nested inside that instructs the program to stop (return) if a variable < 5. However, the program seems to always miss this condition. Even stranger, for any value > 5, the IF condition is properly followed. The variable in question is starting out from a very large value and with each iteration of the loop, being descreased a certain amount. The variable never goes below zero.

Wondering if this is something wrong with my algorithm, or machine, or compiler..or all of the above =(

Not sure if this makes sense. I can elaborate. Please help if you can.

Thanks.
Oct 11 '06 #1
9 1782
Banfa
9,065 Expert Mod 8TB
Most likely something wrong with your algorithm or code.
Oct 11 '06 #2
Can you include some of the code from the project so that we can see where there might be an error in the algorithm?
Oct 11 '06 #3
sanou
19
Expand|Select|Wrap|Line Numbers
  1. public: static double 
  2.  
  3. // constants
  4. degrees = 45,
  5. radians = 0.0174532925 * degrees,
  6.  
  7. // general time counters, i=1 means 1 tick per millisecond; 1000 ticks = 1 sec
  8. i = 0,
  9. time = 0,
  10.  
  11. // unique time components for x and y
  12. x_i = 0,
  13. x_time = 0,
  14. y_i = 0,
  15. y_time = 0,
  16.  
  17. // direction vector
  18. y_current_direction = -1,
  19. x_current_direction = 1,
  20.  
  21. // gravity = 9.8 m/s^2
  22. gravity = y_current_direction * 50 ,  //default gravity is 9.8
  23.  
  24. // velocity = some value with units m/s
  25. velocity = 137,
  26.  
  27. // y values
  28. y_initial_position = 386,
  29. y_current_position = 0,
  30.  
  31. y_velocity = sin(radians) * velocity,
  32.  
  33. y_max = (0 - (y_velocity * y_velocity)) / (2 * gravity),
  34.  
  35. // x values
  36. x_initial_position = 160,  
  37. x_current_position = 0,
  38.  
  39. x_velocity = cos(radians) * velocity,
  40.  
  41. // box dimensions
  42. top = 115,
  43. left = 160,
  44. right = 750,
  45. bottom = 386,
  46.  
  47. // ball physical features
  48. mass = 1,
  49. friction = 0.8;
  50.  
  51. private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  52.  
  53.  if(timer1->Enabled == false){
  54.      timer1->Enabled = true;
  55.      button1->Text = L"Stop!";
  56.  }
  57.  else{
  58.      timer1->Enabled = false;
  59.      button1->Text = L"Start!";
  60.  }
  61. }
  62.  
  63.  
  64. private: System::Void myTimer_Tick(System::Object^  sender, System::EventArgs^  e) {
  65.  
  66. double    x = y_max,
  67. reset_x_time = 0,
  68. reset_y_time = 0;    
  69.  
  70. time = i/10;
  71. x_time = x_i/10;
  72. y_time = y_i/10;
  73.  
  74. x_current_position = ((x_initial_position) + (x_velocity * x_current_direction * x_time));
  75. y_current_position =  (2 * (y_initial_position)) + (direction_adjust * (y_initial_position + (y_velocity * y_time) + (0.5 * gravity * y_time * y_time)));
  76.  
  77. if(y_current_position > bottom){
  78.     y_current_position = bottom;
  79.     reset_y_time = (2 * ((0 - y_velocity)/gravity)) - y_time;
  80.     y_time = abs(reset_y_time);
  81.     y_i = abs(reset_y_time * 10);
  82. // coefficient of restitution!
  83.     y_velocity *= 0.7;
  84. // friction coefficient!!
  85.     x_velocity *= friction;
  86.     x_initial_position = x_current_position;
  87.     x_time = x_i = reset_x_time;
  88. }
  89.  
  90. if(x_current_position > right){
  91.     x_initial_position = x_current_position = right;
  92.     x_current_direction *= -1;
  93.     x_time = x_i = reset_x_time;
  94. }
  95.  
  96. if(x_current_position < left){
  97.     x_initial_position = x_current_position = left;
  98.     x_current_direction *= -1;
  99.     x_time = x_i = reset_x_time;
  100. }
  101.  
  102. pictureBox1->Location = System::Drawing::Point((int)x_current_position, (int)y_current_position);
  103.  
  104. ///////////////////////////////////////////////////
  105. /* THIS CODE SEEMS TO BE MISSED?*/
  106.  
  107. if(x_initial_velocity < 5){
  108.    return;
  109. }
  110.  
  111. ///////////////////////////////////////////////////
  112.  
  113. x_i++;
  114. y_i++;
  115.  
  116. }
Oct 11 '06 #4
Expand|Select|Wrap|Line Numbers
  1. public: static double 
  2.  
  3. // constants
  4. degrees = 45,
  5. radians = 0.0174532925 * degrees,
  6.  
  7. // general time counters, i=1 means 1 tick per millisecond; 1000 ticks = 1 sec
  8. i = 0,
  9. time = 0,
  10.  
  11. // unique time components for x and y
  12. x_i = 0,
  13. x_time = 0,
  14. y_i = 0,
  15. y_time = 0,
  16.  
  17. // direction vector
  18. y_current_direction = -1,
  19. x_current_direction = 1,
  20.  
  21. // gravity = 9.8 m/s^2
  22. gravity = y_current_direction * 50 ,  //default gravity is 9.8
  23.  
  24. // velocity = some value with units m/s
  25. velocity = 137,
  26.  
  27. // y values
  28. y_initial_position = 386,
  29. y_current_position = 0,
  30.  
  31. y_velocity = sin(radians) * velocity,
  32.  
  33. y_max = (0 - (y_velocity * y_velocity)) / (2 * gravity),
  34.  
  35. // x values
  36. x_initial_position = 160,  
  37. x_current_position = 0,
  38.  
  39. x_velocity = cos(radians) * velocity,
  40.  
  41. // box dimensions
  42. top = 115,
  43. left = 160,
  44. right = 750,
  45. bottom = 386,
  46.  
  47. // ball physical features
  48. mass = 1,
  49. friction = 0.8;
  50.  
  51. private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  52.  
  53.  if(timer1->Enabled == false){
  54.      timer1->Enabled = true;
  55.      button1->Text = L"Stop!";
  56.  }
  57.  else{
  58.      timer1->Enabled = false;
  59.      button1->Text = L"Start!";
  60.  }
  61. }
  62.  
  63.  
  64. private: System::Void myTimer_Tick(System::Object^  sender, System::EventArgs^  e) {
  65.  
  66. double    x = y_max,
  67. reset_x_time = 0,
  68. reset_y_time = 0;    
  69.  
  70. time = i/10;
  71. x_time = x_i/10;
  72. y_time = y_i/10;
  73.  
  74. x_current_position = ((x_initial_position) + (x_velocity * x_current_direction * x_time));
  75. y_current_position =  (2 * (y_initial_position)) + (direction_adjust * (y_initial_position + (y_velocity * y_time) + (0.5 * gravity * y_time * y_time)));
  76.  
  77. if(y_current_position > bottom){
  78.     y_current_position = bottom;
  79.     reset_y_time = (2 * ((0 - y_velocity)/gravity)) - y_time;
  80.     y_time = abs(reset_y_time);
  81.     y_i = abs(reset_y_time * 10);
  82. // coefficient of restitution!
  83.     y_velocity *= 0.7;
  84. // friction coefficient!!
  85.     x_velocity *= friction;
  86.     x_initial_position = x_current_position;
  87.     x_time = x_i = reset_x_time;
  88. }
  89.  
  90. if(x_current_position > right){
  91.     x_initial_position = x_current_position = right;
  92.     x_current_direction *= -1;
  93.     x_time = x_i = reset_x_time;
  94. }
  95.  
  96. if(x_current_position < left){
  97.     x_initial_position = x_current_position = left;
  98.     x_current_direction *= -1;
  99.     x_time = x_i = reset_x_time;
  100. }
  101.  
  102. pictureBox1->Location = System::Drawing::Point((int)x_current_position, (int)y_current_position);
  103.  
  104. ///////////////////////////////////////////////////
  105. /* THIS CODE SEEMS TO BE MISSED?*/
  106.  
  107. if(x_initial_velocity < 5){
  108.    return;
  109. }
  110.  
  111. ///////////////////////////////////////////////////
  112.  
  113. x_i++;
  114. y_i++;
  115.  
  116. }

Your condition is checking if x_initiial_velocity is less than 5, but I don't see that variable ever declared or assigned a value in the above code.

Should it be x_velocity?
Oct 12 '06 #5
sanou
19
yea it was a typo

i changed it to x_initial_velocity but it still doesn't work. i'm really confused
Oct 12 '06 #6
sanou
19
well this is weird, i tried moving the IF statement around. I nested it into another if statement looking like this,

Expand|Select|Wrap|Line Numbers
  1. if(y_current_position > bottom){
  2.    y_current_position = bottom;
  3.    reset_y_time = (2 * ((0 - y_initial_velocity)/gravity)) - y_time;
  4.    y_time = abs(reset_y_time);
  5.    y_i = abs(reset_y_time * 10);
  6.    y_initial_velocity *= 0.7;
  7.  
  8.       if(x_initial_velocity < 1){
  9.          return;
  10.       }
  11.  
  12.    x_initial_velocity *= friction;
  13.    x_initial_position = x_current_position;
  14.    x_time = x_i = reset_x_time;
  15. }
now everything works fine. i'm still confused tho.
Oct 12 '06 #7
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. y_current_position =  (2 * (y_initial_position)) + (direction_adjust * (y_initial_position + (y_velocity * y_time) + (0.5 * gravity * y_time * y_time)));
direction_adjust is not defined, if I change it to y_current_direction code seems to hit the if statement OK.
Oct 12 '06 #8
sanou
19
my apologies, i originally posted some outdated code

i double checked the direction_adjust variable and it does not appear to play a role in the mistake

i think it maybe more of a problem with the windows form timer. like somehow the function that activates the timer loop is not properly synchronized with the rest of the program.

as you know, i'm using this program to simulate some simple physics. my current timer tick is set to 1 (approx 1 tick every millisecond). i've allowed for users to reset the ball location, along with the velocity and angle of trajectory. however, simply reseting the variables while the program is running does not seem to work. i have to stop the timer, replace the old variables with the new ones, then restart the timer. i find it hard to believe that a program will simply "miss" an IF statement, so i'm wondering if the reason behind it is improper synchronization of some sort. i'm not too familiar with the windows timer event so i dunno...
Oct 12 '06 #9
Banfa
9,065 Expert Mod 8TB
No this should not really be a probem, a Windows GUI runs in a single thread (unless you have started another 1) so you can only be handling 1 message at a time.

I have to say I had no trouble with your code when I ran it as a console program.

I doubt Windows will ever be able to supply you with a tick every millisecond, I think about every 50 milliseconds or so is what it can manage (an old DOS limitation). It does have timers that go faster (in theory) but not message driven timers). Try slowing down the timer, if the program is single threaded the speed of the timer should have no effect of the calculation.

I refuse to believe that it is missing the if statement, if it can do that then there is a lot of code in a lot of trouble all over the world, try adding some debug, write a line to a file each time through the calculation to output all the different variables and what they are doing.
Oct 12 '06 #10

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

Similar topics

3
by: Utada P.W. SIU | last post by:
Dear All, I have writing a ASP that export an Excel file by using response.contentType = "application/vnd.ms-excel" I can export an Excel However, the grey color gridline is missed, how can I...
2
by: Nik | last post by:
Hi, I'm trying (and failing) to learn MC++ and I get the feeling that when I ask this question, the proverbial record will scratch to a halt and everyone will stare at me as though I'm totally...
0
by: ABC | last post by:
Why the properties of web user controls which inherted from my custom base UI controls MISSED? How should I to set enable?
0
by: ABC | last post by:
Why the properties of web user controls which inherted from my custom base UI controls MISSED? How should I to set enable?
1
by: Tim Haynes | last post by:
Hello all, I'm trying to do something which I believe is very normal, standard sort of requirement. I want to secure access to a web service across the Internet using username/password on the...
3
by: shaun roe | last post by:
mild rant follows Working now for a couple of years with xslt and now xslt 2.0, does anyone else get the impression that xslt 2.0 somehow missed the point? Yes its got a fancy new data model...
2
tolkienarda
by: tolkienarda | last post by:
hi all i've been beating my brains out on my moniter because i cant figure out what is wrong with my code. what i am doing is checking input from a form to make sure it is accurate and if it isn't...
1
by: gerry | last post by:
I started looking into the ObjectDataSource and I really like it. It does seem to me though that they really missed the mark in one respect - The designers went through the trouble to allow the...
2
by: david | last post by:
I wrote one function which is used to count comment and code lines and it works as it should be, but when compiling I get warning (i686-apple- darwin9-g++-4.0.1): pirma.cpp: In function ‘int...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.