begriffs / pg_rational

Precise fractional arithmetic for PostgreSQL
MIT License
233 stars 14 forks source link

Question: How to handle multiple insertions between the 0th and 1st index? #26

Closed adam-expa closed 1 year ago

adam-expa commented 1 year ago

My understanding may be flawed here, but if the rational type still fits in 64 bits then the smallest non-zero should be ~1/(2^64). How do you handle the case where you have more that 64 insertions between two indices?

Example:

Suppose I have three items, A, B, and C with indices 0, 1, and 2 respectively. If I reorder C to be between A and B by calling rational_intermediate(0, 1) then C should now have an index of 1/2. If I then reorder B to be between A and C with rational_intermediate(0, 1/2) then B should now have an index of 1/4. Each time this operation is performed the middle index will be (1/2)^n

What happens if I perform this swap and more than 64 times? Won't the rational run out of resolution? Is there a standard practice for renormalizing indices when an index would be below the precision that a 64 bit rational can represent?

adam-expa commented 1 year ago

Never mind this was my misunderstanding of how rational_intermediate works. After testing locally it seems to not take the true midpoint!