Skip to content

Commit ec20636

Browse files
committed
Adding 'Mathematical Foundation' section to 'TECHNICAL_DOCUMENTATION.md'
1 parent 21d3c2f commit ec20636

File tree

1 file changed

+170
-8
lines changed

1 file changed

+170
-8
lines changed

docs/TECHNICAL_DOCUMENTATION.md

Lines changed: 170 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
1. [Overview](#overview)
66
2. [Architecture](#architecture)
7-
3. [Data Flow](#data-flow)
8-
4. [API Reference](#api-reference)
9-
5. [Usage Patterns](#usage-patterns)
10-
6. [Modern Application Examples (2025)](#modern-application-examples-2025)
11-
7. [Internationalization & Localization](#internationalization--localization)
12-
8. [Performance Considerations](#performance-considerations)
13-
9. [Integration Patterns](#integration-patterns)
14-
10. [Troubleshooting](#troubleshooting)
7+
3. [Mathematical Foundation](#mathematical-foundation)
8+
4. [Data Flow](#data-flow)
9+
5. [API Reference](#api-reference)
10+
6. [Usage Patterns](#usage-patterns)
11+
7. [Modern Application Examples (2025)](#modern-application-examples-2025)
12+
8. [Internationalization & Localization](#internationalization--localization)
13+
9. [Performance Considerations](#performance-considerations)
14+
10. [Integration Patterns](#integration-patterns)
15+
11. [Troubleshooting](#troubleshooting)
1516

1617
## Overview
1718

@@ -96,6 +97,167 @@ graph LR
9697
style C fill:#1e40af,stroke:#1e3a8a,stroke-width:2px,color:#ffffff
9798
```
9899

100+
## Mathematical Foundation
101+
102+
The filesize.js library implements several mathematical algorithms to convert raw byte values into human-readable format with appropriate units. This section describes the core mathematical formulas and their implementations.
103+
104+
### Fundamental Conversion Formula
105+
106+
The basic conversion from bytes to higher-order units follows the general formula:
107+
108+
$$
109+
\text{value} = \frac{\text{bytes}}{\text{base}^{\text{exponent}}}
110+
$$
111+
112+
Where:
113+
- \( \text{bytes} \) is the input byte value
114+
- \( \text{base} \) is either 2 (binary) or 10 (decimal) depending on the standard
115+
- \( \text{exponent} \) determines the unit scale (0=bytes, 1=KB/KiB, 2=MB/MiB, etc.)
116+
117+
### Exponent Calculation
118+
119+
The appropriate exponent for automatic unit selection is calculated using logarithms:
120+
121+
$$
122+
e = \lfloor \log_{\text{base}}(\text{bytes}) \rfloor
123+
$$
124+
125+
For implementation efficiency, this is computed using the change of base formula:
126+
127+
$$
128+
e = \left\lfloor \frac{\ln(\text{bytes})}{\ln(\text{base})} \right\rfloor
129+
$$
130+
131+
Where:
132+
- \( \ln \) is the natural logarithm
133+
- \( \lfloor \cdot \rfloor \) is the floor function
134+
- \( \text{base} = 1024 \) for binary (IEC) standard
135+
- \( \text{base} = 1000 \) for decimal (SI/JEDEC) standards
136+
137+
### Binary vs Decimal Standards
138+
139+
#### Binary Standard (IEC)
140+
Uses powers of 2 with base 1024:
141+
142+
$$
143+
\text{value} = \frac{\text{bytes}}{2^{10 \cdot e}} = \frac{\text{bytes}}{1024^e}
144+
$$
145+
146+
Units: B, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB
147+
148+
#### Decimal Standard (SI/JEDEC)
149+
Uses powers of 10 with base 1000:
150+
151+
$$
152+
\text{value} = \frac{\text{bytes}}{10^{3 \cdot e}} = \frac{\text{bytes}}{1000^e}
153+
$$
154+
155+
Units: B, KB, MB, GB, TB, PB, EB, ZB, YB
156+
157+
### Bits Conversion
158+
159+
When converting to bits instead of bytes, the formula becomes:
160+
161+
$$
162+
\text{value}_{\text{bits}} = \frac{8 \cdot \text{bytes}}{\text{base}^e}
163+
$$
164+
165+
This multiplication by 8 reflects the conversion from bytes to bits (1 byte = 8 bits).
166+
167+
### Precision and Rounding
168+
169+
#### Decimal Rounding
170+
The rounding operation applies a power-of-10 scaling factor:
171+
172+
$$
173+
\text{rounded\_value} = \frac{\text{round}(\text{value} \cdot 10^r)}{10^r}
174+
$$
175+
176+
Where \( r \) is the number of decimal places specified by the `round` parameter.
177+
178+
#### Significant Digits (Precision)
179+
When precision is specified, the value is adjusted to show \( p \) significant digits:
180+
181+
$$
182+
\text{precise\_value} = \text{toPrecision}(\text{value}, p)
183+
$$
184+
185+
This uses JavaScript's built-in precision formatting rather than mathematical rounding.
186+
187+
### Overflow Handling
188+
189+
When a calculated value equals or exceeds the base threshold, the algorithm increments the exponent:
190+
191+
$$
192+
\text{if } \text{value} \geq \text{base} \text{ and } e < 8 \text{ then:}
193+
$$
194+
$$
195+
\begin{cases}
196+
\text{value} = 1 \\
197+
e = e + 1
198+
\end{cases}
199+
$$
200+
201+
This ensures proper unit progression (e.g., 1024 KB becomes 1 MB).
202+
203+
### Exponent Boundary Conditions
204+
205+
The library enforces boundaries on the exponent:
206+
207+
$$
208+
e = \begin{cases}
209+
0 & \text{if } e < 0 \\
210+
8 & \text{if } e > 8 \\
211+
e & \text{otherwise}
212+
\end{cases}
213+
$$
214+
215+
For exponents exceeding 8, precision adjustment occurs:
216+
217+
$$
218+
\text{precision}_{\text{adjusted}} = \text{precision} + (8 - e_{\text{original}})
219+
$$
220+
221+
### Special Cases
222+
223+
#### Zero Input
224+
When the input is zero:
225+
226+
$$
227+
\text{value} = 0, \quad e = 0, \quad \text{unit} = \text{base unit}
228+
$$
229+
230+
#### Negative Input
231+
For negative inputs, the absolute value is processed and the sign is preserved:
232+
233+
$$
234+
\text{result} = -\left|\text{filesize}(|\text{bytes}|, \text{options})\right|
235+
$$
236+
237+
### Mathematical Complexity
238+
239+
The algorithmic complexity of the conversion process is:
240+
241+
- **Time Complexity**: \( O(1) \) - constant time for all operations
242+
- **Space Complexity**: \( O(1) \) - constant space usage
243+
- **Numerical Precision**: IEEE 754 double precision for values up to \( 2^{53} - 1 \)
244+
245+
### Implementation Examples
246+
247+
#### Standard Conversion (1536 bytes)
248+
Given: bytes = 1536, base = 1024 (binary)
249+
250+
1. Calculate exponent: \( e = \lfloor \log_{1024}(1536) \rfloor = \lfloor 1.084 \rfloor = 1 \)
251+
2. Calculate value: \( \text{value} = \frac{1536}{1024^1} = 1.5 \)
252+
3. Result: "1.5 KiB"
253+
254+
#### Bits Conversion (1024 bytes)
255+
Given: bytes = 1024, bits = true, base = 1024
256+
257+
1. Calculate exponent: \( e = \lfloor \log_{1024}(1024) \rfloor = 1 \)
258+
2. Calculate value: \( \text{value} = \frac{1024 \cdot 8}{1024^1} = 8 \)
259+
3. Result: "8 Kib"
260+
99261
## Data Flow
100262

101263
### Primary Processing Pipeline

0 commit comments

Comments
 (0)