[HN Gopher] Show HN: dnum - A small TypeScript library to handle...
___________________________________________________________________
Show HN: dnum - A small TypeScript library to handle decimal
numbers
Author : bpierre
Score : 25 points
Date : 2023-08-30 14:04 UTC (1 days ago)
(HTM) web link (github.com)
(TXT) w3m dump (github.com)
| meagher wrote:
| big fan of this library!
| bpierre wrote:
| <3
| iudqnolq wrote:
| 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)
|
| I learned it from Evan Wallace's Blog (Figma, Esbuild).
| https://madebyevan.com/algos/crdt-fractional-indexing/
| quickthrower2 wrote:
| That is a first step towards an open source version of
| https://news.ycombinator.com/item?id=37328669
| guideamigo_com wrote:
| Another day.
|
| Another Typescript library that does what languages like Java,
| Python, and Go have for decades!
| [deleted]
| williamdclt wrote:
| 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.
| AA-BA-94-2A-56 wrote:
| 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;
|
| https://crates.io/crates/rust_decimal
| revskill wrote:
| Wait, you can use Java, Python, and Go in browser and the edge
| ?
| AA-BA-94-2A-56 wrote:
| You can compile Java and Go to web assembly and use those
| wherever you would have used JavaScript.
| DarkNova6 wrote:
| One can only hope for webassembly...
| JodieBenitez wrote:
| Don't know about Java or Go, but Python... yes.
|
| https://codepen.io/johndodo/pen/ExGyWbx
| [deleted]
___________________________________________________________________
(page generated 2023-08-31 23:01 UTC)