-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-arithmetic-drift-simple.err
More file actions
83 lines (68 loc) · 2.02 KB
/
Copy path08-arithmetic-drift-simple.err
File metadata and controls
83 lines (68 loc) · 2.02 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
# SPDX-License-Identifier: MPL-2.0
# Example: Arithmetic Drift (Simplified)
# Learning: Floating-point precision isn't perfect - errors accumulate
main
# Simple arithmetic - should be exact, right?
let a = 1 + 1
println("1 + 1 =", a)
let b = 2 + 2
println("2 + 2 =", b)
let c = 10 / 3
println("10 / 3 =", c)
# Accumulating drift through multiple operations
let d = 0 + 1
println("Step 1:", d)
let e = d + 1
println("Step 2:", e)
let f = e + 1
println("Step 3:", f)
let g = f + 1
println("Step 4:", g)
let h = g + 1
println("Step 5:", h)
# The famous floating-point issue
let x = 0.1 + 0.2
println("0.1 + 0.2 =", x)
println("(Expected: 0.3)")
# More operations = more drift
let y = 0.1 + 0.1
println("0.1 + 0.1 =", y)
let z = y + 0.1
println("0.1 + 0.1 + 0.1 =", z)
println("(Expected: 0.3)")
# Multiplication magnifies error
let m = 0.1 * 3
println("0.1 * 3 =", m)
println("(Expected: 0.3)")
# Division creates even more drift
let div = 10 / 3
println("10 / 3 =", div)
let div2 = div * 3
println("(10 / 3) * 3 =", div2)
println("(Expected: 10.0)")
end
# 🎲 EXPECTED BEHAVIOR:
# Each arithmetic operation introduces a small random error:
# - Addition: ±0.0001 to ±0.01 depending on operation count
# - Subtraction: ±0.0001 to ±0.01
# - Multiplication: ±0.001 to ±0.1
# - Division: ±0.01 to ±0.5
#
# Error accumulates over time - watch the drift grow!
#
# 🎓 LEARNING OUTCOME:
# Floating-point arithmetic is NOT exact!
# - IEEE 754 has limited precision
# - Errors compound over many operations
# - Order of operations matters
# - Never compare floats with ==
#
# Real-world parallel:
# - JavaScript: 0.1 + 0.2 !== 0.3 (0.30000000000000004)
# - Financial calculations require decimal types
# - Scientific computing needs error analysis
#
# 💡 STABILIZATION:
# 1. Use integer arithmetic → stability: 100
# 2. Limit operation chains → stability: 90
# 3. Use decimal types → stability: 95