I’m participating in the 48in24 challenge, but to unlock ‘leap’ challenge I also need to complete this one first.
I believe my code should work, because I’ve checked others’ solutions and they’re very close to mine.
Now 3/6 tests (number 4,5,6) are failing with very confusing error message:
“Expected (= 1701.7 (cars-assemble/production-rate 10)) but got (not (= 1701.7 1701.7))”
Turns out, for some reason - order of multiplication matters: (* 221 speed (success-rate speed)) passes all tests, but (* 221 (success-rate speed) speed) fails.
Maybe test should be changed to not so precisely test floats?
Presuming the issue here is that the number aren’t rounded as you’re expecting, that’s an “issue” with computers, not clojure or Exercism: https://0.30000000000000004.com/
I, believe, there’s nothing wrong with Clojure. It’s just that floating point numbers have limited precision. You can test this in any language you like and most will get it wrong (maybe all, except raku, because it uses decimal numbers instead):
(221 * 10 * 0.77) == (221 * 0.77 * 10) // False!
But, I also believe, that it’s the issue with the test setup. It’s generally a bad practice to have normal comparison against floats, especially ==. It’s always better to use epsilon value or language-defined implementation of it (e.g. isCloseTo() in java). You can see proper implementation of this test in the Java track.
That’s correct, the tests should take care of floating point issues, especially when the only thing that needs to be changed is the order of operations. I’ve fixed a couple exercises in the past, this one was on the todo list but completely forgot to do it.
Yeah, using epsilon is usually the way it is done. My approach just forces doubles into floats. Something like (= 1701.7 1701.7000000000003) which fails because it’s comparing two doubles, becomes (= float(1701.7) float(1701.7000000000003)) and now both numbers are floats. The second one will get truncated and lose its last digits and the check will pass.
This is more than enough and has worked great so far, as in Complex Numbers in Clojure on Exercism for example. People stopped coming up with all sorts of clunky solutions after the fix and the previous solutions all continued to work.
Anyway, let’s hope that someone re-opens this PR. I still need to fix that typo.