cisco / ChezScheme

Chez Scheme
Apache License 2.0
6.91k stars 982 forks source link

Replace naive complex division by Algorithm 116 #710

Closed LdBeth closed 11 months ago

LdBeth commented 11 months ago

Using Robert L. Smith's "Algorithm 116: Complex division", which fixes nan produced by original naive division method and potential float overflows.

fix #709

LdBeth commented 11 months ago

Some test cases are added and this is ready for code review. Please let me know if there is more test case I can add or code styling issue.

burgerrg commented 11 months ago

Thank you for finding this! Numerical Recipes in C uses the same algorithm because it avoids overflows, underflow, and loss of precision.

burgerrg commented 11 months ago

@LdBeth, I made minor changes to the code, added a couple tests, and updated the documentation. Please let me know if you find any mistakes. Thanks!

jltaylor-us commented 11 months ago

fwiw, Smith has (or is co-author on) an "improved" algorithm that handles even more edge cases, but at the cost of some additional complexity (and thus lower performance). (I'm not suggesting that we need to implement this algorithm, just pointing out that it exists.) https://arxiv.org/abs/1210.4539

LdBeth commented 11 months ago

@burgerrg I have no problem with the changes. Thank you for the documentation update!