Skip to content

Commit 00d983a

Browse files
committed
Merge pull request #10955 from JuliaLang/sb/big-parse
use parse for BigFloat/BigInt
2 parents b47d3d0 + 70cee6f commit 00d983a

12 files changed

+112
-106
lines changed

base/deprecated.jl

+3
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,6 @@ export float32_isvalid, float64_isvalid
529529
@deprecate parseint(T::Type, s, base) parse(T, s, base)
530530

531531
@deprecate linrange linspace
532+
533+
@deprecate BigFloat(s::AbstractString) parse(BigFloat,s)
534+
@deprecate BigInt(s::AbstractString) parse(BigInt,s)

base/exports.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ export
13241324
@__FILE__,
13251325
@int128_str,
13261326
@uint128_str,
1327-
@bigint_str,
1327+
@big_str,
13281328
@mstr, # triple-quoted strings
13291329
@cmd, # `commands`
13301330

base/gmp.jl

-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ widen(::Type{BigInt}) = BigInt
7474
signed(x::BigInt) = x
7575

7676
BigInt(x::BigInt) = x
77-
BigInt(s::AbstractString) = parse(BigInt,s)
7877

7978
function tryparse_internal(::Type{BigInt}, s::AbstractString, startpos::Int, endpos::Int, base::Int, raise::Bool)
8079
_n = Nullable{BigInt}()

base/int.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,12 @@ macro uint128_str(s)
254254
parse(UInt128,s)
255255
end
256256

257-
macro bigint_str(s)
258-
parse(BigInt,s)
257+
macro big_str(s)
258+
n = tryparse(BigInt,s)
259+
!isnull(n) && return get(n)
260+
n = tryparse(BigFloat,s)
261+
!isnull(n) && return get(n)
262+
throw(ArgumentError("invalid number format $(repr(s)) for BigInt or BigFloat"))
259263
end
260264

261265
## system word size ##

base/mpfr.jl

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ export
44
BigFloat,
55
get_bigfloat_precision,
66
set_bigfloat_precision,
7-
with_bigfloat_precision
7+
with_bigfloat_precision,
8+
bigfloat_str,
9+
big_str
810

911
import
1012
Base: (*), +, -, /, <, <=, ==, >, >=, ^, besselj, besselj0, besselj1, bessely,
@@ -18,7 +20,7 @@ import
1820
cosh, sinh, tanh, sech, csch, coth, acosh, asinh, atanh, atan2,
1921
serialize, deserialize, cbrt, typemax, typemin, unsafe_trunc,
2022
realmin, realmax, get_rounding, set_rounding, maxintfloat, widen,
21-
significand, frexp
23+
significand, frexp, tryparse
2224

2325
import Base.Rounding: get_rounding_raw, set_rounding_raw
2426

@@ -77,14 +79,6 @@ function BigFloat(x::BigInt)
7779
return z
7880
end
7981

80-
function BigFloat(x::AbstractString, base::Int)
81-
z = BigFloat()
82-
err = ccall((:mpfr_set_str, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{UInt8}, Int32, Int32), &z, x, base, ROUNDING_MODE[end])
83-
err == 0 || throw("incorrectly formatted number \"$x\"")
84-
return z
85-
end
86-
BigFloat(x::AbstractString) = BigFloat(x, 10)
87-
8882
BigFloat(x::Integer) = BigFloat(BigInt(x))
8983

9084
BigFloat(x::Union(Bool,Int8,Int16,Int32)) = BigFloat(convert(Clong,x))
@@ -93,6 +87,12 @@ BigFloat(x::Union(UInt8,UInt16,UInt32)) = BigFloat(convert(Culong,x))
9387
BigFloat(x::Union(Float16,Float32)) = BigFloat(Float64(x))
9488
BigFloat(x::Rational) = BigFloat(num(x)) / BigFloat(den(x))
9589

90+
function tryparse(::Type{BigFloat}, s::AbstractString, base::Int=0)
91+
z = BigFloat()
92+
err = ccall((:mpfr_set_str, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{UInt8}, Int32, Int32), &z, s, base, ROUNDING_MODE[end])
93+
err == 0 ? Nullable(z) : Nullable{BigFloat}()
94+
end
95+
9696
convert(::Type{Rational}, x::BigFloat) = convert(Rational{BigInt}, x)
9797
convert{S}(::Type{BigFloat}, x::Rational{S}) = BigFloat(x) # to resolve ambiguity
9898
convert(::Type{BigFloat}, x::Real) = BigFloat(x)

base/string.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ tryparse(::Type{Float64}, s::SubString) = ccall(:jl_try_substrtod, Nullable{Floa
16301630
tryparse(::Type{Float32}, s::AbstractString) = ccall(:jl_try_strtof, Nullable{Float32}, (Ptr{UInt8},), s)
16311631
tryparse(::Type{Float32}, s::SubString) = ccall(:jl_try_substrtof, Nullable{Float32}, (Ptr{UInt8},Csize_t,Cint), s.string, s.offset, s.endof)
16321632

1633-
function parse{T<:Union(Float32,Float64)}(::Type{T}, s::AbstractString)
1633+
function parse{T<:FloatingPoint}(::Type{T}, s::AbstractString)
16341634
nf = tryparse(T, s)
16351635
isnull(nf) ? throw(ArgumentError("invalid number format $(repr(s)) for $T")) : get(nf)
16361636
end

src/julia-parser.scm

+5-5
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@
325325
`(macrocall @int128_str ,s)
326326
n))
327327
((within-int128? s) `(macrocall @int128_str ,s))
328-
(else `(macrocall @bigint_str ,s))))))
328+
(else `(macrocall @big_str ,s))))))
329329

330330
(define (fix-uint-neg neg n)
331331
(if neg
@@ -342,7 +342,7 @@
342342
((<= l 32) (uint32 n))
343343
((<= l 64) (uint64 n))
344344
((<= l 128) `(macrocall @uint128_str ,s))
345-
(else `(macrocall @bigint_str ,s)))))
345+
(else `(macrocall @big_str ,s)))))
346346

347347
(define (sized-uint-oct-literal n s)
348348
(if (string.find s "o0")
@@ -354,7 +354,7 @@
354354
(else (uint64 n)))
355355
(if (oct-within-uint128? s)
356356
`(macrocall @uint128_str ,s)
357-
`(macrocall @bigint_str ,s)))))
357+
`(macrocall @big_str ,s)))))
358358

359359
(define (strip-leading-0s s)
360360
(define (loop i)
@@ -386,7 +386,7 @@
386386
(define (large-number? t)
387387
(and (pair? t)
388388
(eq? (car t) 'macrocall)
389-
(memq (cadr t) '(@int128_str @uint128_str @bigint_str))))
389+
(memq (cadr t) '(@int128_str @uint128_str @big_str))))
390390

391391
; skip to end of comment, starting at #: either #...<eol> or #= .... =#.
392392
(define (skip-comment port)
@@ -797,7 +797,7 @@
797797
(if (eq? op '-)
798798
(if (large-number? num)
799799
(if (eqv? (caddr num) "-170141183460469231731687303715884105728")
800-
`(macrocall @bigint_str "170141183460469231731687303715884105728")
800+
`(macrocall @big_str "170141183460469231731687303715884105728")
801801
`(,(car num) ,(cadr num) ,(string.tail (caddr num) 1)))
802802
(if (= num -9223372036854775808)
803803
`(macrocall @int128_str "9223372036854775808")

test/bigint.jl

+46-46
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
a = BigInt("123456789012345678901234567890")
2-
b = BigInt("123456789012345678901234567891")
1+
a = parse(BigInt,"123456789012345678901234567890")
2+
b = parse(BigInt,"123456789012345678901234567891")
33

44
@test typeof(a+1) == BigInt
55
@test a+1 == b
@@ -11,20 +11,20 @@ b = BigInt("123456789012345678901234567891")
1111
@test !(b < a)
1212
@test !(b <= a)
1313

14-
c = BigInt("246913578024691357802469135780")
14+
c = parse(BigInt,"246913578024691357802469135780")
1515
@test typeof(a * 2) == BigInt
1616
@test a*2 == c
1717
@test c-a == a
1818
@test c == a + a
1919
@test c+1 == a+b
2020

21-
d = BigInt("-246913578024691357802469135780")
21+
d = parse(BigInt,"-246913578024691357802469135780")
2222
@test typeof(d) == BigInt
2323
@test d == -c
2424

2525
ee = typemax(Int64)
2626
@test typeof(BigInt(ee)) == BigInt
27-
@test BigInt(ee)+1 == BigInt("9223372036854775808")
27+
@test BigInt(ee)+1 == parse(BigInt,"9223372036854775808")
2828

2929
#Multiple calls for sanity check, since we're doing direct memory manipulation
3030
@test string(a) == "123456789012345678901234567890"
@@ -163,58 +163,58 @@ end
163163
@test gcd(BigInt(48), BigInt(180)) == 12
164164
@test lcm(BigInt(48), BigInt(180)) == 720
165165

166-
@test factorial(BigInt(40)) == BigInt("815915283247897734345611269596115894272000000000")
166+
@test factorial(BigInt(40)) == parse(BigInt,"815915283247897734345611269596115894272000000000")
167167
@test binomial(BigInt(1), -1) == BigInt(0)
168168
@test binomial(BigInt(1), 2) == BigInt(0)
169-
@test binomial(BigInt(-53), 42) == BigInt("959509335087854414441273718")
170-
@test binomial(BigInt(113), BigInt(42)) == BigInt("18672199984318438125634054194360")
169+
@test binomial(BigInt(-53), 42) == parse(BigInt,"959509335087854414441273718")
170+
@test binomial(BigInt(113), BigInt(42)) == parse(BigInt,"18672199984318438125634054194360")
171171

172172
a = rand(1:100, 10000)
173173
b = map(BigInt, a)
174174
@test sum(a) == sum(b)
175175

176176
# Iterated arithmetic
177-
a = BigInt("315135")
178-
b = BigInt("12412")
179-
c = BigInt("3426495623485904783478347")
180-
d = BigInt("-1398984130")
181-
f = BigInt("2413804710837418037418307081437315263635345357386985747464")
182-
g = BigInt("-1")
183-
184-
@test +(a, b) == BigInt("327547")
185-
@test +(a, b, c) == BigInt("3426495623485904783805894")
186-
@test +(a, b, c, d) == BigInt("3426495623485903384821764")
187-
@test +(a, b, c, d, f) == BigInt("2413804710837418037418307081437318690130968843290370569228")
188-
@test +(a, b, c, d, f, g) == BigInt("2413804710837418037418307081437318690130968843290370569227")
189-
190-
@test *(a, b) == BigInt("3911455620")
191-
@test *(a, b, c) == BigInt("13402585563389346256121263521460140")
192-
@test *(a, b, c, d) == BigInt("-18750004504148804423388563022070650287578200")
193-
@test *(a, b, c, d, f) == BigInt("-45258849200337190631492857400003938881995610529251881450243326128168934937055005474972396281351684800")
194-
@test *(a, b, c, d, f, g) == BigInt("45258849200337190631492857400003938881995610529251881450243326128168934937055005474972396281351684800")
195-
196-
@test ($)(a, b) == BigInt("327299")
197-
@test ($)(a, b, c) == BigInt("3426495623485904783798472")
198-
@test ($)(a, b, c, d) == BigInt("-3426495623485906178489610")
199-
@test ($)(a, b, c, d, f) == BigInt("-2413804710837418037418307081437316711364709261074607933698")
200-
@test ($)(a, b, c, d, f, g) == BigInt("2413804710837418037418307081437316711364709261074607933697")
201-
202-
@test (&)(a, b) == BigInt("124")
203-
@test (&)(a, b, c) == BigInt("72")
204-
@test (&)(a, b, c, d) == BigInt("8")
205-
@test (&)(a, b, c, d, f) == BigInt("8")
206-
@test (&)(a, b, c, d, f, g) == BigInt("8")
207-
208-
@test (|)(a, b) == BigInt("327423")
209-
@test (|)(a, b, c) == BigInt("3426495623485904783802111")
210-
@test (|)(a, b, c, d) == BigInt("-1396834561")
211-
@test (|)(a, b, c, d, f) == BigInt("-1358954753")
212-
@test (|)(a, b, c, d, f, g) == BigInt("-1")
177+
a = parse(BigInt,"315135")
178+
b = parse(BigInt,"12412")
179+
c = parse(BigInt,"3426495623485904783478347")
180+
d = parse(BigInt,"-1398984130")
181+
f = parse(BigInt,"2413804710837418037418307081437315263635345357386985747464")
182+
g = parse(BigInt,"-1")
183+
184+
@test +(a, b) == parse(BigInt,"327547")
185+
@test +(a, b, c) == parse(BigInt,"3426495623485904783805894")
186+
@test +(a, b, c, d) == parse(BigInt,"3426495623485903384821764")
187+
@test +(a, b, c, d, f) == parse(BigInt,"2413804710837418037418307081437318690130968843290370569228")
188+
@test +(a, b, c, d, f, g) == parse(BigInt,"2413804710837418037418307081437318690130968843290370569227")
189+
190+
@test *(a, b) == parse(BigInt,"3911455620")
191+
@test *(a, b, c) == parse(BigInt,"13402585563389346256121263521460140")
192+
@test *(a, b, c, d) == parse(BigInt,"-18750004504148804423388563022070650287578200")
193+
@test *(a, b, c, d, f) == parse(BigInt,"-45258849200337190631492857400003938881995610529251881450243326128168934937055005474972396281351684800")
194+
@test *(a, b, c, d, f, g) == parse(BigInt,"45258849200337190631492857400003938881995610529251881450243326128168934937055005474972396281351684800")
195+
196+
@test ($)(a, b) == parse(BigInt,"327299")
197+
@test ($)(a, b, c) == parse(BigInt,"3426495623485904783798472")
198+
@test ($)(a, b, c, d) == parse(BigInt,"-3426495623485906178489610")
199+
@test ($)(a, b, c, d, f) == parse(BigInt,"-2413804710837418037418307081437316711364709261074607933698")
200+
@test ($)(a, b, c, d, f, g) == parse(BigInt,"2413804710837418037418307081437316711364709261074607933697")
201+
202+
@test (&)(a, b) == parse(BigInt,"124")
203+
@test (&)(a, b, c) == parse(BigInt,"72")
204+
@test (&)(a, b, c, d) == parse(BigInt,"8")
205+
@test (&)(a, b, c, d, f) == parse(BigInt,"8")
206+
@test (&)(a, b, c, d, f, g) == parse(BigInt,"8")
207+
208+
@test (|)(a, b) == parse(BigInt,"327423")
209+
@test (|)(a, b, c) == parse(BigInt,"3426495623485904783802111")
210+
@test (|)(a, b, c, d) == parse(BigInt,"-1396834561")
211+
@test (|)(a, b, c, d, f) == parse(BigInt,"-1358954753")
212+
@test (|)(a, b, c, d, f, g) == parse(BigInt,"-1")
213213

214214
@test isprime(BigInt(1000000007))
215215
@test isprime(BigInt(1000000007), 1)
216216
@test isprime(BigInt(10000000019))
217-
@test isprime(BigInt("359334085968622831041960188598043661065388726959079837"))
217+
@test isprime(parse(BigInt,"359334085968622831041960188598043661065388726959079837"))
218218
@test !isprime(BigInt(1))
219219
@test !isprime(BigInt(10000000020))
220220

@@ -258,7 +258,7 @@ s = string(n)
258258

259259
# serialization (#5133)
260260
let
261-
n = BigInt("359334085968622831041960188598043661065388726959079837")
261+
n = parse(BigInt,"359334085968622831041960188598043661065388726959079837")
262262
b = IOBuffer()
263263
serialize(b,n)
264264
seek(b,0)

0 commit comments

Comments
 (0)