forked from EntropyString/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentropy.js
More file actions
47 lines (40 loc) · 913 Bytes
/
entropy.js
File metadata and controls
47 lines (40 loc) · 913 Bytes
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
36
37
38
39
40
41
42
43
44
45
46
47
import lcm from './lcm'
const _log2 = Math.log2
const _log10 = Math.log10
const _log2_10 = _log2(10)
const _bitsPerByte = 8
const _totalOf = (numStrings, log2Risk) => {
if (numStrings == 0) { return 0 }
let N
if (numStrings < 1000) {
N = _log2(numStrings) + _log2(numStrings-1)
}
else {
N = 2 * _log2(numStrings)
}
return N + log2Risk - 1
}
const bits = (total, risk) => {
if (total == 0) { return 0 }
return _totalOf(total, _log2(risk))
}
// CxTBD Mark as obsolete
const bitsWithRiskPower = (total, rPower) => {
let log2Risk = _log2_10 * rPower
return _totalOf(total, log2Risk)
}
// CxTBD Mark as obsolete
const bitsWithPowers = (tPower, rPower) => {
let N = 0
if (tPower < 4) {
return bitsWithRiskPower(Math.pow(10, tPower), rPower)
}
else {
return (2 * tPower + rPower) * _log2_10 - 1
}
}
export default {
bits,
bitsWithRiskPower,
bitsWithPowers
}