Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLJS and decimals #11

Closed
zikajk opened this issue Jan 23, 2024 · 3 comments
Closed

CLJS and decimals #11

zikajk opened this issue Jan 23, 2024 · 3 comments
Assignees

Comments

@zikajk
Copy link

zikajk commented Jan 23, 2024

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?

@2food
Copy link
Contributor

2food commented Jan 25, 2024

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 😄

@2food 2food self-assigned this Jan 25, 2024
@zikajk
Copy link
Author

zikajk commented Jan 25, 2024

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 .

@2food
Copy link
Contributor

2food commented Feb 7, 2024

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.

@2food 2food closed this as completed Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants