-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e63e6a
commit d9a9cc4
Showing
9 changed files
with
112 additions
and
98 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
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 |
---|---|---|
|
@@ -4,7 +4,9 @@ export | |
BigFloat, | ||
get_bigfloat_precision, | ||
set_bigfloat_precision, | ||
with_bigfloat_precision | ||
with_bigfloat_precision, | ||
bigfloat_str, | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
simonbyrne
Author
Contributor
|
||
big_str | ||
|
||
import | ||
Base: (*), +, -, /, <, <=, ==, >, >=, ^, besselj, besselj0, besselj1, bessely, | ||
|
@@ -18,7 +20,7 @@ import | |
cosh, sinh, tanh, sech, csch, coth, acosh, asinh, atanh, atan2, | ||
serialize, deserialize, cbrt, typemax, typemin, unsafe_trunc, | ||
realmin, realmax, get_rounding, set_rounding, maxintfloat, widen, | ||
significand, frexp | ||
significand, frexp, tryparse | ||
|
||
import Base.Rounding: get_rounding_raw, set_rounding_raw | ||
|
||
|
@@ -77,14 +79,6 @@ function BigFloat(x::BigInt) | |
return z | ||
end | ||
|
||
function BigFloat(x::AbstractString, base::Int) | ||
z = BigFloat() | ||
err = ccall((:mpfr_set_str, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{UInt8}, Int32, Int32), &z, x, base, ROUNDING_MODE[end]) | ||
err == 0 || throw("incorrectly formatted number \"$x\"") | ||
return z | ||
end | ||
BigFloat(x::AbstractString) = BigFloat(x, 10) | ||
|
||
BigFloat(x::Integer) = BigFloat(BigInt(x)) | ||
|
||
BigFloat(x::Union(Bool,Int8,Int16,Int32)) = BigFloat(convert(Clong,x)) | ||
|
@@ -93,6 +87,24 @@ BigFloat(x::Union(UInt8,UInt16,UInt32)) = BigFloat(convert(Culong,x)) | |
BigFloat(x::Union(Float16,Float32)) = BigFloat(Float64(x)) | ||
BigFloat(x::Rational) = BigFloat(num(x)) / BigFloat(den(x)) | ||
|
||
function tryparse(::Type{BigFloat}, s::AbstractString, base::Int=0) | ||
z = BigFloat() | ||
err = ccall((:mpfr_set_str, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{UInt8}, Int32, Int32), &z, s, base, ROUNDING_MODE[end]) | ||
err == 0 ? Nullable(z) : Nullable{BigFloat}() | ||
end | ||
|
||
macro bigfloat_str(s) | ||
parse(BigFloat,s) | ||
end | ||
|
||
macro big_str(s) | ||
n = tryparse(BigInt,s) | ||
!isnull(n) && return get(n) | ||
n = tryparse(BigFloat,s) | ||
!isnull(n) && return get(n) | ||
throw(ArgumentError("invalid number format $(repr(s)) for BigInt or BigFloat")) | ||
end | ||
|
||
convert(::Type{Rational}, x::BigFloat) = convert(Rational{BigInt}, x) | ||
convert{S}(::Type{BigFloat}, x::Rational{S}) = BigFloat(x) # to resolve ambiguity | ||
convert(::Type{BigFloat}, x::Real) = BigFloat(x) | ||
|
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.
Do we really need this? When would you need to use
bigfloat"1.23"
instead ofbig"1.23"
?