You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- $\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
115
115
- $\text{exponent}$ determines the unit scale (0=bytes, 1=KB/KiB, 2=MB/MiB, etc.)
116
116
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
+
117
121
### Exponent Calculation
118
122
119
123
The appropriate exponent for automatic unit selection is calculated using logarithms:
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.
186
211
187
212
### Overflow Handling
188
213
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:
190
218
191
219
```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:}
193
221
```
194
222
```math
195
223
\begin{cases}
@@ -198,7 +226,22 @@ e = e + 1
198
226
\end{cases}
199
227
```
200
228
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:}
0 commit comments