-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from kalmarek/float-interface-improvements
Float interface improvements - and some other things
- Loading branch information
Showing
19 changed files
with
492 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "Arblib" | ||
uuid = "fb37089c-8514-4489-9461-98f9c8763369" | ||
authors = ["Marek Kaluba <[email protected]>", "Sascha Timme <Sascha Timme <[email protected]>", "Joel Dahne <[email protected]>"] | ||
version = "1.0" | ||
version = "1.1" | ||
|
||
[deps] | ||
FLINT_jll = "e134572f-a0d5-539d-bddf-3cad8db41a82" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Conversion to float types in Base | ||
|
||
## Float64 | ||
Base.Float64(x::MagOrRef, r::RoundingMode = RoundUp) = Float64(x, convert(arb_rnd, r)) | ||
Base.Float64(x::ArfOrRef, r::RoundingMode) = Float64(x, convert(arb_rnd, r)) | ||
Base.Float64(x::ArbOrRef, r::RoundingMode = RoundNearest) = Float64(x, convert(arb_rnd, r)) | ||
|
||
function Base.Float64(x::MagOrRef, r::arb_rnd) | ||
r == ArbRoundUp || | ||
throw(ArgumentError("only supports rounding up when converting Mag to Float64")) | ||
return get(x) | ||
end | ||
Base.Float64(x::ArfOrRef, r::arb_rnd) = get_d(x, r) | ||
Base.Float64(x::ArbOrRef, r::arb_rnd) = Float64(midref(x), r) | ||
|
||
# Deprecated | ||
Base.Float64(x::ArfOrRef; rnd::arb_rnd = ArbRoundNearest) = Float64(x, rnd) | ||
|
||
## Float16 and Float32 | ||
Base.Float16(x::MagOrRef, r::RoundingMode = RoundUp) = Float16(Float64(x, r), r) | ||
Base.Float16(x::MagOrRef, r::arb_rnd) = Float16(x, convert(RoundingMode, r)) | ||
Base.Float32(x::MagOrRef, r::RoundingMode = RoundUp) = Float32(Float64(x, r), r) | ||
Base.Float32(x::MagOrRef, r::arb_rnd) = Float32(x, convert(RoundingMode, r)) | ||
Base.Float16(x::Union{ArfOrRef,ArbOrRef}, r::RoundingMode = RoundNearest) = | ||
Float16(Float64(x, r), r) | ||
Base.Float16(x::Union{ArfOrRef,ArbOrRef}, r::arb_rnd) = Float16(x, convert(RoundingMode, r)) | ||
Base.Float32(x::Union{ArfOrRef,ArbOrRef}, r::RoundingMode = RoundNearest) = | ||
Float32(Float64(x, r), r) | ||
Base.Float32(x::Union{ArfOrRef,ArbOrRef}, r::arb_rnd) = Float32(x, convert(RoundingMode, r)) | ||
|
||
## BigFloat | ||
|
||
# Note that this uses different default values than the constructors | ||
# in Base. It always defaults to RoundNearest and uses the precision | ||
# given by the input argument. This means that it doesn't depend on | ||
# the global rounding mode and precision. | ||
|
||
Base.BigFloat(x::Union{ArfOrRef,ArbOrRef}, r::RoundingMode; precision = Base.precision(x)) = | ||
BigFloat(x, convert(Base.MPFR.MPFRRoundingMode, r); precision) | ||
|
||
Base.BigFloat(x::Union{ArfOrRef,ArbOrRef}, r::arb_rnd; precision = Base.precision(x)) = | ||
BigFloat(x, convert(RoundingMode, r); precision) | ||
|
||
function Base.BigFloat( | ||
x::ArfOrRef, | ||
r::Base.MPFR.MPFRRoundingMode = Base.MPFR.RoundNearest; | ||
precision = Base.precision(x), | ||
) | ||
y = BigFloat(; precision) | ||
get!(y, x, r) | ||
return y | ||
end | ||
Base.BigFloat( | ||
x::ArbOrRef, | ||
r::Base.MPFR.MPFRRoundingMode = Base.MPFR.RoundNearest; | ||
precision = Base.precision(x), | ||
) = BigFloat(midref(x), r; precision) | ||
|
||
## Conversion to integers | ||
|
||
# IMPROVE: We currently don't support any rounding to integers. This | ||
# would maybe be nice to do. | ||
# IMPROVE: Is there any point in supporting conversion to other | ||
# integers? | ||
|
||
function Base.Int(x::ArfOrRef) | ||
is_int(x) || throw(InexactError(:Int, Int, x)) | ||
typemin(Int) <= x <= typemax(Int) || throw(InexactError(:Int, Int, x)) | ||
return get_si(x, ArbRoundNearest) | ||
end | ||
|
||
Base.Int(x::ArbOrRef) = is_int(x) ? Int(midref(x)) : throw(InexactError(:Int, Int, x)) | ||
|
||
Base.Int(x::AcbOrRef) = | ||
is_int(x) ? Int(midref(realref(x))) : throw(InexactError(:Int, Int, x)) | ||
|
||
function Base.BigInt(x::ArfOrRef) | ||
is_int(x) || throw(InexactError(:BigInt, BigInt, x)) | ||
n = fmpz_struct() | ||
ccall( | ||
@libflint(arf_get_fmpz), | ||
Cint, | ||
(Ref{fmpz_struct}, Ref{arf_struct}, Ref{arb_rnd}), | ||
n, | ||
x, | ||
ArbRoundNearest, | ||
) | ||
return BigInt(n) | ||
end | ||
|
||
Base.BigInt(x::ArbOrRef) = | ||
is_int(x) ? BigInt(midref(x)) : throw(InexactError(:BigInt, BigInt, x)) | ||
|
||
Base.BigInt(x::AcbOrRef) = | ||
is_int(x) ? BigInt(midref(realref(x))) : throw(InexactError(:BigInt, BigInt, x)) | ||
|
||
## Conversion to Complex | ||
|
||
# TODO: This currently allows construction of Complex{ArbRef}, which | ||
# we probably don't want. | ||
Base.Complex{T}(z::AcbOrRef) where {T} = Complex{T}(realref(z), imagref(z)) | ||
Base.Complex(z::AcbOrRef) = Complex{Arb}(z) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
83bbb16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
Release Notes:
This is a minor release with a few new features:
typemin
andtypemax
forMag
,Arf
andArb
.frexp
andldexp
forArf
andArb
.T == Float64
inradius
. This doesn't allocate and can be useful for example when heuristically choosing algorithm based on the radius. SinceFloat64
has a bounded exponent this will overflow (rounding up) for extremely small or large radius.T == Arf
ingetball
.log2
andlog10
forArb
andAcb
.Int
andBigInt
. The conversion toInt
would previously crash on overflow.load_string
, non-inplace version ofload_string!
.It also improves the handling of rounding arguments when converting to other float types. In particular it handles rounding arguments for conversion to
BigFloat
,Float64
,Float32
andFloat16
. ForArb
the conversion is done for the midpoint and the radius does therefore not play a role in the rounding. In the future it is possible that rounding forArb
will consider the full ball.83bbb16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/100393
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: