[lp_math_median]

Description

Link: [lp_math_median]
Author: Bil Corry
Category: Math
Version: 8.x
License: Public Domain
Posted: Dec. 03, 2005
Updated: Dec. 04, 2005
More by this author...
Returns the median average value given an array of values.

Note: Requires [lp_integer_odd], [lp_decimal_precisionSet], and [lp_math_mean]

Parameters

-numbers array, required Array of numbers to average.
-precision integer, optional Number of decimal places of the returned value. Defaults to zero.

Sample Usage

(lp_math_median: (:10,2,3,4,1), 2);
'<br>';
(lp_math_median: (:10,2,4,6,7,1), 2);
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[

define_tag:'lp_math_median',
	-description='Returns the median average value given an array of values.',
	-priority='replace',
	-required='numbers', -type='array',
	-optional='precision';

	// did we get a precision?
	if:!(local_defined:'precision');
		// nope, set it to zero
		local:'precision' = 0;
	/if;

	// convert array of items into array of lp_decimals
	local:'numbers_array' = array;

	iterate: #numbers, local:'number';
		#numbers_array->(insert: (decimal: #number));
	/iterate;

	// sort in order
	#numbers_array->sort;
	
	local:'midpoint' = integer:(math_ceil: (decimal: (decimal: #numbers_array->size) / (decimal: 2)));

	if: (lp_integer_odd: #numbers_array->size);
		return: (lp_decimal_precisionSet: (decimal: #numbers_array->(get: #midpoint)), (integer:#precision));		
	else; // even
		return: (lp_decimal_precisionSet: (lp_math_mean: (array:(decimal: #numbers_array->(get: #midpoint)),(decimal: #numbers_array->(get: #midpoint + 1)))) , (integer:#precision));
	/if;

/define_tag;

]

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular

Support tagSwap.net