Entropy: Difference between revisions

(→‎OCaml: add a more idiomatic variant)
Line 2,930:
Entropy of 1223334444 is 1.84643934 bits.
The result should be around 1.84644 bits.
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
DUP SIZE 2 LN → str len log2
≪ { 255 } 0 CON
1 len '''FOR''' j
str j DUP SUB
NUM DUP2 GET 1 + PUT
'''NEXT'''
0 1 255 '''FOR''' j
'''IF''' OVER j GET
'''THEN''' LAST len / DUP LN log2 / * + '''END'''
'''NEXT'''
NEG SWAP DROP
≫ ≫ 'NTROP' STO
|
''( "string" -- entropy )''
Initialize local variables
Initialize a vector with 255 counters
For each character in the string...
... increase the counter according to ASCII code
For each non-zero counter
calculate term
Change sign and forget the vector
|}
The following line of code deliver what is required:
"1223334444" NTROP
{{out}}
<pre>
1: 1.84643934467
</pre>
 
1,150

edits