Skip to content

Commit

Permalink
Fix rendering of number whose magnitude is greater than 2^53 (#727)
Browse files Browse the repository at this point in the history
Only numbers with magnitude no greater than 2^53 are precisely representable in JavaScript Number type.
  • Loading branch information
krzsztf authored Oct 29, 2024
1 parent 748f73e commit f7d7908
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,9 @@
:render-fn 'nextjournal.clerk.render/render-number
#?@(:clj [:transform-fn (update-val #(cond-> %
(or (instance? clojure.lang.Ratio %)
(instance? clojure.lang.BigInt %)) pr-str))])})
(instance? clojure.lang.BigInt %)
(> % 9007199254740992)
(< % -9007199254740992)) pr-str))])})

(def number-hex-viewer
{:name `number-hex-viewer :render-fn '(fn [num] (nextjournal.clerk.render/render-number (str "0x" (.toString (js/Number. num) 16))))})
Expand Down
6 changes: 5 additions & 1 deletion test/nextjournal/clerk/viewer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@
(is (match? {:nextjournal/value "1142497398145249635243N"}
(v/present 1142497398145249635243N)))
(is (match? {:nextjournal/value "10/33"}
(v/present 10/33))))
(v/present 10/33)))
(is (match? {:nextjournal/value "9007199254740993"}
(v/present 9007199254740993)))
(is (match? {:nextjournal/value "-9007199254740993"}
(v/present -9007199254740993))))

(testing "opts are not propagated to children during presentation"
(let [count-opts (fn [o]
Expand Down

0 comments on commit f7d7908

Please sign in to comment.