-
Notifications
You must be signed in to change notification settings - Fork 0
/
fptest.js
87 lines (75 loc) · 1.71 KB
/
fptest.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function threshold(tValue) {
const k = 0x100_0000_0000_0000 // 2^56
if (tValue < 1.0) {
return Math.floor(k*tValue + 0.5)
}
return Math.floor(k/tValue + 0.5)
}
function samplingRate(tValue) {
if (tValue < 1.0) {
return Math.floor(1.0/tValue + 0.5)
}
return Math.floor(tValue + 0.5)
}
function samplingProbability(tValue) {
if (tValue < 1.0) {
return tValue
}
return 1.0 / tValue
}
function all(args) {
for (var i=0; i<args.length; i++) {
var arg = process.argv[i]
var tValue = parseFloat(arg)
if (isNaN(tValue)) {
console.log("bad value: %s", arg)
continue
}
console.log("tValue: %f", tValue)
console.log("threshold: %d", threshold(tValue))
console.log("samplingRate: %d", samplingRate(tValue))
console.log("samplingProbability: %f", samplingProbability(tValue))
console.log()
}
}
function thresh(arg) {
var tValue = parseFloat(arg)
if (isNaN(tValue)) {
return "bad value: " + arg
}
return ""+threshold(tValue)
}
function rate(arg) {
var tValue = parseFloat(arg)
if (isNaN(tValue)) {
return "bad value: " + arg
}
return ""+samplingRate(tValue)
}
function prob(arg) {
var tValue = parseFloat(arg)
if (isNaN(tValue)) {
return "bad value: " + arg
}
return samplingProbability(tValue).toFixed(14)
}
function main() {
var args = process.argv.slice(2)
if (args.length == 0) {
console.log("usage: node fptest.js command <tValue> ...")
return
}
if (args[0] == "all") {
all(args)
} else if (args[0] == "thresh") {
console.log(thresh(args[1]))
} else if (args[0] == "rate") {
console.log(rate(args[1]))
}
else if (args[0] == "prob") {
console.log(prob(args[1]))
} else {
console.log("bad command: %s", args[0])
}
}
main()