Skip to content

Commit b77d0c9

Browse files
committed
Updating documentation
1 parent e14aa06 commit b77d0c9

File tree

1 file changed

+67
-22
lines changed

1 file changed

+67
-22
lines changed

docs/TECHNICAL_DOCUMENTATION.md

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,18 @@ The filesize.js library implements several mathematical algorithms to convert ra
106106
The basic conversion from bytes to higher-order units follows the general formula:
107107

108108
```math
109-
\text{value} = \frac{\text{bytes}}{\text{base}^{\text{exponent}}}
109+
\text{value} = \frac{\text{bytes}}{\text{divisor}[\text{exponent}]}
110110
```
111111

112112
Where:
113113
- $\text{bytes}$ is the input byte value
114-
- $\text{base}$ is either 2 (binary) or 10 (decimal) depending on the standard
114+
- $\text{divisor}[\text{exponent}]$ is a pre-computed lookup table value
115115
- $\text{exponent}$ determines the unit scale (0=bytes, 1=KB/KiB, 2=MB/MiB, etc.)
116116

117+
**Implementation Note**: For performance optimization, the library uses pre-computed lookup tables (`BINARY_POWERS` and `DECIMAL_POWERS`) instead of calculating powers at runtime:
118+
- Binary: `[1, 1024, 1048576, 1073741824, ...]` (powers of 1024)
119+
- Decimal: `[1, 1000, 1000000, 1000000000, ...]` (powers of 1000)
120+
117121
### Exponent Calculation
118122

119123
The appropriate exponent for automatic unit selection is calculated using logarithms:
@@ -137,32 +141,47 @@ Where:
137141
### Binary vs Decimal Standards
138142

139143
#### Binary Standard (IEC)
140-
Uses powers of 2 with base 1024:
144+
Uses pre-computed powers of 1024:
141145

142146
```math
143-
\text{value} = \frac{\text{bytes}}{2^{10 \cdot e}} = \frac{\text{bytes}}{1024^e}
147+
\text{value} = \frac{\text{bytes}}{\text{BINARY\_POWERS}[e]}
144148
```
145149

150+
Where `BINARY_POWERS[e] = 1024^e` for efficiency.
151+
146152
Units: B, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB
147153

148-
#### Decimal Standard (SI)
149-
Uses powers of 10 with base 1000:
154+
#### Decimal Standard (SI/JEDEC)
155+
Uses pre-computed powers of 1000:
150156

151157
```math
152-
\text{value} = \frac{\text{bytes}}{10^{3 \cdot e}} = \frac{\text{bytes}}{1000^e}
158+
\text{value} = \frac{\text{bytes}}{\text{DECIMAL\_POWERS}[e]}
153159
```
154160

161+
Where `DECIMAL_POWERS[e] = 1000^e` for efficiency.
162+
155163
Units: B, KB, MB, GB, TB, PB, EB, ZB, YB (uses JEDEC-style symbols)
156164

157165
### Bits Conversion
158166

159-
When converting to bits instead of bytes, the formula becomes:
167+
When converting to bits instead of bytes, the implementation follows this sequence:
160168

169+
1. First calculate the base value: $\text{value} = \frac{\text{bytes}}{\text{divisor}[e]}$
170+
2. Then multiply by 8: $\text{value}_{\text{bits}} = \text{value} \times 8$
171+
3. Check for overflow and auto-increment if needed
172+
173+
**Bits-Specific Overflow Handling**:
161174
```math
162-
\text{value}_{\text{bits}} = \frac{8 \cdot \text{bytes}}{\text{base}^e}
175+
\text{if } \text{value}_{\text{bits}} \geq \text{ceil} \text{ and } e < 8 \text{ then:}
176+
```
177+
```math
178+
\begin{cases}
179+
\text{value}_{\text{bits}} = \frac{\text{value}_{\text{bits}}}{\text{ceil}} \\
180+
e = e + 1
181+
\end{cases}
163182
```
164183

165-
This multiplication by 8 reflects the conversion from bytes to bits (1 byte = 8 bits).
184+
This ensures proper unit progression for bit values (e.g., 8192 Kbit becomes 8 Mbit).
166185

167186
### Precision and Rounding
168187

