From 63ca6bf3f0f7db2de36cf88c1d86441b483a6961 Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Sat, 7 Mar 2026 18:52:01 -0500 Subject: [PATCH 1/2] improve printing and bump to 1.1.1 --- Project.toml | 6 +--- src/DiagHamInterface.jl | 2 -- src/utility/numbers.jl | 66 ++++++++++++++++------------------------- 3 files changed, 27 insertions(+), 47 deletions(-) diff --git a/Project.toml b/Project.toml index cffc511..c721a55 100644 --- a/Project.toml +++ b/Project.toml @@ -1,21 +1,17 @@ name = "DiagHamInterface" uuid = "2f27495d-c82b-46d5-b81b-1bb9aa58c4d5" -version = "1.1.0" +version = "1.1.1" authors = ["Andreas Feuerpfeil "] [deps] DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" -Format = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8" Preferences = "21216c6a-2e73-6563-6e65-726566657250" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" -ThreadSafeDicts = "4239201d-c60e-5e0a-9702-85d713665ba7" [compat] DelimitedFiles = "1" -Format = "1.3.7" Preferences = "1.5.0" Printf = "1" SparseArrays = "1" -ThreadSafeDicts = "0.1.6" julia = "1.10" diff --git a/src/DiagHamInterface.jl b/src/DiagHamInterface.jl index 6398097..33ab05f 100644 --- a/src/DiagHamInterface.jl +++ b/src/DiagHamInterface.jl @@ -12,10 +12,8 @@ export write_to_txt export write_matrix_elements using DelimitedFiles -using Format using SparseArrays using Preferences -using ThreadSafeDicts include("utility/backup.jl") include("utility/diagham_path.jl") diff --git a/src/utility/numbers.jl b/src/utility/numbers.jl index 1de0b90..0e3943f 100644 --- a/src/utility/numbers.jl +++ b/src/utility/numbers.jl @@ -1,40 +1,26 @@ -# Maps the format string (e.g. "%.5f") to the compiled formatter Function. -const LOCAL_FORMAT_CACHE = ThreadSafeDict{String, Function}() - """ - format_with_precision(x; atol=eps(float(T)), mode=:auto, maxdigits=20) + format_with_precision(x; atol=0.0, maxdigits=typemax(Int)) -Format number `x` as a string with absolute precision `atol`. -Mode `:auto` uses `%f` for `[1e-3, 1e6)`, else `%e`. Use `:f` or `:e` to force format. +Format number `x` as a string with absolute precision `atol` and a maximum of +`maxdigits` significant digits. +Uses scientific notation for very small or large numbers. """ -function format_with_precision(x::T; atol = eps(float(T)), mode::Symbol = :auto, maxdigits::Int = 20) where {T <: Real} +function format_with_precision(x::T; atol = 0.0, maxdigits::Int = typemax(Int)) where {T <: Real} iszero(x) && return "0.0" - absx = abs(x) - abs_atol = abs(atol) - - use_e = mode == :e || (mode == :auto && (absx < 1.0e-3 || absx >= 1.0e6)) - - if use_e - exp10 = floor(Int, log10(absx)) - log10_atol = log10(abs_atol) - p = ceil(Int, exp10 - log10_atol) - p = clamp(p, 0, maxdigits) - - formatter = get_threadsafe_formatter("%.$(p)e") - return formatter(x) - else - p = ceil(Int, -log10(abs_atol)) - p = clamp(p, 0, maxdigits) - - formatter = get_threadsafe_formatter("%.$(p)f") - return formatter(x) - end -end - -function get_threadsafe_formatter(fmtstr::String) - return get!(LOCAL_FORMAT_CACHE, fmtstr) do - Format.generate_formatter(fmtstr) + + if iszero(atol) + maxdigits == typemax(Int) && return string(x) + return string(round(x, sigdigits=maxdigits)) end + + mag_x = floor(Int, log10(abs(x))) + mag_atol = floor(Int, log10(abs(atol))) + + required_sigdigits = max(1, mag_x - mag_atol + 1) + s = min(required_sigdigits, maxdigits) + + rounded_x = round(x, sigdigits=s) + return string(rounded_x) end read_number(V::Number) = V @@ -64,18 +50,18 @@ function _read_number_bracket(V::AbstractString) return ComplexF64(real, imag) end -function write_number(V::Number; atol::Real = eps(real(float(V)))) - return format_with_precision(V; atol = atol) +function write_number(V::Number; kwargs...) + return format_with_precision(V; kwargs...) end -function write_number(V::Complex; atol::Real = eps(real(float(V)))) - return string("(", format_with_precision(real(V); atol = atol), ",", format_with_precision(imag(V); atol = atol), ")") +function write_number(V::Complex; kwargs...) + return string("(", format_with_precision(real(V); kwargs...), ",", format_with_precision(imag(V); kwargs...), ")") end -function write_number_space(V::Number; atol::Real = eps(real(float(V)))) - return format_with_precision(V; atol = atol) +function write_number_space(V::Number; kwargs...) + return format_with_precision(V; kwargs...) end -function write_number_space(V::Complex; atol::Real = eps(real(float(V)))) - return "$(format_with_precision(real(V); atol = atol)) $(format_with_precision(imag(V); atol = atol))" +function write_number_space(V::Complex; kwargs...) + return "$(format_with_precision(real(V); kwargs...)) $(format_with_precision(imag(V); kwargs...))" end From 38eaca60313068e334460ce9ababe3a098300015 Mon Sep 17 00:00:00 2001 From: AFeuerpfeil Date: Sat, 7 Mar 2026 18:56:20 -0500 Subject: [PATCH 2/2] fix and format --- src/utility/numbers.jl | 12 ++++++------ test/test_basics.jl | 9 +-------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/utility/numbers.jl b/src/utility/numbers.jl index 0e3943f..4907cff 100644 --- a/src/utility/numbers.jl +++ b/src/utility/numbers.jl @@ -7,19 +7,19 @@ Uses scientific notation for very small or large numbers. """ function format_with_precision(x::T; atol = 0.0, maxdigits::Int = typemax(Int)) where {T <: Real} iszero(x) && return "0.0" - + if iszero(atol) maxdigits == typemax(Int) && return string(x) - return string(round(x, sigdigits=maxdigits)) + return string(round(x, sigdigits = maxdigits)) end - + mag_x = floor(Int, log10(abs(x))) mag_atol = floor(Int, log10(abs(atol))) - + required_sigdigits = max(1, mag_x - mag_atol + 1) s = min(required_sigdigits, maxdigits) - - rounded_x = round(x, sigdigits=s) + + rounded_x = round(x, sigdigits = s) return string(rounded_x) end diff --git a/test/test_basics.jl b/test/test_basics.jl index 5af2691..a8cd196 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -97,14 +97,7 @@ using Test # Test normal range numbers result = format_with_precision(1.5) @test !occursin("e", result) && !occursin("E", result) - - # Test with forced mode - result_e = format_with_precision(1.5; mode = :e) - @test occursin("e", result_e) || occursin("E", result_e) - - result_f = format_with_precision(1.5; mode = :f) - @test !occursin("e", result_f) && !occursin("E", result_f) - + # Test negative numbers result_neg = format_with_precision(-1.5) @test startswith(result_neg, "-")