Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
quot and rem are inefficient
Key details
Description
The implementation of the quot and rem functions are needlessly complicated. Currently they are:
(defn quot [n d](fix (/(- n (js-mod n d)) d)))(defn rem [n d](- n (* d (quot n d))))
However all numbers in js are doubles already, so all this is unnecessary:
(defn quot [n d](fix (/ n d)))(defn rem [n d](js-mod n d)))
Notice that "rem" is simply js-mod, and I'm not sure why no one noticed this before. I keep js-mod for now since a lot of code uses it, and if cljs ever grows a number tower the distinction may be important.
Patch attached, which also:
Creates a macro version of quot and rem.
Updates documentation for quot, rem, js-mod and mod for clarity.
Implement fix (private function to round to zero) with ES6 Math.trunc() if available.
Existing quot and rem tests pass, although there could be some better tests of edge cases (negative decimal num or div, NaN and +-Infinity args).
The implementation of the quot and rem functions are needlessly complicated. Currently they are:
(defn quot [n d] (fix (/ (- n (js-mod n d)) d))) (defn rem [n d] (- n (* d (quot n d))))However all numbers in js are doubles already, so all this is unnecessary:
(defn quot [n d] (fix (/ n d))) (defn rem [n d] (js-mod n d)))Notice that "rem" is simply js-mod, and I'm not sure why no one noticed this before. I keep js-mod for now since a lot of code uses it, and if cljs ever grows a number tower the distinction may be important.
Patch attached, which also:
Creates a macro version of quot and rem.
Updates documentation for quot, rem, js-mod and mod for clarity.
Implement fix (private function to round to zero) with ES6 Math.trunc() if available.
Existing quot and rem tests pass, although there could be some better tests of edge cases (negative decimal num or div, NaN and +-Infinity args).