Skip to content

Commit

Permalink
[FileFormats.NL] Fix printing of large integers (#1659)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 11, 2021
1 parent 16eb3d6 commit 5d2ddbe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/FileFormats/NL/NL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,12 @@ function _process_constraint(
return
end

_str(x::Float64) = isinteger(x) ? string(round(Int, x)) : string(x)
function _str(x::Float64)
if isinteger(x) && (typemin(Int) <= x <= typemax(Int))
return string(round(Int, x))
end
return string(x)
end

_write_term(io, x::Float64, ::Any) = println(io, "n", _str(x))
_write_term(io, x::Int, ::Any) = println(io, "o", x)
Expand Down
8 changes: 8 additions & 0 deletions test/FileFormats/NL/NL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,14 @@ function test_moi()
return
end

function test_float_rounding()
@test NL._str(1.0) == "1"
@test NL._str(1.2) == "1.2"
@test NL._str(1e50) == "1.0e50"
@test NL._str(-1e50) == "-1.0e50"
return
end

function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
Expand Down

0 comments on commit 5d2ddbe

Please sign in to comment.