Open
Description
The implementation of setprecision(::Type{BigFloat}, precision::Integer; base::Integer=2)
checks that precision
and base
has valid values. More precisely it does
function setprecision(::Type{BigFloat}, precision::Integer; base::Integer=2)
base > 1 || throw(DomainError(base, "`base` cannot be less than 2."))
precision > 0 || throw(DomainError(precision, "`precision` cannot be less than 1."))
DEFAULT_PRECISION[] = _convert_precision_from_base(precision, base)
return precision
end
The version taking a function as argument is missing this check, it is currently implemented as
function setprecision(f::Function, ::Type{BigFloat}, prec::Integer; base::Integer=2)
Base.ScopedValues.@with(CURRENT_PRECISION => _convert_precision_from_base(prec, base), f())
end
As a consequence we have e.g.
julia> setprecision(BigFloat, -1)
ERROR: DomainError with -1:
`precision` cannot be less than 1.
Stacktrace:
[1] setprecision(::Type{BigFloat}, precision::Int64; base::Int64)
@ Base.MPFR ./mpfr.jl:1067
[2] setprecision(::Type{BigFloat}, precision::Int64)
@ Base.MPFR ./mpfr.jl:1065
[3] top-level scope
@ REPL[15]:1
julia> setprecision(BigFloat, -1) do
"No error!"
end
"No error!"
The issue was introduced in #51362 when setprecision
was changed to use ScopedValues
. At the moment it seems like the missing check cannot actually lead to any issues because the constructor for BigFloat
explicitly checks that precision >= 2
. It does however still seem reasonable to include the check.
julia> versioninfo()
Julia Version 1.12.0-beta3
Commit faca79b503a (2025-05-12 06:47 UTC)
Build Info:
Official https://julialang.org release
Platform Info:
OS: Linux (x86_64-linux-gnu)
CPU: 24 × AMD Ryzen 9 5900X 12-Core Processor
WORD_SIZE: 64
LLVM: libLLVM-18.1.7 (ORCJIT, znver3)
GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 24 virtual cores)