Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by

The workaround for negative numbers (https://dev.clojure.org/jira/browse/CLJS-417) results in annoying behavior for floats:

(mod 2.1 3) ; => 2.0999999999999996

Both Clojure and the standard JavaScript modulo return the expected 2.1 here.

Two possible solutions:

  • only do the double-mod workaround when the dividend is actually negative
  • check if the dividend is smaller than the divisor and just return it in that case

9 Answers

0 votes
by

Comment made by: dnolen

Patch welcome.

0 votes
by

Comment made by: zto

The patch renames cljs.core/mod to double-mod and redefines mod to invoke js-mod directly when both args have the same sign.
It includes test cases for the previously failing cases, but I've also tested more exhaustively against the Clojure impl: https://gist.github.com/aw7/a32bd69923c65bddc23fd63ee062833c

0 votes
by

Comment made by: dnolen

Great thanks, have you submitted your Clojure CA?

0 votes
by

Comment made by: zto

Yeah, 2h ago.

0 votes
by

Comment made by: anmonteiro

Nit: shouldn't the double-mod function be marked as private then?

0 votes
by

Comment made by: zto

I guess there's no need for double-mod to exist as a separate function at all, I've added a patch where it's inlined into mod.

0 votes
by
_Comment made by: mfikes_

André, ClojureScript requires squashed patches (CLJS-2319-Fix-cljs.core-mod-handling-of-floats_INLINED.patch has two patches in it). See https://clojurescript.org/community/patches
0 votes
by

Comment made by: mfikes

Arg... something in the patch is killing the older JavaScriptCore we run tests with in Travis. My only guess is the additional mod tests may be triggering CLJS-910, but that seems unlikely.

0 votes
by
...