Skip to content

Commit

Permalink
make measurements work with at-printf (#102)
Browse files Browse the repository at this point in the history
* make measurements work with at-printf

* move 'import Printf' to src/show.jl
  • Loading branch information
ranocha authored Apr 9, 2021
1 parent e71f520 commit 500ff82
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "2.5.0"
[deps]
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

Expand Down
6 changes: 6 additions & 0 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#
### Code:

import Printf

function truncated_print(io::IO, m::Measurement, error_digits::Int;
atbeg = "", atend = "", pm = "±")
val = if iszero(m.err) || !isfinite(m.err)
Expand Down Expand Up @@ -88,6 +90,10 @@ function Base.alignment(io::IO, measure::Measurement)
(length(m.captures[1]), length(m.captures[2]))
end

if VERSION >= v"1.6.0-rc1"
Printf.tofloat(measure::Measurement) = Printf.tofloat(measure.val)
end

### Juno pretty printing
@require Juno="e5e0dc1b-0480-54bc-9374-aad01c23163d" begin
Juno.render(i::Juno.Inline, measure::Measurement) =
Expand Down
13 changes: 11 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Measurements, SpecialFunctions, QuadGK, Calculus
using Test, LinearAlgebra, Statistics, Unitful
using Test, LinearAlgebra, Statistics, Unitful, Printf

import Base: isapprox
import Measurements: value, uncertainty
Expand Down Expand Up @@ -628,6 +628,15 @@ end
# the same number (note that the tag will be different, but that's not important here).
for a in (w, x, y); @test eval(Meta.parse(repr(a))) == a; end
end
if VERSION >= v"1.6.0-rc1"
@testset "@printf" begin
for T in (Float16, Float32, Float64, BigFloat)
m1 = measurement(one(T))
@test_nowarn @printf("Testing @printf: %.2e\n", m1)
@test @sprintf("Testing @sprintf: %.2e\n", m1) == "Testing @sprintf: 1.00e+00\n"
end
end
end
end

@testset "sum" begin
Expand Down Expand Up @@ -851,7 +860,7 @@ end
@test m .* (1:2:6) isa StepRangeLen
@test (1:.1:6) .- m isa StepRangeLen
@test m ./ (1:6.0) isa Vector{<:Measurement}

# JuliaLang/julia#30944
@test range(0±0, step=1±.1, length=10) isa StepRangeLen
end

3 comments on commit 500ff82

@ranocha
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@giordano Would you mind tagging a new release of Measurements.jl?

@giordano
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ranocha
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot 👍

Please sign in to comment.