Skip to content

Commit 8b00d7d

Browse files
author
Ben Baumgold
committed
#70 fix bug preventing large floats from being displayed
1 parent d9c48b2 commit 8b00d7d

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "TableView"
22
uuid = "40c74d1a-b44c-5b06-a7c1-6cbea58ea978"
3-
version = "0.6.7"
3+
version = "0.6.8"
44

55
[deps]
66
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/TableView.jl

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ using Observables: @map
77
export showtable
88

99
const ag_grid_imports = []
10-
const js_max_safe_int = Int128(2^53-1)
1110

1211
function __init__()
1312
version = readchomp(joinpath(@__DIR__, "..", "ag-grid.version"))
@@ -259,6 +258,14 @@ function _showtable_async!(w, schema, names, types, rows, coldefs, tablelength,
259258
onimport(w, handler)
260259
end
261260

261+
function _is_javascript_safe(x::Integer)
262+
-Int128(2^53)-1 < x < Int128(2^53)-1
263+
end
264+
265+
function _is_javascript_safe(x::AbstractFloat)
266+
-2^53-1 < x < 2^53-1
267+
end
268+
262269
# directly write JSON instead of allocating temporary dicts etc
263270
function table2json(schema, rows, types; requested = nothing)
264271
io = IOBuffer()
@@ -273,7 +280,7 @@ function table2json(schema, rows, types; requested = nothing)
273280
columnwriter = JSON.Writer.CompactContext(io)
274281
JSON.begin_object(columnwriter)
275282
Tables.eachcolumn(schema, row) do val, ind, name
276-
if val isa Real && isfinite(val) && -js_max_safe_int < trunc(Int128, val) < js_max_safe_int
283+
if val isa Real && isfinite(val) && _is_javascript_safe(val)
277284
JSON.show_pair(columnwriter, ser, name, val)
278285
elseif val === nothing || val === missing
279286
JSON.show_pair(columnwriter, ser, name, repr(val))

test/runtests.jl

+9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ end
4242
@test firstrow["b"] == "9007199254740992"
4343
@test firstrow["c"] == "18014398509481984"
4444
end
45+
@testset "large floats" begin
46+
rows = Tables.table([1.0e50 1.0e100])
47+
names = [:a, :b]
48+
types = [Float64, Float64]
49+
json = TableView.table2json(Tables.Schema(names, types), rows, types)
50+
firstrow = JSON.parse(json)[1]
51+
@test firstrow["a"] == "1.0e50"
52+
@test firstrow["b"] == "1.0e100"
53+
end
4554
@testset "normal array" begin
4655
array = rand(10, 10)
4756
@test showtable(array) isa WebIO.Scope

0 commit comments

Comments
 (0)