TypeCast double to integer for Array( Index )

Just starting out? Need help? Post your questions and find answers here.
TeraByte
User
User
Posts: 40
Joined: Wed May 09, 2012 12:40 am

TypeCast double to integer for Array( Index )

Post by TeraByte »

The following causes compiler to hiccup:

Code: Select all

Define d1.d
Dim MyArray.d(10)

d1 = 2.0
MyArray( d1 ) = 123.45
So I changed it to this which works:

Code: Select all

Define d1.d
Dim MyArray.d(10)

d1 = 2.0
MyArray( Int(d1) ) = 123.45
Question:
My snippet is just to demonstrate the issue where my index is being passed in by a 3rd party app as a double. Thus I need to typecast it to an integer for the array.

Is there a more efficient way to typecast from double to integer for the array index?
TeraByte
User
User
Posts: 40
Joined: Wed May 09, 2012 12:40 am

Re: TypeCast double to integer for Array( Index )

Post by TeraByte »

Is this more efficient?

Code: Select all

Define d1.d,  i1.i
Dim MyArray.d(10)

d1 = 2.0
i1 = d1
MyArray( i1 ) = 123.45
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: TypeCast double to integer for Array( Index )

Post by IdeasVacuum »

...the most efficient way would surely be to read the passed value as an int instead of as a double - how is the other app passing the value exactly?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
TeraByte
User
User
Posts: 40
Joined: Wed May 09, 2012 12:40 am

Re: TypeCast double to integer for Array( Index )

Post by TeraByte »

The 3rd party app is passing me an array of doubles. The array is multi purpose whereby sometimes the values in the array are to be used as doubles in math calculations, yet other times, as in my case, as an array index.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: TypeCast double to integer for Array( Index )

Post by IdeasVacuum »

Yes, but lets say your eyes are closed and nobody has told you what type of data you will receive - it could be bananas, it could be doubles.
- how is the other app passing the value exactly?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: TypeCast double to integer for Array( Index )

Post by skywalk »

How are you declaring your 3rd party dll?
Maybe use a prototype and call it differently depending on double array or integer array?

Code: Select all

Prototype[C].i your_dll_fn(param1.i, param2.i, etc.i, *nPoints.i, *IntOrDbl_array)
Global.i hdll = OpenLibrary(#PB_Any, "C:\Your_dll_path\your_dll.dll")
If hdll
  Global Your_dll_fn.Your_dll_fn = GetFunction(hdll, "Your_dll_fn")
Else
  MessageRequester("", "Error opening dll", #MB_ICONERROR)
EndIf
Define.i nPts
Dim arD.d(10)
Dim arI.i(10)
; Call dll with Double array
  your_dll_fn(0, 0, 0, @nPts, arD())
; Or Call it with Integer array
  your_dll_fn(0, 0, 0, @nPts, arI())

If hdll
  CloseLibrary(hdll)
EndIf
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply