anteoas / broch

A library for handling numbers with units.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CLJS and decimals

zikajk opened this issue · comments

Hi, thanks for this useful library. However, there is one common problem with JS, precision.

CLJ environment:
(b/+ (b/millimeters 1) (b/meters 1)) => #broch/quantity[1001 "mm"]
(b/* (b/+ (b/millimeters 1) (-> (b/meters 1) (b/millimeters))) (b/meters 1.5)) => #broch/quantity[1501500 "mm²"]

CLJS environment
(b/+ (b/millimeters 1) (b/meters 1)) => #broch/quantity[1000.9999999999999 "mm"]
(b/* (b/+ (b/millimeters 1) (-> (b/meters 1) (b/millimeters))) (b/meters 1.5)) => #broch/quantity[1501499.9999999998 "mm²"]

e.g. https://mathjs.org/ doesn't have this problem.

I don't know what must be done to make this more consistent with JVM results. Maybe somebody thought about this already?

Hi, thanks for bringing this up. I haven't tested this properly in cljs apparently.

I want broch's behaviour in cljs to be equal to that in clj. In clj, I've used ratio's to maintain precision, but this isn't available in cljs (yet). So I will have to solve this differently in cljs, maybe using mathjs as you suggest. However, broch currently has no dependencies, and I would like to keep it that way.

So I'll have a think about how best to solve this. Preferably without introducing dependencies, but I might have to cede that in this case.

I'll keep you posted on this. Again, thanks for bringing it up 😄

I'm happy to help.

Math.js uses decimal.js and big.js. IIRC these ibraries handle numbers as text.

Some thoughts can be seen in this article .

Solved in latest release.
Ended up implementing the necessary exact operations on my own ratio type in cljs.
Let me know if there's any further issues.