-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
141 lines (116 loc) · 4.73 KB
/
index.html
File metadata and controls
141 lines (116 loc) · 4.73 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Ulp TPS Calculator</title>
<style>
body { font-family: system-ui, sans-serif; padding: 1rem; line-height: 1.5; }
label { display:block; margin: 0.5rem 0; }
input { width: 10rem; }
pre { background:#f6f8fa; padding: 1rem; border-radius: 6px; }
</style>
</head>
<body>
<h1>Floating Point Freeze Calculator by jojo and chizz 😛</h1>
<label>Distance (in units/s): <input id="distance" value="100" /></label>
<label>Velocity: <input id="velocity" value="10" /></label>
<label>TPS: <input id="tps" value="" /></label>
<button id="calc">Calculate</button>
<pre id="out"></pre>
<script>
// Small helper values for float32
const FLOAT32_MIN_POSITIVE = 1.401298464324817e-45; // smallest positive float32 subnormal
function ulp32(distance) {
const f = Math.fround(distance);
if (!Number.isFinite(f)) return NaN;
if (f <= 0) return FLOAT32_MIN_POSITIVE;
const buf = new ArrayBuffer(4);
const dv = new DataView(buf);
dv.setFloat32(0, f, true); // little-endian to match BitConverter.GetBytes
let bits = dv.getInt32(0, true); // interpret bits as signed int32 like the C# code
let nextBits = bits + 1;
dv.setInt32(0, nextBits, true);
const next = dv.getFloat32(0, true);
return next - f;
}
function formatNum(n) {
if (!Number.isFinite(n)) return String(n);
return Number(n).toPrecision(17);
}
function updateInputsAndCalc() {
const dRaw = document.getElementById('distance').value.trim();
const vRaw = document.getElementById('velocity').value.trim();
const tRaw = document.getElementById('tps').value.trim();
const d = Number(dRaw);
const v = Number(vRaw);
const t = Number(tRaw);
const hasD = dRaw !== '' && Number.isFinite(d);
const hasV = vRaw !== '' && Number.isFinite(v);
const hasT = tRaw !== '' && Number.isFinite(t);
if (hasD && hasV && !hasT) {
const dFloat = Math.fround(d);
const vFloat = Math.fround(v);
const u = ulp32(dFloat);
if (!Number.isFinite(u) || u <= 0) {
document.getElementById('out').textContent = 'Invalid ULP computed for the given distance.';
return;
}
const min = (2.0 / u) * vFloat + FLOAT32_MIN_POSITIVE;
const max = (4.0 / u) * vFloat;
document.getElementById('tps').value = formatNum((min + max) / 2);
document.getElementById('out').textContent =
`distance (float32): ${dFloat}
ulp (float32): ${formatNum(u)}
velocity (float32): ${vFloat}
Minimum tps to freeze: ${formatNum(min)}
Maximum tps to freeze: ${formatNum(max)}`;
} else if (hasD && hasT && !hasV) {
const dFloat = Math.fround(d);
const u = ulp32(dFloat);
if (!Number.isFinite(u) || u <= 0) {
document.getElementById('out').textContent = 'Invalid ULP computed for the given distance.';
return;
}
const vFloat = (t - FLOAT32_MIN_POSITIVE) * u / 2.0;
document.getElementById('velocity').value = formatNum(vFloat);
document.getElementById('out').textContent =
`distance (float32): ${dFloat}
ulp (float32): ${formatNum(u)}
tps: ${t}
velocity (float32): ${formatNum(vFloat)}`;
} else if (hasV && hasT && !hasD) {
const vFloat = Math.fround(v);
const targetUlp = 2.0 * vFloat / (t - FLOAT32_MIN_POSITIVE);
let dFloat;
if (targetUlp <= FLOAT32_MIN_POSITIVE) {
dFloat = FLOAT32_MIN_POSITIVE;
} else {
dFloat = targetUlp * Math.pow(2, 23);
dFloat = Math.max(FLOAT32_MIN_POSITIVE, Math.min(dFloat, Number.MAX_VALUE));
dFloat = Math.fround(dFloat);
}
const u = ulp32(dFloat);
document.getElementById('distance').value = formatNum(dFloat);
document.getElementById('out').textContent =
`velocity (float32): ${vFloat}
tps: ${t}
distance (float32): ${formatNum(dFloat)}
ulp (float32): ${formatNum(u)}`;
} else {
document.getElementById('out').textContent = 'Please enter exactly two values.';
}
}
document.getElementById('calc').addEventListener('click', updateInputsAndCalc);
window.addEventListener('error', function (e) {
console.error('Unhandled error', e.error || e.message, e);
const out = document.getElementById('out');
if (out) out.textContent = 'An error occurred — check console for details.';
});
window.addEventListener('unhandledrejection', function (e) {
console.error('Unhandled promise rejection', e.reason);
const out = document.getElementById('out');
if (out) out.textContent = 'An error occurred — check console for details.';
});
</script>
</body>
</html>