Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Rational -> BigFloat rounding #30189

Merged
merged 1 commit into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ rounding_raw(::Type{BigFloat}) = ROUNDING_MODE[]
setrounding_raw(::Type{BigFloat}, r::MPFRRoundingMode) = ROUNDING_MODE[]=r

rounding(::Type{BigFloat}) = convert(RoundingMode, rounding_raw(BigFloat))
setrounding(::Type{BigFloat},r::RoundingMode) = setrounding_raw(BigFloat, convert(MPFRRoundingMode, r))
setrounding(::Type{BigFloat}, r::RoundingMode) = setrounding_raw(BigFloat, convert(MPFRRoundingMode, r))


# overload the definition of unsafe_convert to ensure that `x.d` is assigned
Expand Down Expand Up @@ -224,8 +224,13 @@ BigFloat(x::Union{UInt8,UInt16,UInt32}, r::MPFRRoundingMode=ROUNDING_MODE[]; pre
BigFloat(x::Union{Float16,Float32}, r::MPFRRoundingMode=ROUNDING_MODE[]; precision::Integer=DEFAULT_PRECISION[]) =
BigFloat(Float64(x), r; precision=precision)

BigFloat(x::Rational, r::MPFRRoundingMode=ROUNDING_MODE[]; precision::Integer=DEFAULT_PRECISION[]) =
BigFloat(numerator(x), r; precision=precision) / BigFloat(denominator(x), r ;precision=precision)
function BigFloat(x::Rational, r::MPFRRoundingMode=ROUNDING_MODE[]; precision::Integer=DEFAULT_PRECISION[])
setprecision(BigFloat, precision) do
setrounding_raw(BigFloat, r) do
BigFloat(numerator(x)) / BigFloat(denominator(x))
end
end
end

function tryparse(::Type{BigFloat}, s::AbstractString; base::Integer=0, precision::Integer=DEFAULT_PRECISION[], rounding::MPFRRoundingMode=ROUNDING_MODE[])
!isempty(s) && isspace(s[end]) && return tryparse(BigFloat, rstrip(s), base = base)
Expand Down
9 changes: 9 additions & 0 deletions base/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ function setrounding(f::Function, ::Type{T}, rounding::RoundingMode) where T
setrounding_raw(T,old_rounding_raw)
end
end
function setrounding_raw(f::Function, ::Type{T}, rounding) where T
old_rounding_raw = rounding_raw(T)
setrounding_raw(T,rounding)
try
return f()
finally
setrounding_raw(T,old_rounding_raw)
end
end


# Should be equivalent to:
Expand Down
2 changes: 1 addition & 1 deletion test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ end
# PR 17217 -- BigFloat constructors with given precision and rounding mode
# test constructors and `big` with additional precision and rounding mode:
for prec in (10, 100, 1000)
for val in ("3.1", pi, "-1.3", 3.1)
for val in ("3.1", pi, "-1.3", 3.1, 1//10)
let a = BigFloat(val),
b = BigFloat(val, prec),
c = BigFloat(val, RoundUp),
Expand Down