Skip to content

Optimized Ord::cmp function#147

Open
mikem8891 wants to merge 3 commits intorust-num:masterfrom
mikem8891:ord-cmp
Open

Optimized Ord::cmp function#147
mikem8891 wants to merge 3 commits intorust-num:masterfrom
mikem8891:ord-cmp

Conversation

@mikem8891
Copy link

@mikem8891 mikem8891 commented Feb 25, 2026

This implementation of the Ord::cmp function does several early comparisons to try to determine ordering before resulting to div and mod operations. This allows about half of unsigned Ratio<t> pairs and 3/4 of signed Ratio<T> pairs to be determined using simple comparisons (based on the entire input space, individual results may vary). If simple comparisons don't determine the ordering, then the function enters a tight loop to converge on a solution as quickly as possible.

Note, #121 would be faster, but would still benefit from this pull request if checked_mul returns None.

I confirmed this fixes #140 . This solution is iterative instead of recursive.

This should also fixes #146 . I phoned this in because I think negative denominators should not be adversely affecting performance or complexity compared to normal fractions.

This implementation seeks to minimize the about of division and modulus operations to improve efficiency
Optimized the Ord::cmp function after benchmarking it against the original function.
Reduced the need to clone values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ord:cmp does not properly handle Ratio<T> with negative denominators Stackoverflow with PartialEq and PartialOrd

1 participant