|
-
Jun 2nd, 2011, 12:30 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Why do i get this warning?
I have created a DLL in C++ that contains a function which a program made in VB6 will use.
But i get the Overflow error when running the app.
I know for sure it is the function in the DLL that causes this.
Here is the function:
Code:
void __stdcall DLL_EXPORT CalculateGravity(double *X1,double *Y1,double *X2,double *Y2,double *FX1,double *FY1,double *FX2,double *FY2,double *W1,double *W2,double MaxAttraction,double MaxMovement,boolean AffectOnlyOne)
{
double dx;
double dy;
double d1;
double d2;
double F1;
double F2;
double TempX;
double TempY;
double D;
if (AffectOnlyOne) //Only Particle 1 Moves
{
dx == *X1 - *X2;
dy == *Y1 - *Y2;
d1 == (dx * dx) + (dy * dy);
if (d1 == 0) {d1 = 0.00001;}
F1 == *W2 / (d1 * sqrt(d1));
if (F1 > MaxAttraction) {F1 == MaxAttraction;}
TempX == *FX1 - (F1 * dx);
TempY == *FY1 - (F1 * dy);
D == sqrt((abs(TempX) * abs(TempX)) + (abs(TempY) * abs(TempY))); //When i remove this line of code here, the Overflow error stops occuring.
if (D > MaxMovement) {
D == D / MaxMovement;
TempX == TempX / D;
TempY == TempY / D;
}
*FX1 = TempX;
*FY1 = TempY;
}
else //Both Particle 1 and Particle 2 Moves
{
//not done with this part yet
}
}
And it gets called from the VB6 app like this:
Code:
'Simulating the particles around the galaxy
For i = 1 To UBound(P) - 1
CalculateGravity P(i).X(CurrentFrame), P(i).Y(CurrentFrame), P(0).X(CurrentFrame), P(0).Y(CurrentFrame), P(i).fx, P(i).fy, P(0).fx, P(0).fy, P(i).W, P(0).W, MaxAttraction, MaxMovement, True
ReDim Preserve P(i).X(MaxProcessFrame + 1)
ReDim Preserve P(i).Y(MaxProcessFrame + 1)
P(i).X(CurrentFrame + 1) = P(i).X(CurrentFrame) + P(i).fx
P(i).Y(CurrentFrame + 1) = P(i).Y(CurrentFrame) + P(i).fy
Next
Basically what this function does is it calculates a bunch of stuff and then overwrites the result on two variables (FX1 and FY1). Those two variables is stored on the VB6 app that calls the function.
Also when i compile the DLL i get alot of warnings saying "Statement has no effect" on almost every line of code.
I'm new to C++, so excuse me if i'm missing something obvious here.
If anyone knows why i get those warnings and the Overflow error please let me know. Thanks.
Last edited by cc2^^; Jun 17th, 2011 at 09:23 AM.
I think I am, therefore, I am. I think.
-
Jun 2nd, 2011, 01:00 PM
#2
Junior Member
Re: Why do i get this warning?
The overflow error is probably due to the fact that it "calculates a bunch of stuff and then overwrites the result on two variables".
You are probably having it do too much all at once.
Okay, I don't know why the "Statement has no effect" warning is occurring, but I'd hazard a guess that it is occurring because some of the statements don't do anything that has an effect.
Are the errors a result of the VB6 code, or the code in the DLL you wrote in C++?
This line, as you said, is the problem with the overflow:
Code:
D == sqrt((abs(TempX) * abs(TempX)) + (abs(TempY) * abs(TempY)));
You are having it calculate the absolute values for TempX, square those two, calculate the absolutes of TempY, square them, add those results together, and calculate the square root and assign them to D all at the same time.
Oh, you should also be using the assignment operator "=", not the equality operator "==". When you are comparing two things, you use "==", when you are assigning a value to something, you need to use "=". I may be mistaken here though.
The absolute value calculations are unnecessary as a whole, squaring a negative number gives a positive number. You were using absolutes to avoid imaginary numbers, no?
Here is what I would try:
Code:
TempX = (TempX * TempX); // TempX is squared
TempY = (TempY * TempY); // TempY is squared
D = sqrt(TempX + TempY); //D is the square root of the sum of the squares of TempX and TempY
Now, it isn't doing everything all at once, and has a few less things to calculate as a whole.
I hope that helps.
-
Jun 3rd, 2011, 01:28 AM
#3
Re: Why do i get this warning?
Are you sure you should be declaring the method parameters as pointers to double? When the overflow occurs, what values are you passing to it?
-
Jun 3rd, 2011, 03:06 AM
#4
Thread Starter
Addicted Member
Re: Why do i get this warning?
Oh, you should also be using the assignment operator "=", not the equality operator "==". When you are comparing two things, you use "==", when you are assigning a value to something, you need to use "=". I may be mistaken here though.
You are totally right! I was confused on when to use '==' and '='. But i got it all figured out now.
The '=' should be used when calculating: dx = *X1 - *X2;
And the '==' should be used when testing a variable: if (d1 == 0) {d1 = 0.00001;}
So i fixed the problem and the program works without errors 
Thanks all.
I think I am, therefore, I am. I think.
-
Jun 3rd, 2011, 04:20 AM
#5
Re: [RESOLVED] Why do i get this warning?
Hi cc2,
if your going to the trouble of writing a C++ dll to improve the speed of your VB6 application you should really have it working on a whole bunch of 'particles' rather than one at a time. There is a reasonable cost to every call which is likely to eat up any improvement in speed, especially when you are passing loads of parameters.
While on the subject, (abs(TempX) * abs(TempX)) + (abs(TempY) * abs(TempY))... what sign is the result of multiplying two negative numbers?
-
Jun 17th, 2011, 09:22 AM
#6
Thread Starter
Addicted Member
Re: [RESOLVED] Why do i get this warning?
 Originally Posted by Milk
