Skip to content

Commit 6f8f87f

Browse files
committed
get_precision with multiple arguments
1 parent afc2889 commit 6f8f87f

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ This page describes the most important changes in `TypeUtils`. The format is bas
44
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to
55
[Semantic Versioning](https://semver.org).
66

7+
## Unreleased
8+
9+
### Added
10+
11+
- With multiple arguments, `get_precision(x, y...)` yields the best precision of all the
12+
given arguments.
13+
14+
715
## Version 1.9.0 (2025-06-20)
816

917
This new minor version adds methods to deal with the numerical precision of numbers and

src/precision.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ get_precision(::Type{<:Factorization{T}}) where {T} = get_precision(T)
4343
return r
4444
end
4545

46+
"""
47+
get_precision(x, y, z...) -> T<:AbstractFloat
48+
49+
yields the best precision of all given arguments `x`, `y`, and `z...`.
50+
51+
"""
52+
@inline get_precision(x, y, z...) = get_precision(get_precision(x, y), z...)
53+
get_precision(x, y) =
54+
get_precision(get_precision(x)::Type{<:AbstractFloat},
55+
get_precision(y)::Type{<:AbstractFloat})::Type{<:AbstractFloat}
56+
57+
get_precision(::Type{AbstractFloat}, ::Type{AbstractFloat}) = AbstractFloat
58+
get_precision(::Type{T}, ::Type{AbstractFloat}) where {T<:AbstractFloat} = T
59+
get_precision(::Type{AbstractFloat}, ::Type{T}) where {T<:AbstractFloat} = T
60+
get_precision(::Type{S}, ::Type{T}) where {S<:AbstractFloat,T<:AbstractFloat} =
61+
promote_type(S, T)::Type{<:AbstractFloat}
62+
4663
"""
4764
adapt_precision(T::Type{<:AbstractFloat}, x) -> y
4865

test/runtests.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,22 @@ same_value_and_type(x::T, y::T) where {T} = (x === y) || (x == y)
921921
@test @inferred(get_precision(B)) == Float32
922922
@test @inferred(get_precision(typeof(B))) == Float32
923923

924+
# Multiple arguments.
925+
@test @inferred(get_precision(Int8, Int)) == AbstractFloat
926+
@test @inferred(get_precision(Int8, Int, BigFloat)) == BigFloat
927+
@test @inferred(get_precision(String, Float16)) == Float16
928+
@test @inferred(get_precision(:hello, Float16)) == Float16
929+
@test @inferred(get_precision(:hello, zero(Float16))) == Float16
930+
@test @inferred(get_precision(Symbol, zero(Float16))) == Float16
931+
@test @inferred(get_precision(Float16, String)) == Float16
932+
@test @inferred(get_precision(Float16, :hello)) == Float16
933+
@test @inferred(get_precision(zero(Float16), :hello)) == Float16
934+
@test @inferred(get_precision(zero(Float16), Symbol)) == Float16
935+
@test @inferred(get_precision(Float32, Int)) == Float32
936+
@test @inferred(get_precision(Float32, Int, pi)) == Float32
937+
@test @inferred(get_precision(Float32, Int, pi, 1.0)) == Float64
938+
@test @inferred(get_precision(Float32, Int, pi, 1.0)) == Float64
939+
@test @inferred(get_precision(Float32, Int, pi, big(1.0))) == BigFloat
924940
end
925941

926942
@testset "adapt_precision" begin

0 commit comments

Comments
 (0)