BBC BASIC for Windows
Programming >> BBC BASIC language >> Using SUM on part of an array
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1382618242

Using SUM on part of an array
Post by Michael Hutton on Oct 24th, 2013, 12:37pm

I think I'm being thick.

I know I could write an asm routine for the following.

I want to sum just part of an array in a function and return the result. I want to use SUM().

The easy (slow way)
Code:
     DEF FN_GetChildrenSum(d(), start%, end%)
      LOCAL sum, I%
      FOR I% = start% TO end%
        sum += d(I%)
      NEXT
      = sum
 


Let's say I want to sum just part but use SUM().

Code:
DEF FN_GetChildrenSum(d(), start%, end%)
LOCAL sum, I%, a()
!^a() = ^d(start%)-1
.. how do I write the dimensions without buggering up the data? Can I fool the SUM command?
- am I just being dumb?
= SUM(a())
 


Michael
Re: Using SUM on part of an array
Post by admin on Oct 24th, 2013, 2:59pm

on Oct 24th, 2013, 12:37pm, Michael Hutton wrote:
Let's say I want to sum just part but use SUM().

That's not supported in BBC BASIC. Arguably, most (if not all) of the 'whole array' commands would be enhanced if it were possible to specify a sub-set of the array. It has been suggested before, and various proposals have been made for a syntax, for example: SUM(array()[, start, end])

I wouldn't be surprised if Basalt already provides an equivalent feature, but the likelihood of it ever being incorporated in BBC BASIC for Windows is vanishingly small.

Richard.
Re: Using SUM on part of an array
Post by Michael Hutton on Oct 24th, 2013, 9:51pm

I haven't heard of 'Basalt' before.

It is not too much of a problem, I will write a little asm routine to do it but was interested to try and see if there was a BASIC solution. I have just programmed an algorithm which relies very heavily on it, it takes something like 30% of the profiling time.

Michael

when is BB4W going to be made threadsafe?
Re: Using SUM on part of an array
Post by admin on Oct 24th, 2013, 10:44pm

on Oct 24th, 2013, 9:51pm, Michael Hutton wrote:
I haven't heard of 'Basalt' before.

Steve Drain's BASIC Alternative Keywords module, an extensive set of additions to the (ARM) BBC BASIC language.

Quote:
It is not too much of a problem, I will write a little asm routine to do it

Make sure it's compatible with BB4W v6!

Quote:
but was interested to try and see if there was a BASIC solution

You can bodge it, but it's not very nice:

Code:
      DEF FN_GetChildrenSum(d(), start%, end%)
      LOCAL I%
      I% = ^d(start%)-5
      LOCAL ?I%, I%!1, a()
      ?I% = 1 : I%!1 = end%-start%+1
      !^a() = I%
      = SUM(a()) 

Richard.
Re: Using SUM on part of an array
Post by Michael Hutton on Oct 28th, 2013, 4:55pm

Hardly a bodge! It is very quick indeed.

I notice you have already put a Wiki article in.

Quote:
Make sure it's compatible with BB4W v6!


I'll have to look into what has changed. Not very up to date this end I'm afraid.

Anyway, thanks for the quick response.

Michael



Re: Using SUM on part of an array
Post by admin on Oct 28th, 2013, 5:15pm

on Oct 28th, 2013, 4:55pm, Michael Hutton wrote:
Hardly a bodge! It is very quick indeed.

It's quick, but it's a bodge because it involves temporarily corrupting the contents of the array and then restoring them. Hence the warning about not using it in a program which accesses the array in an interrupt (which could therefore see the corrupted data if the interrupt happened at just the wrong time). That would make it entirely unsuited to going in a library, for example.

Richard.
Re: Using SUM on part of an array
Post by admin on Nov 1st, 2013, 11:16pm

on Oct 28th, 2013, 4:55pm, Michael Hutton wrote:
I'll have to look into what has changed. Not very up to date this end I'm afraid.

Here are the main things you need to watch out for to ensure any code you write is compatible with BB4W version 6:
Richard.