-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfp-error-demo.html
More file actions
224 lines (191 loc) · 8.07 KB
/
fp-error-demo.html
File metadata and controls
224 lines (191 loc) · 8.07 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IEEE-754 Error Visualization | AGLE</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Space Grotesk', -apple-system, sans-serif;
background: linear-gradient(135deg, #0d1117 0%, #161b22 100%);
color: #c9d1d9;
padding: 2rem;
min-height: 100vh;
}
.container {
max-width: 900px;
margin: 0 auto;
}
h1 {
font-size: 2rem;
background: linear-gradient(135deg, #58a6ff, #79c0ff);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 1rem;
}
.theory {
background: #161b22;
border: 1px solid #30363d;
border-radius: 8px;
padding: 1.5rem;
margin-bottom: 2rem;
}
.formula {
font-family: 'Georgia', serif;
font-size: 1.2rem;
text-align: center;
margin: 1rem 0;
color: #79c0ff;
}
.demo {
background: #0d1117;
border: 2px solid #30363d;
border-radius: 8px;
padding: 2rem;
}
button {
background: linear-gradient(135deg, #1f6feb, #58a6ff);
color: white;
border: none;
padding: 0.75rem 2rem;
border-radius: 6px;
font-size: 1rem;
cursor: pointer;
transition: all 0.3s;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(88, 166, 255, 0.4);
}
#results {
margin-top: 2rem;
font-family: 'Courier New', monospace;
font-size: 0.9rem;
}
.error-value {
color: #f85149;
font-weight: bold;
}
.success {
color: #3fb950;
}
.histogram {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(40px, 1fr));
gap: 4px;
margin-top: 1rem;
}
.bar {
background: linear-gradient(180deg, #58a6ff, #1f6feb);
border-radius: 4px;
transition: height 0.3s;
}
.back-link {
display: inline-block;
margin-bottom: 1rem;
color: #58a6ff;
text-decoration: none;
}
.back-link:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<a href="index.html" class="back-link">← Voltar ao RNG</a>
<h1>🔬 Demonstração: Erro de Ponto Flutuante IEEE-754</h1>
<div class="theory">
<h2>Teoria</h2>
<p>O erro de ponto flutuante é a diferença entre um valor ideal (precisão infinita) e sua representação real em IEEE-754:</p>
<div class="formula">ε = x<sub>real</sub> - round<sub>IEEE</sub>(x)</div>
<p>Este erro é <strong>pequeno, mas real</strong> e <strong>não-determinístico</strong> no hardware. A AGLE amplifica esses erros via mapas caóticos para extrair entropia.</p>
</div>
<div class="demo">
<h3>Experimento Interativo</h3>
<p>Clique para gerar 10.000 erros de ponto flutuante e visualizar a distribuição:</p>
<button id="runDemo">▶ Executar Demonstração</button>
<div id="results"></div>
</div>
</div>
<script src="lib/agle_wasm.js"></script>
<script>
const runBtn = document.getElementById('runDemo');
const resultsDiv = document.getElementById('results');
runBtn.addEventListener('click', async () => {
resultsDiv.innerHTML = '<p class="success">⚙️ Inicializando WASM...</p>';
// Carregar WASM (se disponível)
let fpErrorFunc = null;
let wasmLoaded = false;
try {
console.log('[Demo] Calling AGLEWasm.init()...');
const wasmAdapter = await window.AGLEWasm.init();
if (wasmAdapter && wasmAdapter.agle_fp_error_raw) {
fpErrorFunc = wasmAdapter.agle_fp_error_raw;
wasmLoaded = true;
console.log('[Demo] WASM loaded successfully');
resultsDiv.innerHTML += '<p class="success">✓ WASM carregado! Usando agle_fp_error_raw()</p>';
} else {
console.warn('[Demo] Adapter returned but missing agle_fp_error_raw');
resultsDiv.innerHTML += '<p>⚠️ WASM não expõe agle_fp_error_raw. Usando simulação sensível.</p>';
}
} catch (e) {
console.error('[Demo] WASM load failed:', e);
resultsDiv.innerHTML += `<p>⚠️ WASM não disponível (${e.message}). Usando simulação sensível.</p>`;
}
// Gerar erros
resultsDiv.innerHTML += '<p>📊 Gerando 10.000 erros...</p>';
const errors = [];
const samples = 10000;
for (let i = 0; i < samples; i++) {
let error;
if (fpErrorFunc) {
// WASM: erro real IEEE-754 via C volatile operations
error = fpErrorFunc(Math.random());
} else {
// Fallback: Simulação sensível de erro (não pode usar volatile em JS)
// Usa operações que causam pequenas perturbações
const x = Math.random();
const y1 = x + 1e-16;
const y2 = x * (1 + 1e-15);
error = Math.abs((y1 - x) + (y2 - x)) / 2;
}
if (error > 0) errors.push(error);
}
// Estatísticas
const mean = errors.reduce((a, b) => a + b, 0) / samples;
const variance = errors.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / samples;
const stdDev = Math.sqrt(variance);
resultsDiv.innerHTML += `
<h4>Resultados</h4>
<p><strong>Amostras coletadas:</strong> ${errors.length.toLocaleString()} / ${samples}</p>
<p><strong>Modo:</strong> ${wasmLoaded ? '🔧 WASM (IEEE-754 real)' : '📝 Simulação JS (aproximação)'}</p>
<p><strong>Média (μ):</strong> <span class="error-value">${mean.toExponential(6)}</span></p>
<p><strong>Desvio Padrão (σ):</strong> <span class="error-value">${stdDev.toExponential(6)}</span></p>
<p><strong>Primeiros 10 erros:</strong></p>
<pre>${errors.slice(0, 10).map(e => e.toExponential(6)).join('\n')}</pre>
`;
// Histograma simplificado
const bins = 20;
const binCounts = new Array(bins).fill(0);
const min = Math.min(...errors);
const max = Math.max(...errors);
const range = max - min;
errors.forEach(e => {
const binIndex = Math.min(bins - 1, Math.floor(((e - min) / range) * bins));
binCounts[binIndex]++;
});
const maxCount = Math.max(...binCounts);
const histogramHTML = binCounts.map(count => {
const height = (count / maxCount) * 100;
return `<div class="bar" style="height: ${height}px" title="${count} amostras"></div>`;
}).join('');
resultsDiv.innerHTML += `
<h4>Distribuição</h4>
<div class="histogram">${histogramHTML}</div>
`;
});
</script>
</body>
</html>