Home All Groups Group Topic Archive Search About

Re: Arrays. Beginner's question!

Author
28 Nov 2007 6:52 PM
Suzy
Show quote
"Suzy" <not@valid> wrote in message news:474c68ed$1@dnews.tpgi.com.au...
>
> "Steve Gerrard" <mynameh***@comcast.net> wrote in message
> news:7PqdnXu93P6JXdbanZ2dnUVZ_hmtnZ2d@comcast.com...
>>
>> "Suzy" <not@valid> wrote in message news:474bb9bc@dnews.tpgi.com.au...
>>>
>>> Thanks Mike. I was going to post that when using a variant I am getting
>>> slightly different results to when I am using Double and was wondering
>>> why.
>>>
>>> The old code was
>>>
>>> Dim K1(7) as double
>>> ...
>>> K1(1)=0.4711
>>> K1(2)=0.462
>>> K1(3)=0.4538
>>> etc...
>>>
>>> which I changed (on advice) to
>>>
>>> Dim K1 As Variant
>>> ...
>>>    K1 = Array(0, 0.4711, 0.462, 0.4538, 0.4491, 0.4421, 0.4385, 0.4268)
>>>
>>> Calculations using this data came out slightly differently using the two
>>> methods. Any suggestions?
>>
>> Can you show an example of a calculation that comes out different?
>>
>> In your new version, you have a variant, which is an array of variants,
>> where each element is variant type double, which is not quite the same as
>> being a double.
>>
>> For a really clean result, I would take Bob's suggestion, and do
>>
>> Dim KTemp As Variant
>> Dim K1(7) As Double
>> Dim N As Long
>>
>> KTemp = Array(0, 0.4711, 0.462, 0.4538, 0.4491, 0.4421, 0.4385, 0.4268)
>>
>> For N = 0 To 7
>>    K1(N) = KTemp(N)
>> Next N
>>
>> Then you have a simple array of doubles, no doubts about data type.
>>
>>
> Well, in fact I did just that and found the anomaly still remained. I went
> back to the variant method and the results were exactly the same!  So I
> had a thirtieth check of all the array elements and... of course there was
> a typo!  .4358 instead of .4385 or somesuch. I put that right and now the
> anomaly has gone. Silly me.
>
> The code I am using is now:
>
> Dim K As Variant  'arrays
> Dim K1 As Variant
> Dim K2 As Variant
> Dim K3 As Variant
> Dim K4 As Variant
> ...
>    K = Array(0, 0.001, 0.003, 0.005, 0.007, 0.01, 0.015, 0.02)
>    K1 = Array(0, 0.4711, 0.462, 0.4538, 0.4491, 0.4421, 0.4358, 0.4268)
>    K2 = Array(0, 0.018, 0.01941, 0.02117, 0.02274, 0.02396, 0.02558,
> 0.02614)
>    K3 = Array(0, 0.08398, 0.08543, 0.0951, 0.08801, 0.1027, 0.1149,
> 0.1112)
>    K4 = Array(0, 0.965, 0.9697, 1.007, 0.9044, 1.038, 1.034, 1.036)
>
> and it all works fine. Could Mike tell me why (if it is not) the use of a
> variant is not advisable in this particular case?
>>
Do I take it from the deafening silence that this method is OK in this case?

