Averages/Arithmetic mean: Difference between revisions
Content added Content deleted
(Added Quackery.) |
|||
Line 2,828: | Line 2,828: | ||
A built-in solution is <tt>avg</tt>. An implementation of it could be: |
A built-in solution is <tt>avg</tt>. An implementation of it could be: |
||
<lang q>mean:{(sum x)%count x}</lang> |
<lang q>mean:{(sum x)%count x}</lang> |
||
=={{header|Quackery}}== |
|||
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>. |
|||
<lang Quackery> [ $ 'bigrat.qky' loadfile ] now! |
|||
[ [] swap times |
|||
[ 20001 random 10000 - |
|||
n->v 100 n->v v/ |
|||
join nested join ] ] is makevector ( --> [ ) |
|||
[ witheach |
|||
[ unpack |
|||
2 point$ echo$ |
|||
i 0 > if |
|||
[ say ", " ] ] ] is echodecs ( [ --> ) |
|||
[ dup size n->v rot |
|||
0 n->v rot |
|||
witheach |
|||
[ unpack v+ ] |
|||
2swap v/ ] is arithmean ( [ --> n/d ) |
|||
[ 5 makevector |
|||
say "Internal representation of a randomly generated vector" cr |
|||
say "of five rational numbers. They are distributed between" cr |
|||
say "-100.00 and +100.00 and are multiples of 0.01." |
|||
cr cr dup echo cr cr |
|||
say "Shown as decimal fractions." |
|||
cr cr dup echodecs cr cr |
|||
arithmean |
|||
say "Arithmetic mean of vector as a decimal fraction to" cr |
|||
say "5 places after the point, as a rounded proper" cr |
|||
say "fraction with the denominator not exceeding 10, and" cr |
|||
say "finally as a vulgar fraction without rounding." cr cr |
|||
2dup 5 point$ echo$ |
|||
say ", " |
|||
2dup proper 10 round improper |
|||
proper$ echo$ |
|||
say ", " |
|||
vulgar$ echo$ cr cr |
|||
say "The same, but with a vector of 9973 rational numbers," cr |
|||
say "20 decimal places and a denominator not exceeding 100." cr cr |
|||
9973 makevector arithmean |
|||
2dup 20 point$ echo$ |
|||
say ", " |
|||
2dup proper 100 round improper |
|||
proper$ echo$ |
|||
say ", " |
|||
vulgar$ echo$ cr ] is demonstrate ( --> )</lang> |
|||
{{out}} |
|||
<pre>Internal representation of a randomly generated vector |
|||
of five rational numbers. They are distributed between |
|||
-100.00 and +100.00 and are multiples of 0.01. |
|||
[ [ -1999 100 ] [ 253 50 ] [ 2867 50 ] [ 3929 50 ] [ -25 2 ] ] |
|||
Shown as decimal fractions. |
|||
-19.99, 5.06, 57.34, 78.58, -12.5 |
|||
Arithmetic mean of vector as a decimal fraction to |
|||
5 places after the point, as a rounded proper |
|||
fraction with the denominator not exceeding 10, and |
|||
finally as a vulgar fraction without rounding. |
|||
21.698, 21 7/10, 10849/500 |
|||
The same, but with a vector of 9973 rational numbers, |
|||
20 decimal places and a denominator not exceeding 100. |
|||
-0.41664995487817106187, -0 5/12, -16621/39892</pre> |
|||
=={{header|R}}== |
=={{header|R}}== |