Author |
Topic: Using SUM on part of an array (Read 957 times) |
|
Michael Hutton
Developer
member is offline


Gender: 
Posts: 248
|
 |
Using SUM on part of an array
« Thread started 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
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Using SUM on part of an array
« Reply #1 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.
|
|
Logged
|
|
|
|
Michael Hutton
Developer
member is offline


Gender: 
Posts: 248
|
 |
Re: Using SUM on part of an array
« Reply #2 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?
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Using SUM on part of an array
« Reply #3 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.
|
|
Logged
|
|
|
|
Michael Hutton
Developer
member is offline


Gender: 
Posts: 248
|
 |
Re: Using SUM on part of an array
« Reply #4 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
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Using SUM on part of an array
« Reply #5 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.
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Using SUM on part of an array
« Reply #6 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:
Don't make assumptions about the size of variant variables - in v6 they are 80-bits (10 bytes). If you need a 64-bit 'double', always use an explicit # suffix. Don't make assumptions about the size of a structure; if it contains one or more variant members or string members its length will be different. If calculating offsets into an array, be aware that the size of an element may be different (variants are 10 bytes, strings are 8 bytes). If writing assembler code be prepared for new type codes in the CALL parameter block, in particular: 10 = 80-bit variant, 40 = 64-bit signed integer, 136 = moveable string. Richard.
|
|
Logged
|
|
|
|
|