Skip to content

Commit 50e8587

Browse files
authored
improve printing and bump to 1.1.1 (#7)
* improve printing and bump to 1.1.1 * fix and format
1 parent 0b26089 commit 50e8587

File tree

4 files changed

+25
-52
lines changed

4 files changed

+25
-52
lines changed

Project.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
name = "DiagHamInterface"
22
uuid = "2f27495d-c82b-46d5-b81b-1bb9aa58c4d5"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
authors = ["Andreas Feuerpfeil <development@manybodylab.com>"]
55

66
[deps]
77
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
8-
Format = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
98
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
109
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1110
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
12-
ThreadSafeDicts = "4239201d-c60e-5e0a-9702-85d713665ba7"
1311

1412
[compat]
1513
DelimitedFiles = "1"
16-
Format = "1.3.7"
1714
Preferences = "1.5.0"
1815
Printf = "1"
1916
SparseArrays = "1"
20-
ThreadSafeDicts = "0.1.6"
2117
julia = "1.10"

src/DiagHamInterface.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ export write_to_txt
1212
export write_matrix_elements
1313

1414
using DelimitedFiles
15-
using Format
1615
using SparseArrays
1716
using Preferences
18-
using ThreadSafeDicts
1917

2018
include("utility/backup.jl")
2119
include("utility/diagham_path.jl")

src/utility/numbers.jl

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,26 @@
1-
# Maps the format string (e.g. "%.5f") to the compiled formatter Function.
2-
const LOCAL_FORMAT_CACHE = ThreadSafeDict{String, Function}()
3-
41
"""
5-
format_with_precision(x; atol=eps(float(T)), mode=:auto, maxdigits=20)
2+
format_with_precision(x; atol=0.0, maxdigits=typemax(Int))
63
7-
Format number `x` as a string with absolute precision `atol`.
8-
Mode `:auto` uses `%f` for `[1e-3, 1e6)`, else `%e`. Use `:f` or `:e` to force format.
4+
Format number `x` as a string with absolute precision `atol` and a maximum of
5+
`maxdigits` significant digits.
6+
Uses scientific notation for very small or large numbers.
97
"""
10-
function format_with_precision(x::T; atol = eps(float(T)), mode::Symbol = :auto, maxdigits::Int = 20) where {T <: Real}
8+
function format_with_precision(x::T; atol = 0.0, maxdigits::Int = typemax(Int)) where {T <: Real}
119
iszero(x) && return "0.0"
12-
absx = abs(x)
13-
abs_atol = abs(atol)
1410

15-
use_e = mode == :e || (mode == :auto && (absx < 1.0e-3 || absx >= 1.0e6))
11+
if iszero(atol)
12+
maxdigits == typemax(Int) && return string(x)
13+
return string(round(x, sigdigits = maxdigits))
14+
end
1615

17-
if use_e
18-
exp10 = floor(Int, log10(absx))
19-
log10_atol = log10(abs_atol)
20-
p = ceil(Int, exp10 - log10_atol)
21-
p = clamp(p, 0, maxdigits)
16+
mag_x = floor(Int, log10(abs(x)))
17+
mag_atol = floor(Int, log10(abs(atol)))
2218

23-
formatter = get_threadsafe_formatter("%.$(p)e")
24-
return formatter(x)
25-
else
26-
p = ceil(Int, -log10(abs_atol))
27-
p = clamp(p, 0, maxdigits)
19+
required_sigdigits = max(1, mag_x - mag_atol + 1)
20+
s = min(required_sigdigits, maxdigits)
2821

29-
formatter = get_threadsafe_formatter("%.$(p)f")
30-
return formatter(x)
31-
end
32-
end
33-
34-
function get_threadsafe_formatter(fmtstr::String)
35-
return get!(LOCAL_FORMAT_CACHE, fmtstr) do
36-
Format.generate_formatter(fmtstr)
37-
end
22+
rounded_x = round(x, sigdigits = s)
23+
return string(rounded_x)
3824
end
3925

4026
read_number(V::Number) = V
@@ -64,18 +50,18 @@ function _read_number_bracket(V::AbstractString)
6450
return ComplexF64(real, imag)
6551
end
6652

67-
function write_number(V::Number; atol::Real = eps(real(float(V))))
68-
return format_with_precision(V; atol = atol)
53+
function write_number(V::Number; kwargs...)
54+
return format_with_precision(V; kwargs...)
6955
end
7056

71-
function write_number(V::Complex; atol::Real = eps(real(float(V))))
72-
return string("(", format_with_precision(real(V); atol = atol), ",", format_with_precision(imag(V); atol = atol), ")")
57+
function write_number(V::Complex; kwargs...)
58+
return string("(", format_with_precision(real(V); kwargs...), ",", format_with_precision(imag(V); kwargs...), ")")
7359
end
7460

75-
function write_number_space(V::Number; atol::Real = eps(real(float(V))))
76-
return format_with_precision(V; atol = atol)
61+
function write_number_space(V::Number; kwargs...)
62+
return format_with_precision(V; kwargs...)
7763
end
7864

79-
function write_number_space(V::Complex; atol::Real = eps(real(float(V))))
80-
return "$(format_with_precision(real(V); atol = atol)) $(format_with_precision(imag(V); atol = atol))"
65+
function write_number_space(V::Complex; kwargs...)
66+
return "$(format_with_precision(real(V); kwargs...)) $(format_with_precision(imag(V); kwargs...))"
8167
end

test/test_basics.jl

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,7 @@ using Test
9797
# Test normal range numbers
9898
result = format_with_precision(1.5)
9999
@test !occursin("e", result) && !occursin("E", result)
100-
101-
# Test with forced mode
102-
result_e = format_with_precision(1.5; mode = :e)
103-
@test occursin("e", result_e) || occursin("E", result_e)
104-
105-
result_f = format_with_precision(1.5; mode = :f)
106-
@test !occursin("e", result_f) && !occursin("E", result_f)
107-
100+
108101
# Test negative numbers
109102
result_neg = format_with_precision(-1.5)
110103
@test startswith(result_neg, "-")

0 commit comments

Comments
 (0)