@@ -105,62 +105,62 @@ The filesize.js library implements several mathematical algorithms to convert ra
105105
106106The basic conversion from bytes to higher-order units follows the general formula:
107107
108- $$
108+ ``` math
109109\text{value} = \frac{\text{bytes}}{\text{base}^{\text{exponent}}}
110- $$
110+ ```
111111
112112Where:
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.)
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.)
116116
117117### Exponent Calculation
118118
119119The appropriate exponent for automatic unit selection is calculated using logarithms:
120120
121- $$
121+ ``` math
122122e = \lfloor \log_{\text{base}}(\text{bytes}) \rfloor
123- $$
123+ ```
124124
125125For implementation efficiency, this is computed using the change of base formula:
126126
127- $$
127+ ``` math
128128e = \left\lfloor \frac{\ln(\text{bytes})}{\ln(\text{base})} \right\rfloor
129- $$
129+ ```
130130
131131Where:
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
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
136136
137137### Binary vs Decimal Standards
138138
139139#### Binary Standard (IEC)
140140Uses powers of 2 with base 1024:
141141
142- $$
142+ ``` math
143143\text{value} = \frac{\text{bytes}}{2^{10 \cdot e}} = \frac{\text{bytes}}{1024^e}
144- $$
144+ ```
145145
146146Units: B, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB
147147
148148#### Decimal Standard (SI/JEDEC)
149149Uses powers of 10 with base 1000:
150150
151- $$
151+ ``` math
152152\text{value} = \frac{\text{bytes}}{10^{3 \cdot e}} = \frac{\text{bytes}}{1000^e}
153- $$
153+ ```
154154
155155Units: B, KB, MB, GB, TB, PB, EB, ZB, YB
156156
157157### Bits Conversion
158158
159159When converting to bits instead of bytes, the formula becomes:
160160
161- $$
161+ ``` math
162162\text{value}_{\text{bits}} = \frac{8 \cdot \text{bytes}}{\text{base}^e}
163- $$
163+ ```
164164
165165This multiplication by 8 reflects the conversion from bytes to bits (1 byte = 8 bits).
166166
@@ -169,93 +169,93 @@ This multiplication by 8 reflects the conversion from bytes to bits (1 byte = 8
169169#### Decimal Rounding
170170The rounding operation applies a power-of-10 scaling factor:
171171
172- $$
172+ ``` math
173173\text{rounded\_value} = \frac{\text{round}(\text{value} \cdot 10^r)}{10^r}
174- $$
174+ ```
175175
176- Where \( r \) is the number of decimal places specified by the ` round ` parameter.
176+ Where $r$ is the number of decimal places specified by the ` round ` parameter.
177177
178178#### Significant Digits (Precision)
179- When precision is specified, the value is adjusted to show \( p \) significant digits:
179+ When precision is specified, the value is adjusted to show $p$ significant digits:
180180
181- $$
181+ ``` math
182182\text{precise\_value} = \text{toPrecision}(\text{value}, p)
183- $$
183+ ```
184184
185185This uses JavaScript's built-in precision formatting rather than mathematical rounding.
186186
187187### Overflow Handling
188188
189189When a calculated value equals or exceeds the base threshold, the algorithm increments the exponent:
190190
191- $$
191+ ``` math
192192\text{if } \text{value} \geq \text{base} \text{ and } e < 8 \text{ then:}
193- $$
194- $$
193+ ```
194+ ``` math
195195\begin{cases}
196196\text{value} = 1 \\
197197e = e + 1
198198\end{cases}
199- $$
199+ ```
200200
201201This ensures proper unit progression (e.g., 1024 KB becomes 1 MB).
202202
203203### Exponent Boundary Conditions
204204
205205The library enforces boundaries on the exponent:
206206
207- $$
207+ ``` math
208208e = \begin{cases}
2092090 & \text{if } e < 0 \\
2102108 & \text{if } e > 8 \\
211211e & \text{otherwise}
212212\end{cases}
213- $$
213+ ```
214214
215215For exponents exceeding 8, precision adjustment occurs:
216216
217- $$
217+ ``` math
218218\text{precision}_{\text{adjusted}} = \text{precision} + (8 - e_{\text{original}})
219- $$
219+ ```
220220
221221### Special Cases
222222
223223#### Zero Input
224224When the input is zero:
225225
226- $$
226+ ``` math
227227\text{value} = 0, \quad e = 0, \quad \text{unit} = \text{base unit}
228- $$
228+ ```
229229
230230#### Negative Input
231231For negative inputs, the absolute value is processed and the sign is preserved:
232232
233- $$
233+ ``` math
234234\text{result} = -\left|\text{filesize}(|\text{bytes}|, \text{options})\right|
235- $$
235+ ```
236236
237237### Mathematical Complexity
238238
239239The algorithmic complexity of the conversion process is:
240240
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 \)
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$
244244
245245### Implementation Examples
246246
247247#### Standard Conversion (1536 bytes)
248248Given: bytes = 1536, base = 1024 (binary)
249249
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 \)
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$
2522523 . Result: "1.5 KiB"
253253
254254#### Bits Conversion (1024 bytes)
255255Given: bytes = 1024, bits = true, base = 1024
256256
257- 1 . Calculate exponent: \( e = \lfloor \log_ {1024}(1024) \rfloor = 1 \)
258- 2 . Calculate value: \( \ text{value} = \frac{1024 \cdot 8}{1024^1} = 8 \)
257+ 1 . Calculate exponent: $ e = \lfloor \log_ {1024}(1024) \rfloor = 1$
258+ 2 . Calculate value: $\ text{value} = \frac{1024 \cdot 8}{1024^1} = 8$
2592593 . Result: "8 Kib"
260260
261261## Data Flow
0 commit comments