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

error with problem 100 (CLJS comparison of floats vs CLJ comparison of Ratio) #82

Open
bo-tato opened this issue Feb 13, 2023 · 6 comments

Comments

@bo-tato
Copy link

bo-tato commented Feb 13, 2023

with problem 100 https://4clojure.oxal.org/#/problem/100 when I run my solution locally I get all tests passing but on the site only 2/5 pass
when I submit any of the solutions from the solutions archive they also only get 2/5 passing so I think something's wrong with the site's evalutation

@borkdude
Copy link
Collaborator

Can you be more specific, e.g. list your solution here, so we can actually reproduce this?

@bo-tato
Copy link
Author

bo-tato commented Feb 13, 2023

sure, my solution was:

(fn lcm
  [head & tail]
  (first (filter
          #(every? (fn [num]
                     (zero? (mod % num)))
                   tail)
          (range head 999 head))))

when I run locally:

(def lcm (fn lcm
  [head & tail]
  (first (filter
          #(every? (fn [num]
                     (zero? (mod % num)))
                   tail)
          (range head 999 head)))))
(== (lcm 2 3) 6)
(== (lcm 5 3 7) 105)
(== (lcm 1/3 2/5) 2)
(== (lcm 3/4 1/6) 3/2)
(== (lcm 7 5/7 2 3/5) 210)

they all return true.
Also rather than having range with a hardcoded max of 999, when I use an infinite lazy seq (iterate (partial + head) head) it runs fine locally but hangs in the browser
I don't think it's something to do with my solution though as I tried submitting 5-10 random solutions from the solutions page https://4clojure.oxal.org/#/problem/100/solutions
and they all also only pass the first two tests (on the site, but work locally)

@borkdude
Copy link
Collaborator

borkdude commented Feb 13, 2023

@bo-tato The reason is probably that == in JVM Clojure is type-independent numerical comparison, but the solutions in this website are evaluated using CLJS and SCI, in which == has a different meaning. Also 3/4 are Ratio types in Clojure which preserve more than floating point numbers. CLJS doesn't have that concept.

We'll have to think what a proper solution for this website and problem number 100 is.

@bo-tato
Copy link
Author

bo-tato commented Feb 13, 2023

interesting, and I guess the reason the solutions that use an infinite seq and return the first solution hang is without the ratio types they never find any solution that is exactly equal

@bo-tato
Copy link
Author

bo-tato commented Feb 14, 2023

a similar thing in problem 108, in order to get my solution to pass I had to replace = with:

(fn kinda-equal [a b]
                      (< -0.001 (- a b) 0.001))

it could be good to just add a simple note on those problems, saying that in JVM clojure it uses exact ratio types while CLJS is using floating point, to save beginners some confusion why their code works locally but not when they submit

@borkdude borkdude changed the title error with problem 100 error with problem 100 (CLJS comparison of floats vs CLJ comparison of Ratio) Mar 12, 2023
@borkdude
Copy link
Collaborator

Interesting library in the context of this issue: https://github.com/microsoft/same-ish

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