Skip to content

Commit 4dc787e

Browse files
committed
CLJS-3425: Incorrect handling of ##NaN with min/max
- prefix inlining min/max as unchecked - fix min and max fns to do what Clojure does
1 parent 10fc535 commit 4dc787e

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,7 @@ reduces them without incurring seq initialization"
16081608
-1
16091609
(loop [idx (cond
16101610
(pos? start) start
1611-
(neg? start) (max 0 (+ start len))
1611+
(neg? start) (unchecked-max 0 (+ start len))
16121612
:else start)]
16131613
(if (< idx len)
16141614
(if (= (nth coll idx) x)
@@ -1624,7 +1624,7 @@ reduces them without incurring seq initialization"
16241624
(if (zero? len)
16251625
-1
16261626
(loop [idx (cond
1627-
(pos? start) (min (dec len) start)
1627+
(pos? start) (unchecked-min (dec len) start)
16281628
(neg? start) (+ len start)
16291629
:else start)]
16301630
(if (>= idx 0)
@@ -1695,7 +1695,7 @@ reduces them without incurring seq initialization"
16951695

16961696
ICounted
16971697
(-count [_]
1698-
(max 0 (- (alength arr) i)))
1698+
(unchecked-max 0 (- (alength arr) i)))
16991699

17001700
IIndexed
17011701
(-nth [coll n]
@@ -2801,17 +2801,30 @@ reduces them without incurring seq initialization"
28012801
:added "1.11.10"}
28022802
[a] (Math/abs a))
28032803

2804+
(defn NaN?
2805+
"Returns true if num is NaN, else false"
2806+
[val]
2807+
(js/isNaN val))
2808+
28042809
(defn ^number max
28052810
"Returns the greatest of the nums."
28062811
([x] x)
2807-
([x y] (cljs.core/max x y))
2812+
([x y]
2813+
(cond
2814+
(NaN? x) x
2815+
(NaN? y) y
2816+
:else (cljs.core/max x y)))
28082817
([x y & more]
28092818
(reduce max (cljs.core/max x y) more)))
28102819

28112820
(defn ^number min
28122821
"Returns the least of the nums."
28132822
([x] x)
2814-
([x y] (cljs.core/min x y))
2823+
([x y]
2824+
(cond
2825+
(NaN? x) x
2826+
(NaN? y) y
2827+
:else (cljs.core/min x y)))
28152828
([x y & more]
28162829
(reduce min (cljs.core/min x y) more)))
28172830

@@ -6156,7 +6169,7 @@ reduces them without incurring seq initialization"
61566169
(let [v-pos (+ start n)]
61576170
(if (or (neg? n) (<= (inc end) v-pos))
61586171
(throw (js/Error. (str_ "Index " n " out of bounds [0," (-count coll) "]")))
6159-
(build-subvec meta (assoc v v-pos val) start (max end (inc v-pos)) nil))))
6172+
(build-subvec meta (assoc v v-pos val) start (unchecked-max end (inc v-pos)) nil))))
61606173

61616174
IReduce
61626175
(-reduce [coll f]
@@ -9924,7 +9937,7 @@ reduces them without incurring seq initialization"
99249937

99259938
IChunkedSeq
99269939
(-chunked-first [rng]
9927-
(IntegerRangeChunk. start step (min cnt 32)))
9940+
(IntegerRangeChunk. start step (unchecked-min cnt 32)))
99289941
(-chunked-rest [rng]
99299942
(if (<= cnt 32)
99309943
()
@@ -10326,7 +10339,7 @@ reduces them without incurring seq initialization"
1032610339
(cons match-vals
1032710340
(lazy-seq
1032810341
(let [post-idx (+ (.-index matches)
10329-
(max 1 (.-length match-str)))]
10342+
(unchecked-max 1 (.-length match-str)))]
1033010343
(when (<= post-idx (.-length s))
1033110344
(re-seq* re (subs s post-idx)))))))))
1033210345

@@ -12163,11 +12176,6 @@ reduces them without incurring seq initialization"
1216312176
[x]
1216412177
(instance? goog.Uri x))
1216512178

12166-
(defn NaN?
12167-
"Returns true if num is NaN, else false"
12168-
[val]
12169-
(js/isNaN val))
12170-
1217112179
(defn ^:private parsing-err
1217212180
"Construct message for parsing for non-string parsing error"
1217312181
[val]
@@ -12296,7 +12304,7 @@ reduces them without incurring seq initialization"
1229612304
-1
1229712305
(loop [idx (cond
1229812306
(pos? start) start
12299-
(neg? start) (max 0 (+ start len))
12307+
(neg? start) (unchecked-max 0 (+ start len))
1230012308
:else start)]
1230112309
(if (< idx len)
1230212310
(if (= (-nth coll idx) x)
@@ -12309,7 +12317,7 @@ reduces them without incurring seq initialization"
1230912317
(if (zero? len)
1231012318
-1
1231112319
(loop [idx (cond
12312-
(pos? start) (min (dec len) start)
12320+
(pos? start) (unchecked-min (dec len) start)
1231312321
(neg? start) (+ len start)
1231412322
:else start)]
1231512323
(if (>= idx 0)

src/main/clojure/cljs/core.cljc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,17 +1201,21 @@
12011201
(core/defmacro ^::ana/numeric neg? [x]
12021202
`(< ~x 0))
12031203

1204-
(core/defmacro ^::ana/numeric max
1204+
(core/defmacro ^::ana/numeric unchecked-max
12051205
([x] x)
1206-
([x y] `(let [x# ~x, y# ~y]
1207-
(~'js* "((~{} > ~{}) ? ~{} : ~{})" x# y# x# y#)))
1208-
([x y & more] `(max (max ~x ~y) ~@more)))
1206+
([x y]
1207+
`(let [x# ~x, y# ~y]
1208+
(if (> x# y#) x# y#)))
1209+
([x y & more]
1210+
`(max (max ~x ~y) ~@more)))
12091211

1210-
(core/defmacro ^::ana/numeric min
1212+
(core/defmacro ^::ana/numeric unchecked-min
12111213
([x] x)
1212-
([x y] `(let [x# ~x, y# ~y]
1213-
(~'js* "((~{} < ~{}) ? ~{} : ~{})" x# y# x# y#)))
1214-
([x y & more] `(min (min ~x ~y) ~@more)))
1214+
([x y]
1215+
`(let [x# ~x, y# ~y]
1216+
(if (< x# y#) x# y#)))
1217+
([x y & more]
1218+
`(min (min ~x ~y) ~@more)))
12151219

12161220
(core/defmacro ^::ana/numeric js-mod [num div]
12171221
(core/list 'js* "(~{} % ~{})" num div))

0 commit comments

Comments
 (0)