@@ -182,14 +201,23 @@ When precision is specified ($p > 0$), the value is adjusted to show $p$ signifi
182201
\text{precise\_value} = \text{toPrecision}(\text{rounded\_value}, p)
183202
```
184203

185-
The precision parameter takes precedence over round when both are specified. If scientific notation results (contains 'E'), the exponent is incremented and the calculation is repeated to avoid exponential notation in output.
204+
**Scientific Notation Avoidance**: If the precision formatting results in scientific notation (contains 'E') and $e < 8$, the algorithm:
205+
1. Increments the exponent: $e = e + 1$
206+
2. Recalculates the value using the new exponent
207+
3. Re-applies rounding and precision formatting
208+
4. This ensures output remains in standard decimal notation
209+
210+
The precision parameter takes precedence over round when both are specified.
186211

187212
### Overflow Handling
188213

189-
When a calculated value equals or exceeds the base threshold, the algorithm increments the exponent:
214+
The library implements two distinct overflow handling mechanisms:
215+
216+
#### 1. Main Flow Overflow (After Rounding)
217+
When the rounded value exactly equals the ceiling value:
190218

191219
```math
192-
\text{if } \text{value} \geq \text{base} \text{ and } e < 8 \text{ then:}
220+
\text{if } \text{value} = \text{ceil} \text{ and } e < 8 \text{ and } \text{exponent} = -1 \text{ then:}
193221
```
194222
```math
195223
\begin{cases}
@@ -198,7 +226,22 @@ e = e + 1
198226
\end{cases}
199227
```
200228

201-
This ensures proper unit progression (e.g., 1024 KB becomes 1 MB).
229+
**Note**: This only applies when exponent is auto-calculated (`exponent = -1`), not when manually specified.
230+
231+
#### 2. Bits-Specific Overflow (During Value Calculation)
232+
For bits conversion, overflow is checked immediately after multiplication:
233+
234+
```math
235+
\text{if } \text{value}_{\text{bits}} \geq \text{ceil} \text{ and } e < 8 \text{ then:}
236+
```
237+
```math
238+
\begin{cases}
239+
\text{value}_{\text{bits}} = \frac{\text{value}_{\text{bits}}}{\text{ceil}} \\
240+
e = e + 1
241+
\end{cases}
242+
```
243+
244+
Both mechanisms ensure proper unit progression (e.g., 1024 KB becomes 1 MB).
202245

203246
### Exponent Boundary Conditions
204247

@@ -245,19 +288,21 @@ The algorithmic complexity of the conversion process is:
245288
### Implementation Examples
246289

247290
#### Default Conversion (1536 bytes)
248-
Given: bytes = 1536, default settings (base = 10, JEDEC standard)
291+
Given: bytes = 1536, default settings (decimal, JEDEC standard)
249292

250-
1. Calculate exponent: $e = \lfloor \log_{1000}(1536) \rfloor = \lfloor 1.062 \rfloor = 1$
251-
2. Calculate value: $\text{value} = \frac{1536}{1000^1} = 1.536$
252-
3. Apply rounding (2 decimal places): $1.536 \rightarrow 1.54$
253-
4. Result: "1.54 kB"
293+
1. Calculate exponent: $e = \lfloor \frac{\ln(1536)}{\ln(1000)} \rfloor = \lfloor 1.062 \rfloor = 1$
294+
2. Lookup divisor: $\text{DECIMAL\_POWERS}[1] = 1000$
295+
3. Calculate value: $\text{value} = \frac{1536}{1000} = 1.536$
296+
4. Apply rounding (2 decimal places): $1.536 \rightarrow 1.54$
297+
5. Result: "1.54 KB"
254298

255299
#### Binary Conversion (1536 bytes)
256300
Given: bytes = 1536, base = 2 (IEC standard)
257301

258-
1. Calculate exponent: $e = \lfloor \log_{1024}(1536) \rfloor = \lfloor 1.084 \rfloor = 1$
259-
2. Calculate value: $\text{value} = \frac{1536}{1024^1} = 1.5$
260-
3. Result: "1.5 KiB"
302+
1. Calculate exponent: $e = \lfloor \frac{\ln(1536)}{\ln(1024)} \rfloor = \lfloor 1.084 \rfloor = 1$
303+
2. Lookup divisor: $\text{BINARY\_POWERS}[1] = 1024$
304+
3. Calculate value: $\text{value} = \frac{1536}{1024} = 1.5$
305+
4. Result: "1.5 KiB"
261306

262307
#### Bits Conversion with Default Base (1024 bytes)
263308
Given: bytes = 1024, bits = true, default settings (base = 10)

0 commit comments

Comments
 (0)