Show HN: dnum − A small TypeScript library to handle decimal numbers
github.com
github.com
Creating a new index in between two indices and comparing two indices are both O(N), but the cute part is that the standard string comparison in JavaScript just works.
You just encode the digits after the decimal point. As in for "0.15" you encode "15". Except you do it in a big base, and to support concurrent edits you add some randomness to the midpoint operation (because this falls apart if two items have identical indexes)
I learned it from Evan Wallace's Blog (Figma, Esbuild). https://madebyevan.com/algos/crdt-fractional-indexing/
Does this mean what is says ? The library supports positive decimals only ?
Numbers are represented in dnum using a pair of integer + precision decimals. For example, this is the number 1.0 with a precision of 18 decimals:
[1000000000000000000n, 18]
A number cannot have less decimals than none, so this is why this error exists.
Another Typescript library that does what languages like Java, Python, and Go have for decades!
I also really dislike the undertone of « other languages are better », which is naive at best.