Skip to content

ArbReal round() behavior is inconsistent due to intermediate ArbFloat conversion #84

@captnjameskirk

Description

@captnjameskirk

I was getting some unexpected results when rounding with ArbReal. After some digging, it looks like the problem is caused by this wrapper in rounding.jl (lines 41 and 42):

round(x::ArbReal{P}, ::RoundingMode{:Nearest}) where {P} =
    ArbReal(round(ArbFloat(x), RoundNearest))

The x argument is converted to ArbFloat first and then the ArbFloat implementation of round() is called. But this conversion is not always exact. Even a single math operation on the ArbReal can affect the accuracy of the conversion, which messes up the intent of rounding, especially at the 0.5 boundary.

julia> round(ArbReal("1.99999999995") * 10^10)
19999999999.0

julia> round(ArbFloat("1.99999999995") * 10^10)
20000000000.0

julia> ArbFloat(ArbReal("1.99999999995") * 10^10)
19999999999.49999999999999999999999999989902580413171

I'm not sure what the best solution would be other than separate versions of round() for ArbReal. An intermediate string() conversion in between works, but that adds too much overhead. For now, I'm just using a duplicate of the ArbFloat version specialized for ArbReal, but I only needed the Nearest rounding mode version and haven't looked at any of the others yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions