Strange Div by Zero Error
Strange Div by Zero Error
Strange Div by Zero Error
Strange Div by Zero Error
Strange Div by Zero Error
Strange Div by Zero Error Strange Div by Zero Error Strange Div by Zero Error Strange Div by Zero Error Strange Div by Zero Error Strange Div by Zero Error Strange Div by Zero Error Strange Div by Zero Error
Strange Div by Zero Error Strange Div by Zero Error
Strange Div by Zero Error
Go Back  Xtreme Visual Basic Talk > > > Strange Div by Zero Error


Reply
 
Thread Tools Display Modes
  #1  
Old 07-22-2017, 01:19 AM
Mathimagics's Avatar
MathimagicsStrange Div by Zero Error Mathimagics is offline
Algorithms 'R' Us

Forum Leader
* Guru *
 
Join Date: Jun 2002
Location: Canberra
Posts: 4,156
Question Strange Div by Zero Error


I'm working on a Snooker game that uses a C dll to interface with the Chipmunk2d Physics engine.

The following piece of code simply retrieves the current ball positions from my interface DLL:

Code:
Declare Sub GetBallPosition Lib "SnookerDLL" (ByVal BN As Long, BX As Double, BY As Double)
Dim BallX(21) as Double
Dim BallY(21) as Double
   ...
   ...
Sub DrawBalls()
   Dim B&, X#, Y#
   For B = 0 To 21
      GetBallPosition B, X, Y
      BallX(B) = X
      BallY(B) = Y
      DrawBall B, X, Y
      Next
   End Sub
The function previously returned Long values but I want the real values which are Doubles, so I changed the DLL and the VB function accordingly.

It throws an error the very first time it fetches the ball position. It does this both in EXE and IDE. In the IDE I can actually type in the statement it is complaining about ie BallX(B) = X. It accepts that without error and from then on everything is ok.

How can an assignment that sets one double to another generate a "Div by Zero"?
__________________
Cogito, ergo codo
Reply With Quote
  #2  
Old 07-22-2017, 07:44 AM
OnErr0r's Avatar
OnErr0rStrange Div by Zero Error OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,773
Default

What does your C function definition look like?
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #3  
Old 07-22-2017, 08:26 AM
Mathimagics's Avatar
MathimagicsStrange Div by Zero Error Mathimagics is offline
Algorithms 'R' Us

Forum Leader
* Guru *
 
Join Date: Jun 2002
Location: Canberra
Posts: 4,156
Default

Code:
#define EXPORT __declspec(dllexport)
#define APISUB EXPORT void __stdcall

APISUB GetBallPosition(int B, double *BX, double *BY) {
   if (B < 0) return;
   if (B >= nBalls) return;
   cpBody *body = Ball[B].body;
   if (body) {
      cpVect BallPos = cpBodyGetPosition(body);
      *BX = BallPos.x;
      *BY = BallPos.y;
      }
   }
cpVect is a struct with 2 doubles. I used to convert them to int and return as Long and it worked fine.

It still does, just not on the very first call. It's really weird.
__________________
Cogito, ergo codo
Reply With Quote
  #4  
Old 07-22-2017, 09:58 AM
OnErr0r's Avatar
OnErr0rStrange Div by Zero Error OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,773
Default

On first glance, everything looks correct. Since only the first call is a problem, could it be something not initialized in the DLL?

I'd be tempted to put BallPos where it has global scope in the dll. You're passing a pointers to values that could possibly be destroyed before the return.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #5  
Old 07-22-2017, 12:33 PM
Mathimagics's Avatar
MathimagicsStrange Div by Zero Error Mathimagics is offline
Algorithms 'R' Us

Forum Leader
* Guru *
 
Join Date: Jun 2002
Location: Canberra
Posts: 4,156
Default

I don't think that's a problem. cpVect is just a simple POINT-like structure with doubles instead of ints. There is no indirection there.

The only DLL pointer is [i]body[i] which is guaranteed to be valid. Chipmunk is purely synchronous, no multi-threading. The 22 ball objects (Ball[b].body) are created once and persist until I destoy the space at DLL unloading. I don't remove potted balls, I just move them to a parking area outside the table.

So nothing external can happen while in that function.

Just about every other function in my interface DLL has double arguments, including ones that are almost identical in form to this one, ie. retrieve a cpVect by the same mechanism.

This is the first time that I have had to resort to an "On Error" statement (no offence ) since, gee whiz, since VB6 was the primary raison d'etre for this forum.
__________________
Cogito, ergo codo
Reply With Quote
  #6  
Old 07-22-2017, 03:18 PM
Mathimagics's Avatar
MathimagicsStrange Div by Zero Error Mathimagics is offline
Algorithms 'R' Us

Forum Leader
* Guru *
 
Join Date: Jun 2002
Location: Canberra
Posts: 4,156
Default

There must be some function in either my DLL or, more likely, the Chipmunk DLL, that leaves an unhandled floating point exception on the stack.

The result is that after I have initialised the game space, the very next time I reference ANY floating point op, no matter how trivial, like X = 0, this "deferred" exception is triggered at the VB level and has to be handled before normal processing can resume.

Perhaps its in the dynamic DLL load/test - I doubt the Chipmunk DLL expects to be loaded via LoadLibrary.
__________________
Cogito, ergo codo
Reply With Quote
Reply

Tags
double, error, dll, dim, ball, div, ide, getballposition, interface, function, ballxb, values, time, doubles, changed, fetches, throws, exe, assignment, generate, sets, accepts, real, type, complaining


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Strange Div by Zero Error
Strange Div by Zero Error
Strange Div by Zero Error Strange Div by Zero Error
Strange Div by Zero Error
Strange Div by Zero Error
Strange Div by Zero Error Strange Div by Zero Error Strange Div by Zero Error Strange Div by Zero Error Strange Div by Zero Error Strange Div by Zero Error Strange Div by Zero Error
Strange Div by Zero Error
Strange Div by Zero Error
 
Strange Div by Zero Error
Strange Div by Zero Error
 
-->