I’m not sure what’s your point? That JS shouldn’t have this library because other languages did it first a while ago? Should concepts only be implemented in one language because after that it’s rediscovering knowledge rather than being inventive?
I also really dislike the undertone of « other languages are better », which is naive at best.
This. Fixed-point decimal isn't even implemented in standard Rust– which I would deem a very mature language, because they want to keep the standard library quite small. If you want to use fixed-point decimal, there's a crate for that;
Rust standard library is very limited and IMO a huge downside of the language, especially as the third party libraries to replace it are not up to par and also often the community has not agreed on one single standard so writing higher level libraries forces you to pick one side of the ecosystem
I'm curious where you got this information from? How do you know the third-party libraries are not up to par? Is this personal or second-hand experience? Is it an assumption?
I recently learned a very cute technique for storing arbitrary precision decimals between 0 and 1 as strings. This is useful for representing the index of items in a reorderable list because you can always make a new index in between two existing indexes (so long as you ensure each index is unique).
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)
22 comments
[ 2.8 ms ] story [ 61.7 ms ] threadAnother Typescript library that does what languages like Java, Python, and Go have for decades!
https://codepen.io/johndodo/pen/ExGyWbx
I also really dislike the undertone of « other languages are better », which is naive at best.
https://crates.io/crates/rust_decimal
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.