1. 32

  2. 4

    Rationals are so underrated.

    I am constantly surprised by how many times I’m working on something and the perfect solution to the problem would be rationals, but the languages/frameworks/libraries/utilities at $DAYJOB don’t support them.

    As a further example beyond the one in this article, there’s safe-money.

    C#.NET and M$ SQL Server still have no built-in support for Rationals. It’s not hard to implement Rationals on top of Ints, but it means either extending the language (and creating a sane representation mapping in the db) with a pretty fundamental type or adding an external dependency (which I’m generally against).

    This is yet another reason I’m so taken by Agda as a possibility for future development work. It has an incredibly powerful, dependent type system (including some of these fundamental types that I expect higher-level languages to have), wonderful syntax and identifier rules (notation as a tool of thought is one of my favorite paradigms championed by APL).

    Fun write-up, thank you @begriffs. /me mentally marks down arbitrary user ordering as another wonderful application for rationals.

    1. 2

      When I was doing video at Apple, I was always angry that neither Objective-C nor Java had a rational datatype. Particularly with video, you are almost never dealing with numbers that aren’t best represented as rationals. Trying to convince people that 23.976 is wrong and the right number is 24 * 1000/1001 was exhausting.

    2. 3

      That does seem rather clever, though it raises design questions in my mind: Why not define user defined order in some other way, such as a pg_array in a different table?

      1. 3

        If by pg_array you mean standard Postgres array type, then my answer would be: mainly because current Postgres lacks support for arrays referring to a foreign key. Also possibly because of increased lock granularity in case of an array.

      2. 3

        This seems pleasingly elegant. I wonder, if this were a built in feature, could it be given the ability to renumber occasionally on the fly whenever the numbering gets too dense in some part of the order, so as to completely lift that just-under-ten-million-subdivisions limit. ;)

        I recently found out that MS SQL server has a data type designed for a similar use case, called hierarchyid. It does paths into user-ordered trees and sounds like it’s meant for comment threading. Quoth the manual:

        By using the GetDescendant method, it is always possible to generate a sibling to the right of any given node, to the left of any given node, or between any two siblings.

        and the amount of data they claim this uses is pretty minimal - for trees with low fan out. Not sure what the space bounds are for high fan-out trees such as a tree with one parent and thousands of children, though.