Skip to content

Commit

Permalink
Merge pull request #71 from baumgold/70-InexactError
Browse files Browse the repository at this point in the history
#70 fix bug preventing large floats from being displayed
  • Loading branch information
pfitzseb authored Mar 28, 2021
2 parents d9c48b2 + e76dd5c commit 8dbe19f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "TableView"
uuid = "40c74d1a-b44c-5b06-a7c1-6cbea58ea978"
version = "0.6.7"
version = "0.6.8"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
15 changes: 13 additions & 2 deletions src/TableView.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ using Observables: @map
export showtable

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

function __init__()
version = readchomp(joinpath(@__DIR__, "..", "ag-grid.version"))
Expand Down Expand Up @@ -259,6 +258,18 @@ function _showtable_async!(w, schema, names, types, rows, coldefs, tablelength,
onimport(w, handler)
end

function _is_javascript_safe(x::Integer)
min_safe_int = -(Int64(2)^53-1)
max_safe_int = Int64(2)^53-1
min_safe_int < x < max_safe_int
end

function _is_javascript_safe(x::AbstractFloat)
min_safe_float = -(Float64(2)^53-1)
max_safe_float = Float64(2)^53-1
min_safe_float < x < max_safe_float
end

# directly write JSON instead of allocating temporary dicts etc
function table2json(schema, rows, types; requested = nothing)
io = IOBuffer()
Expand All @@ -273,7 +284,7 @@ function table2json(schema, rows, types; requested = nothing)
columnwriter = JSON.Writer.CompactContext(io)
JSON.begin_object(columnwriter)
Tables.eachcolumn(schema, row) do val, ind, name
if val isa Real && isfinite(val) && -js_max_safe_int < trunc(Int128, val) < js_max_safe_int
if val isa Real && isfinite(val) && _is_javascript_safe(val)
JSON.show_pair(columnwriter, ser, name, val)
elseif val === nothing || val === missing
JSON.show_pair(columnwriter, ser, name, repr(val))
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ end
@test firstrow["b"] == "9007199254740992"
@test firstrow["c"] == "18014398509481984"
end
@testset "large floats" begin
rows = Tables.table([1.0e50 1.0e100])
names = [:a, :b]
types = [Float64, Float64]
json = TableView.table2json(Tables.Schema(names, types), rows, types)
firstrow = JSON.parse(json)[1]
@test firstrow["a"] == "1.0e50"
@test firstrow["b"] == "1.0e100"
end
@testset "normal array" begin
array = rand(10, 10)
@test showtable(array) isa WebIO.Scope
Expand Down

0 comments on commit 8dbe19f

Please sign in to comment.