Author
28 Nov 2007 7:40 PM
dpb
Suzy wrote:
Show quote
> "Suzy" <not@valid> wrote in message news:474c68ed$1@dnews.tpgi.com.au...
>> "Steve Gerrard" <mynameh***@comcast.net> wrote in message
>> news:7PqdnXu93P6JXdbanZ2dnUVZ_hmtnZ2d@comcast.com...
>>> "Suzy" <not@valid> wrote in message news:474bb9bc@dnews.tpgi.com.au...
>>>> Thanks Mike. I was going to post that when using a variant I am getting
>>>> slightly different results to when I am using Double and was wondering
>>>> why.
>>>>
>>>> The old code was
>>>>
>>>> Dim K1(7) as double
>>>> ...
>>>> K1(1)=0.4711
>>>> K1(2)=0.462
>>>> K1(3)=0.4538
>>>> etc...
>>>>
>>>> which I changed (on advice) to
>>>>
>>>> Dim K1 As Variant
>>>> ...
>>>>    K1 = Array(0, 0.4711, 0.462, 0.4538, 0.4491, 0.4421, 0.4385, 0.4268)
>>>>
>>>> Calculations using this data came out slightly differently using the two
>>>> methods. Any suggestions?
>>> Can you show an example of a calculation that comes out different?
>>>
>>> In your new version, you have a variant, which is an array of variants,
>>> where each element is variant type double, which is not quite the same as
>>> being a double.
>>>
>>> For a really clean result, I would take Bob's suggestion, and do
>>>
>>> Dim KTemp As Variant
>>> Dim K1(7) As Double
>>> Dim N As Long
>>>
>>> KTemp = Array(0, 0.4711, 0.462, 0.4538, 0.4491, 0.4421, 0.4385, 0.4268)
>>>
>>> For N = 0 To 7
>>>    K1(N) = KTemp(N)
>>> Next N
>>>
>>> Then you have a simple array of doubles, no doubts about data type.
>>>
>>>
>> Well, in fact I did just that and found the anomaly still remained. I went
>> back to the variant method and the results were exactly the same!  So I
>> had a thirtieth check of all the array elements and... of course there was
>> a typo!  .4358 instead of .4385 or somesuch. I put that right and now the
>> anomaly has gone. Silly me.
>>
>> The code I am using is now:
>>
>> Dim K As Variant  'arrays
>> Dim K1 As Variant
>> Dim K2 As Variant
>> Dim K3 As Variant
>> Dim K4 As Variant
>> ...
>>    K = Array(0, 0.001, 0.003, 0.005, 0.007, 0.01, 0.015, 0.02)
>>    K1 = Array(0, 0.4711, 0.462, 0.4538, 0.4491, 0.4421, 0.4358, 0.4268)
>>    K2 = Array(0, 0.018, 0.01941, 0.02117, 0.02274, 0.02396, 0.02558,
>> 0.02614)
>>    K3 = Array(0, 0.08398, 0.08543, 0.0951, 0.08801, 0.1027, 0.1149,
>> 0.1112)
>>    K4 = Array(0, 0.965, 0.9697, 1.007, 0.9044, 1.038, 1.034, 1.036)
>>
>> and it all works fine. Could Mike tell me why (if it is not) the use of a
>> variant is not advisable in this particular case?
> Do I take it from the deafening silence that this method is OK in this case?

For some definition of "fine" undoubtedly... :)

