RosettaCodeData/Task/Entropy/Liberty-BASIC/entropy.basic

33 lines
1.0 KiB
Plaintext

dim countOfChar( 255) ' all possible one-byte ASCII chars
source$ ="1223334444"
charCount =len( source$)
usedChar$ =""
for i =1 to len( source$) ' count which chars are used in source
ch$ =mid$( source$, i, 1)
if not( instr( usedChar$, ch$)) then usedChar$ =usedChar$ +ch$
'currentCh$ =mid$(
j =instr( usedChar$, ch$)
countOfChar( j) =countOfChar( j) +1
next i
l =len( usedChar$)
for i =1 to l
probability =countOfChar( i) /charCount
entropy =entropy -( probability *logBase( probability, 2))
next i
print " Characters used and the number of occurrences of each "
for i =1 to l
print " '"; mid$( usedChar$, i, 1); "'", countOfChar( i)
next i
print " Entropy of '"; source$; "' is "; entropy; " bits."
print " The result should be around 1.84644 bits."
end
function logBase( x, b) ' in LB log() is base 'e'.
logBase =log( x) /log( 2)
end function