Hi cc2,
if your going to the trouble of writing a C++ dll to improve the speed of your VB6 application you should really have it working on a whole bunch of 'particles' rather than one at a time. There is a reasonable cost to every call which is likely to eat up any improvement in speed, especially when you are passing loads of parameters.
While on the subject, (abs(TempX) * abs(TempX)) + (abs(TempY) * abs(TempY))... what sign is the result of multiplying two negative numbers?
I noticed the program ran even slower with the DLL, so it seems like i have to include the loop inside the DLL. But then i have to declare the particle in C++. But i don't know how to do that >.<
Code:
Private Type Particle
X() As Double 'Position
Y() As Double
fx As Double 'Movement
fy As Double
W As Double 'Weight
R As Byte 'Red
G As Byte 'Green
B As Byte 'Blue
End Type
Dim P() As Particle
That needs to be translated into C++ along with the loop code:
Code:
For i = 1 To UBound(P) - 1
CalculateGravity P(i).X(CurrentFrame), P(i).Y(CurrentFrame), P(0).X(CurrentFrame), P(0).Y(CurrentFrame), P(i).fx, P(i).fy, P(0).fx, P(0).fy, P(i).W, P(0).W, MaxAttraction, MaxMovement, True
ReDim Preserve P(i).X(MaxProcessFrame + 1)
ReDim Preserve P(i).Y(MaxProcessFrame + 1)
P(i).X(CurrentFrame + 1) = P(i).X(CurrentFrame) + P(i).fx
P(i).Y(CurrentFrame + 1) = P(i).Y(CurrentFrame) + P(i).fy
Next
I think i can try to translate the loop in c++ myself, but i am clueless on how to declare the private-type Particle. Does anyone know how to do that?
I think I am, therefore, I am. I think.
-
Jun 18th, 2011, 08:33 AM
#7
Re: Why do i get this warning?
Have a go at getting your VB code more optimized, it looks like your code spends most of its time reallocating and copying arrays. Can you make the following work in VB6? If you can it should improve your speed and give you something that will translate to C more easily.
Code:
Private Type Vector2Double
X As Double 'Position
Y As Double
End Type
Private Type VectorArray
Positions() As Vector2Double
End Type
Private Type Particle
Position As Vector2Double 'Position
Velocity As Vector2Double 'Movement
Weight As Double 'Weight
Argb As Long '&hAARRGGBB
End Type
Private P() As Particle
Private Frames() As VectorArray
Code:
For i = 1 To UBound(P) - 1 '<-- Why the -1?
Frames(i).Positions(CurrentFrame) = P(i).Position 'Add the position to a pre sized buffer to avoid redimming
CalculateGravity P(i), P(0), MaxAttraction, MaxMovement, True
Update P(i)
Next
Code:
Private Sub Update(ByRef p As Particle)
p.X = p.X + p.fx
p.Y = p.Y + p.fy
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|