What's the real question, again? What's the point of having multiple Kx
arrays rather than a 2D single array?  There would superficially seem
not advantage to Variant arrays for what appear to be "real" numeric
values that wouldn't seem likely to need to change their spots in the
application, thus negating any reason for Variant other than the
convenience of the Array() notation for initializing them.  If that's
the issue, I personally would be highly likely to use one of the other
workarounds to the lack of Data in VB (although I'll never say never :) ).

As an unrelated aside--If VBFred reinstituted Data or an equivalent
construct, maybe there's one spot I could grant Michael C an advantage
to dotnet... <vbg>

--
Author
28 Nov 2007 7:43 PM
Bob Butler
"Suzy" <not@valid> wrote in message news:474db8ed@dnews.tpgi.com.au...
<cut>
>> and it all works fine. Could Mike tell me why (if it is not) the use of a
>> variant is not advisable in this particular case?
>>>
> Do I take it from the deafening silence that this method is OK in this
> case?


If it works and the performance is acceptable then go for it.  If you have
reason to believe that the scope will change such that performance may
degrade using this approach then you may want to re-visit it.
Author
28 Nov 2007 7:58 PM
Suzy
Show quote
"Bob Butler" <noway@nospam.ever> wrote in message
news:ewqracfMIHA.1188@TK2MSFTNGP04.phx.gbl...
> "Suzy" <not@valid> wrote in message news:474db8ed@dnews.tpgi.com.au...
> <cut>
>>> and it all works fine. Could Mike tell me why (if it is not) the use of
>>> a variant is not advisable in this particular case?
>>>>
>> Do I take it from the deafening silence that this method is OK in this
>> case?
>
>
> If it works and the performance is acceptable then go for it.  If you have
> reason to believe that the scope will change such that performance may
> degrade using this approach then you may want to re-visit it.
>
>Thanks Bob. It does work now that I have fixed my stupid typo.
Author
28 Nov 2007 8:33 PM
Mike Williams
"Suzy" <not@valid> wrote in message news:474dc878@dnews.tpgi.com.au...
>> If it works and the performance is acceptable then go for it.  If
>> you have reason to believe that the scope will change such that
>> performance may degrade using this approach then you may
>> want to re-visit it.
>>
>>Thanks Bob. It does work now that I have fixed my stupid typo.

Suzy, the reason you did not get a response initially to your earlier post
(where you asked, "Do I take it that this method is okay) is because most
people probably missed it. That's because you are making it very difficult
for people to see what you have posted. In your responses you quote an
extract from the post to which you are responding, thus:

> Are you using arrays in a Variant

.. . . and you are inserting your own reply beneath that extract, but you are
ALSO indenting your own reply, thus:

> Yes. That's what I am doing

.. . . so you end up with:

> Are you using arrays in a Variant
> Yes. That's what I am doing.

This is a very strange way of arranging your post, and we cannot tell where
the "extract from the original" ends and your own response begins. You
should NOT do that. Instead of the above you should arrange it as follows:

> Are you using arrays in a Variant

Yes. That's what I am doing.

Note that ONLY the extract from the original is indented. Your own response
is NOT indented. In that we we can immediately see what's what :-)

Mike
Author
28 Nov 2007 8:00 PM
Larry Serflaten
Show quote
"Suzy" <not@valid> wrote

> > The code I am using is now:
> >
> > Dim K As Variant  'arrays
> > Dim K1 As Variant
> > Dim K2 As Variant
> > Dim K3 As Variant
> > Dim K4 As Variant
> > ...
> >    K = Array(0, 0.001, 0.003, 0.005, 0.007, 0.01, 0.015, 0.02)
> >    K1 = Array(0, 0.4711, 0.462, 0.4538, 0.4491, 0.4421, 0.4358, 0.4268)
> >    K2 = Array(0, 0.018, 0.01941, 0.02117, 0.02274, 0.02396, 0.02558,
> > 0.02614)
> >    K3 = Array(0, 0.08398, 0.08543, 0.0951, 0.08801, 0.1027, 0.1149,
> > 0.1112)
> >    K4 = Array(0, 0.965, 0.9697, 1.007, 0.9044, 1.038, 1.034, 1.036)
> >
> > and it all works fine. Could Mike tell me why (if it is not) the use of a
> > variant is not advisable in this particular case?
> >>

> Do I take it from the deafening silence that this method is OK in this case?


Oddly enough, I do not see the post you are replying to.  Perhaps others
did not see it as well?

About your use of Variants, I would wonder why you would not prefer
to use an array of the appropreate data type.  There are several ways you
could make the assignment to your array(s) and still have arrays that are
typed as needed.  For example, a sub similar to:

Sub ReadData(ByRef Group() As Double, ParamArray List())
Dim index As Long
   ReDim Group(0 To UBound(List))
   For index = 0 To UBound(List)
     Group(index) = List(index)
   Next
End Sub

.... would allow you to build arrays of Doubles in much the same manner
as you are using now:

Dim K() As Double
Dim K1() As Double
' ...
Dim K4() As Double

  ReadData K, 0, 0.001, 0.003, 0.005, 0.007, 0.01, 0.015, 0.02
  ReadData K1, 0, 0.4711, 0.462, 0.4538, 0.4491, 0.4421, 0.4358, 0.4268
  '...
  ReadData K4, 0, 0.965, 0.9697, 1.007, 0.9044, 1.038, 1.034, 1.036

If you put your values in a typed array, VB can optimize your efforts
knowing you want to work on Double values.  If you leave them as
Variants, VB really can't make any assumtions about your data.

For a contrived example where using a properly typed array makes a
difference, consider what the output would be in the following case:

  Dim A1, A2() As Double

  ' Assignment of the same data, using the different methods
  A1 = Array("2", "3")
  ReadData A2, "2", "3"

  ' Identical output???
  Debug.Print A1(0) + A1(1)
  Debug.Print A2(0) + A2(1)

Try it and see the difference....

LFS
Author
28 Nov 2007 8:02 PM
Mike Williams
"Suzy" <not@valid> wrote in message news:474db8ed@dnews.tpgi.com.au...

> K1 = Array(0, 0.4711, 0.462, 0.4538, 0.4491, 0.4421, 0.4358, 0.4268)
> K2 = Array(0, 0.018, 0.01941, 0.02117, 0.02274, 0.02396, 0.02558, Do I
> take it from the deafening silence that this method is OK in this case?

If that's what you want to use then it's okay. Personally I would much
rather use a standard array of Doubles and I only ever use Variants if there
is simply no other option, partly because they occupy a bit more memory and
partly because they are quite a lot slower. But if you're not using
thousands of them and if you're not performing tens of thousands of
calculations on them then you'll be okay. In your specific case, where it
looks as though you are using a limited number of Variants for some fairly
simple calculations then you probably won't notice the difference, but for
some applications they will slow down your code considerably. As an example,
I've just done the following:

For p = 1 To 100
  For n = 0 To 7
    k1(n) = k1(n) + k2(n)
  Next n
Next p

If the arrays k1() and k2() are Variants, as in your own code, it takes
about 200 microseconds to execute the above loop in a standard native code
optimised compiled exe. However, if the arrays k1() and k2() are standard
arrays of Doubles the same loop takes only about 3 microseconds to execute
(yes, that's 3). I think you'll agree that's quite a considerable
difference.

Mike
Author
28 Nov 2007 8:18 PM
Ralph
Show quote
"Suzy" <not@valid> wrote in message news:474db8ed@dnews.tpgi.com.au...
>
> > <snipped>
> >
> > The code I am using is now:
> >
> > Dim K As Variant  'arrays
> > Dim K1 As Variant
> > Dim K2 As Variant
> > Dim K3 As Variant
> > Dim K4 As Variant
> > ...
> >    K = Array(0, 0.001, 0.003, 0.005, 0.007, 0.01, 0.015, 0.02)
> >    K1 = Array(0, 0.4711, 0.462, 0.4538, 0.4491, 0.4421, 0.4358, 0.4268)
> >    K2 = Array(0, 0.018, 0.01941, 0.02117, 0.02274, 0.02396, 0.02558,
> > 0.02614)
> >    K3 = Array(0, 0.08398, 0.08543, 0.0951, 0.08801, 0.1027, 0.1149,
> > 0.1112)
> >    K4 = Array(0, 0.965, 0.9697, 1.007, 0.9044, 1.038, 1.034, 1.036)
> >
> > and it all works fine. Could Mike tell me why (if it is not) the use of
a
> > variant is not advisable in this particular case?
> >>
> Do I take it from the deafening silence that this method is OK in this
case?
>

I don't know about them, but it is certainly OK as far as I am concerned.

No reason to develop a rabid fear of Variants. Just don't over-do it. They
are part of the culture. Variants show up all over the place so you are
likely using them even if you don't think you are. Recordsets for example
are nothing but Collections of Collections containing Variants, OLE
datatypes are Variants, even properties of common controls, object
libraries, and VBA library functions are often thinly veiled Variants.

I don't believe in over-thinking pre-optimizing. So the only concern in this
case is how often are these arrays used? Are you abusing them to the point
where you will get some measurable value out of working them to a native
type or not?

It looks like you are trying to get around the lack of a Constant Array in
VB. But didn't we just have this same discussion a while ago? (Data...
Read...)

One small thing. In your example above, Kx(0) is a vbInteger, the rest are
Doubles.

-ralph

AddThis Social Bookmark Button