Commit b9c526e
authored
[decimal] Use Chudnovsky with binary splitting to calculate pi (#95)
This pull request introduces significant improvements to the
`BigDecimal` library by optimizing the calculation of π (pi) for high
precision and documenting the performance characteristics of different
algorithms. The main changes include replacing Machin's formula with the
Chudnovsky algorithm for faster computation, introducing binary
splitting for efficiency, and adding internal notes on time complexity
and algorithm results.
### Optimizations for π Calculation:
*
[`src/decimojo/bigdecimal/constants.mojo`](diffhunk://#diff-41cd4654562f4a78c9b73f60941a2f1e1d8509a3dc8a93469408e4a5456619f2R154-R289):
Replaced Machin's formula with the Chudnovsky algorithm for π
calculation, leveraging binary splitting for higher precision and faster
computation. Added caching logic to store precomputed values for
precision ≤ 1024.
*
[`src/decimojo/bigdecimal/constants.mojo`](diffhunk://#diff-41cd4654562f4a78c9b73f60941a2f1e1d8509a3dc8a93469408e4a5456619f2R154-R289):
Introduced helper functions `pi_chudnovsky_binary_split`,
`chudnovsky_split`, and `compute_m_k_rational` to implement the
Chudnovsky algorithm efficiently.
### Documentation Updates:
*
[`docs/internal_notes.md`](diffhunk://#diff-6da6b2e1a170d4269cd7863de3fd61fd4098c67fc788deb68bbe6f0f18b98d24R3-R10):
Added a section describing time complexity and performance results for π
calculation using Machin's formula and the Chudnovsky algorithm.
### Code Cleanup:
*
[`src/decimojo/bigdecimal/constants.mojo`](diffhunk://#diff-41cd4654562f4a78c9b73f60941a2f1e1d8509a3dc8a93469408e4a5456619f2L196-L197):
Removed redundant code related to Machin's formula for π, including
unused variables and outdated comments.1 parent a195b9e commit b9c526e
File tree
2 files changed
+134
-15
lines changed- docs
- src/decimojo/bigdecimal
2 files changed
+134
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
154 | 157 | | |
155 | | - | |
156 | | - | |
| 158 | + | |
157 | 159 | | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
| 160 | + | |
| 161 | + | |
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
| 166 | + | |
| 167 | + | |
171 | 168 | | |
172 | 169 | | |
173 | 170 | | |
174 | | - | |
175 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
176 | 290 | | |
177 | 291 | | |
178 | 292 | | |
| |||
193 | 307 | | |
194 | 308 | | |
195 | 309 | | |
196 | | - | |
197 | | - | |
198 | 310 | | |
199 | 311 | | |
200 | 312 | | |
0 commit comments