User Tools

Site Tools


finding_20the_20modulus_20of_20part_20of_20an_20array

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

finding_20the_20modulus_20of_20part_20of_20an_20array [2018/03/31 13:19]
127.0.0.1 external edit
finding_20the_20modulus_20of_20part_20of_20an_20array [2018/04/17 16:13] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Richard Russell, October 2013//\\ \\  The [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin6.html#​mod|MOD()]] function, when applied to an array, returns the square-root of the sum of the squares of the elements of the array (the so-called //​modulus//​). It may occasionally be useful to discover the modulus of only **part** of an array, but BBC BASIC doesn'​t have a built-in function to do that. \\ \\  Of course it's straightforward to find the modulus using a loop:​\\ ​ //by Richard Russell, October 2013//\\ \\  The [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin6.html#​mod|MOD()]] function, when applied to an array, returns the square-root of the sum of the squares of the elements of the array (the so-called //​modulus//​). It may occasionally be useful to discover the modulus of only **part** of an array, but BBC BASIC doesn'​t have a built-in function to do that. \\ \\  Of course it's straightforward to find the modulus using a loop:​\\ ​
 +<code bb4w>
         totalsq = 0         totalsq = 0
         FOR index% = first% TO last%         FOR index% = first% TO last%
Line 7: Line 8:
         NEXT         NEXT
         Modulus = SQR(totalsq)         Modulus = SQR(totalsq)
 +</​code>​
 where **first%** and **last%** define the range of indices (inclusive) over which the modulus should be calculated.\\ \\  However this approach is relatively slow, especially if the number of elements included is large, and it would be desirable to have a faster method. Fortunately there is a way, although the code isn't very nice:​\\ ​ where **first%** and **last%** define the range of indices (inclusive) over which the modulus should be calculated.\\ \\  However this approach is relatively slow, especially if the number of elements included is large, and it would be desirable to have a faster method. Fortunately there is a way, although the code isn't very nice:​\\ ​
 +<code bb4w>
         DEF FN_ModPartialArray(d(),​ first%, last%)         DEF FN_ModPartialArray(d(),​ first%, last%)
         LOCAL I%         LOCAL I%
Line 15: Line 18:
         !^a() = I%         !^a() = I%
         = MOD(a())         = MOD(a())
 +</​code>​
 The array **a()** must be the same type (i.e. have the same suffix character, if any) as the array whose modulus is being calculated.\\ \\  Note that during execution of the function the array contents are temporarily changed, so don't use this method if your program has a timer interrupt in which the array is accessed.\\ \\  If errors are trapped (using ON ERROR) and you need your program to resume execution after an error (e.g. pressing the **Esc**ape key), add a statement to ensure the array contents are restored:​\\ ​ The array **a()** must be the same type (i.e. have the same suffix character, if any) as the array whose modulus is being calculated.\\ \\  Note that during execution of the function the array contents are temporarily changed, so don't use this method if your program has a timer interrupt in which the array is accessed.\\ \\  If errors are trapped (using ON ERROR) and you need your program to resume execution after an error (e.g. pressing the **Esc**ape key), add a statement to ensure the array contents are restored:​\\ ​
 +<code bb4w>
         DEF FN_ModPartialArray(d(),​ first%, last%)         DEF FN_ModPartialArray(d(),​ first%, last%)
         LOCAL I%         LOCAL I%
Line 24: Line 29:
         !^a() = I%         !^a() = I%
         = MOD(a())         = MOD(a())
 +</​code>​
finding_20the_20modulus_20of_20part_20of_20an_20array.txt ยท Last modified: 2018/04/17 16:13 by tbest3112