Skip to content

Commit 18cdadd

Browse files
committed
Merge branch 'master' of github.com:relevance/clojurescript
2 parents fa2128d + f8a03ea commit 18cdadd

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/cljs/cljs/core.cljs

+53
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,22 @@ reduces them without incurring seq initialization"
798798
([x y & more]
799799
(reduce min (min x y) more)))
800800

801+
(defn- fix [q]
802+
(if (>= q 0)
803+
(js* "(Math.floor(~{q}))")
804+
(js* "(Math.ceil(~{q}))")))
805+
806+
(defn mod [n d]
807+
(js* "(~{n} % ~{d})"))
808+
809+
(defn quot [n d]
810+
(let [rem (mod n d)]
811+
(fix (js* "((~{n} - ~{rem}) / ~{d})"))))
812+
813+
(defn rem [n d]
814+
(let [q (quot n d)]
815+
(js* "(~{n} - (~{d} * ~{q}))")))
816+
801817
(defn bit-xor
802818
"Bitwise exclusive or"
803819
[x y] (js* "(~{x} ^ ~{y})"))
@@ -3119,6 +3135,43 @@ reduces them without incurring seq initialization"
31193135
(assert (= (find {:a 1} nil) nil))
31203136
(assert (= (find {:a 1 :b 2} nil) nil))
31213137
(assert (= (find [1 2 3] 0) [0 1]))
3138+
3139+
;; mod,quot,rem
3140+
(assert (= (quot 4 2) 2))
3141+
(assert (= (quot 3 2) 1))
3142+
(assert (= (quot 6 4) 1))
3143+
(assert (= (quot 0 5) 0))
3144+
(assert (= (quot 42 5) 8))
3145+
(assert (= (quot 42 -5) -8))
3146+
(assert (= (quot -42 -5) 8))
3147+
(assert (= (quot 9 3) 3))
3148+
(assert (= (quot 9 -3) -3))
3149+
(assert (= (quot -9 3) -3))
3150+
(assert (= (quot 2 -5) 0))
3151+
(assert (= (quot -2 5) 0))
3152+
(assert (= (quot 0 3) 0))
3153+
(assert (= (quot 0 -3) 0))
3154+
3155+
(assert (= (mod 4 2) 0))
3156+
(assert (= (mod 3 2) 1))
3157+
(assert (= (mod 6 4) 2))
3158+
(assert (= (mod 0 5) 0))
3159+
(assert (= (mod 4.5 2.0) 0.5))
3160+
(assert (= (mod 42 5) 2))
3161+
(assert (= (mod 9 3) 0))
3162+
(assert (= (mod 9 -3) 0))
3163+
(assert (= (mod -9 3) 0))
3164+
(assert (= (mod -9 -3) 0))
3165+
(assert (= (mod 0 3) 0))
3166+
(assert (= (mod 3216478362187432 432143214) 120355456))
3167+
3168+
(assert (= (rem 4 2) 0))
3169+
(assert (= (rem 0 5) 0))
3170+
(assert (= (rem 4.5 2.0) 0.5))
3171+
(assert (= (rem 42 5) 2))
3172+
(assert (= (rem 2 5) 2))
3173+
(assert (= (rem 2 -5) 2))
3174+
(assert (= (rem 0 3) 0))
31223175

31233176
:ok
31243177
)

0 commit comments

Comments
 (0)