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.
9 1782 Banfa 9,065
Expert Mod 8TB
Most likely something wrong with your algorithm or code.
Can you include some of the code from the project so that we can see where there might be an error in the algorithm?
- public: static double
-
-
// constants
-
degrees = 45,
-
radians = 0.0174532925 * degrees,
-
-
// general time counters, i=1 means 1 tick per millisecond; 1000 ticks = 1 sec
-
i = 0,
-
time = 0,
-
-
// unique time components for x and y
-
x_i = 0,
-
x_time = 0,
-
y_i = 0,
-
y_time = 0,
-
-
// direction vector
-
y_current_direction = -1,
-
x_current_direction = 1,
-
-
// gravity = 9.8 m/s^2
-
gravity = y_current_direction * 50 , //default gravity is 9.8
-
-
// velocity = some value with units m/s
-
velocity = 137,
-
-
// y values
-
y_initial_position = 386,
-
y_current_position = 0,
-
-
y_velocity = sin(radians) * velocity,
-
-
y_max = (0 - (y_velocity * y_velocity)) / (2 * gravity),
-
-
// x values
-
x_initial_position = 160,
-
x_current_position = 0,
-
-
x_velocity = cos(radians) * velocity,
-
-
// box dimensions
-
top = 115,
-
left = 160,
-
right = 750,
-
bottom = 386,
-
-
// ball physical features
-
mass = 1,
-
friction = 0.8;
-
-
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
-
-
if(timer1->Enabled == false){
-
timer1->Enabled = true;
-
button1->Text = L"Stop!";
-
}
-
else{
-
timer1->Enabled = false;
-
button1->Text = L"Start!";
-
}
-
}
-
-
-
private: System::Void myTimer_Tick(System::Object^ sender, System::EventArgs^ e) {
-
-
double x = y_max,
-
reset_x_time = 0,
-
reset_y_time = 0;
-
-
time = i/10;
-
x_time = x_i/10;
-
y_time = y_i/10;
-
-
x_current_position = ((x_initial_position) + (x_velocity * x_current_direction * x_time));
-
y_current_position = (2 * (y_initial_position)) + (direction_adjust * (y_initial_position + (y_velocity * y_time) + (0.5 * gravity * y_time * y_time)));
-
-
if(y_current_position > bottom){
-
y_current_position = bottom;
-
reset_y_time = (2 * ((0 - y_velocity)/gravity)) - y_time;
-
y_time = abs(reset_y_time);
-
y_i = abs(reset_y_time * 10);
-
// coefficient of restitution!
-
y_velocity *= 0.7;
-
// friction coefficient!!
-
x_velocity *= friction;
-
x_initial_position = x_current_position;
-
x_time = x_i = reset_x_time;
-
}
-
-
if(x_current_position > right){
-
x_initial_position = x_current_position = right;
-
x_current_direction *= -1;
-
x_time = x_i = reset_x_time;
-
}
-
-
if(x_current_position < left){
-
x_initial_position = x_current_position = left;
-
x_current_direction *= -1;
-
x_time = x_i = reset_x_time;
-
}
-
-
pictureBox1->Location = System::Drawing::Point((int)x_current_position, (int)y_current_position);
-
-
///////////////////////////////////////////////////
-
/* THIS CODE SEEMS TO BE MISSED?*/
-
-
if(x_initial_velocity < 5){
-
return;
-
}
-
-
///////////////////////////////////////////////////
-
-
x_i++;
-
y_i++;
-
-
}
- public: static double
-
-
// constants
-
degrees = 45,
-
radians = 0.0174532925 * degrees,
-
-
// general time counters, i=1 means 1 tick per millisecond; 1000 ticks = 1 sec
-
i = 0,
-
time = 0,
-
-
// unique time components for x and y
-
x_i = 0,
-
x_time = 0,
-
y_i = 0,
-
y_time = 0,
-
-
// direction vector
-
y_current_direction = -1,
-
x_current_direction = 1,
-
-
// gravity = 9.8 m/s^2
-
gravity = y_current_direction * 50 , //default gravity is 9.8
-
-
// velocity = some value with units m/s
-
velocity = 137,
-
-
// y values
-
y_initial_position = 386,
-
y_current_position = 0,
-
-
y_velocity = sin(radians) * velocity,
-
-
y_max = (0 - (y_velocity * y_velocity)) / (2 * gravity),
-
-
// x values
-
x_initial_position = 160,
-
x_current_position = 0,
-
-
x_velocity = cos(radians) * velocity,
-
-
// box dimensions
-
top = 115,
-
left = 160,
-
right = 750,
-
bottom = 386,
-
-
// ball physical features
-
mass = 1,
-
friction = 0.8;
-
-
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
-
-
if(timer1->Enabled == false){
-
timer1->Enabled = true;
-
button1->Text = L"Stop!";
-
}
-
else{
-
timer1->Enabled = false;
-
button1->Text = L"Start!";
-
}
-
}
-
-
-
private: System::Void myTimer_Tick(System::Object^ sender, System::EventArgs^ e) {
-
-
double x = y_max,
-
reset_x_time = 0,
-
reset_y_time = 0;
-
-
time = i/10;
-
x_time = x_i/10;
-
y_time = y_i/10;
-
-
x_current_position = ((x_initial_position) + (x_velocity * x_current_direction * x_time));
-
y_current_position = (2 * (y_initial_position)) + (direction_adjust * (y_initial_position + (y_velocity * y_time) + (0.5 * gravity * y_time * y_time)));
-
-
if(y_current_position > bottom){
-
y_current_position = bottom;
-
reset_y_time = (2 * ((0 - y_velocity)/gravity)) - y_time;
-
y_time = abs(reset_y_time);
-
y_i = abs(reset_y_time * 10);
-
// coefficient of restitution!
-
y_velocity *= 0.7;
-
// friction coefficient!!
-
x_velocity *= friction;
-
x_initial_position = x_current_position;
-
x_time = x_i = reset_x_time;
-
}
-
-
if(x_current_position > right){
-
x_initial_position = x_current_position = right;
-
x_current_direction *= -1;
-
x_time = x_i = reset_x_time;
-
}
-
-
if(x_current_position < left){
-
x_initial_position = x_current_position = left;
-
x_current_direction *= -1;
-
x_time = x_i = reset_x_time;
-
}
-
-
pictureBox1->Location = System::Drawing::Point((int)x_current_position, (int)y_current_position);
-
-
///////////////////////////////////////////////////
-
/* THIS CODE SEEMS TO BE MISSED?*/
-
-
if(x_initial_velocity < 5){
-
return;
-
}
-
-
///////////////////////////////////////////////////
-
-
x_i++;
-
y_i++;
-
-
}
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?
yea it was a typo
i changed it to x_initial_velocity but it still doesn't work. i'm really confused
well this is weird, i tried moving the IF statement around. I nested it into another if statement looking like this, - if(y_current_position > bottom){
-
y_current_position = bottom;
-
reset_y_time = (2 * ((0 - y_initial_velocity)/gravity)) - y_time;
-
y_time = abs(reset_y_time);
-
y_i = abs(reset_y_time * 10);
-
y_initial_velocity *= 0.7;
-
-
if(x_initial_velocity < 1){
-
return;
-
}
-
-
x_initial_velocity *= friction;
-
x_initial_position = x_current_position;
-
x_time = x_i = reset_x_time;
-
}
now everything works fine. i'm still confused tho.
Banfa 9,065
Expert Mod 8TB - 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.
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...
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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?
|
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?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |