This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-05-27
Channels
- # announcements (8)
- # babashka (11)
- # beginners (34)
- # clerk (11)
- # clj-http (2)
- # clojure (5)
- # clojure-europe (9)
- # clojure-gamedev (1)
- # clojure-nl (1)
- # clojure-norway (17)
- # clojure-poland (1)
- # clojure-sweden (5)
- # clojure-uk (9)
- # clojurescript (17)
- # core-typed (12)
- # cursive (4)
- # datahike (4)
- # datalevin (2)
- # datomic (7)
- # emacs (8)
- # events (8)
- # graphql (5)
- # gratitude (1)
- # hyperfiddle (19)
- # jobs-discuss (4)
- # leiningen (4)
- # lsp (21)
- # meander (2)
- # off-topic (9)
- # play-clj (1)
- # polylith (10)
- # releases (1)
- # sci (18)
- # vim (10)
What functions would allow for x + y, and x - y, where x and y start off as strings that are limited to two decimal points (e.g 1.01)? I started off assuming i should parse the x and y, as floats, then fix them to two decimals (anything more isn't allowed by the business logic) and then parse that into a float again (because .toFixed returns a string). That looks like this in cljs:
(js/parseFloat (.toFixed (js/parseFloat "1.101") 2))
;; => 1.1
From there i assumed i could safely add and subtract , but i was quickly proven wrong (js code):
> 12.8 + 1.3
14.100000000000001
To get around this, i assumed i could multiple the number by 100 then parse to an int then do the math, then divide by 100, but that also causes arithmetic errors (js code):
> 100 * 1.1
110.00000000000001
So Instead of trying to do anything besides parse a float, i used string manipulation to make things into integers and then piece them back together (cljs):
(let [[d r] (clojure.string/split (.toFixed (js/parseFloat "1.101") 2) #"\.")
b (js/parseInt (str d r))
s (str b)
split-index (- (count s) 2)
a (str (subs s 0 split-index) "." (subs s split-index))]
{:before b ;; the result of parsing the users float
:after a}) ;; after some addition or subtraction, turn the number back into something that looks like a float with two decimal points
;; => {:after "1.10", :before 110}
This seems to work on the first three examples i tested, but it feels hacky. However that hackyness could be that the users didn't need to input decimal values in the first place, as their is no sense of fractions in the problem (no division or multiplication) and instead the users should have just encoded the numbers using whole numbers (e.g 110).there are no integers in Javascript, only Number, and those errors are floating point precision errors which will appear regardless of how you parse strings as long as they end up as Numbers. i think most people just truncate the unwanted precision, or use a big decimal library if the math is important to be correct
( well there is there is BigInt
if you can represent your problem using that )
Your saying, any two Js Numbers, even if their "Whole Numbers" {0,1,2...} when added can result in a number which isn't actually the result of adding them?
arithmetic with 2 whole numbers never has precision errors I don't think, but certainly arithmetic with a whole number and non-whole numbers can
basically you'll have to chop off everything but the first two digits after every operation, in your case, i think, as youve described it
I would model it with either integers or fractions. IIRC there was a library for CLJS that implemented fractions, or maybe it was a JS one. But it's also easy to implement it yourself if the number of operations that you need is small.
I just to support addition really.
I'm not sure but I think you're safe truncating every time, seems like your problem only requires a fixed number of digits to support
Yeah, in this case that feels correct. In the other, which deals with american currency, i'm going to do some division so it's going to get tricky in another way.
currency is outside of my expertise 😬
Also note that JS does have a separate int type that might be useful if you decide to go the ratio route - js/BigInt
.
We have ratios in #emmy , initially we wrapped https://github.com/infusion/Fraction.js/ but the current implementation is all cljs
Though with currency it does feel like you’d be better off using integers for everything and dividing by 100 only when you need to display (so, calculate with pennies)
Doing currency with floats or doubles is a big anti pattern.