Skip to content

Commit 7083848

Browse files
committed
use for BigFloat/BigInt
1 parent 7e63e6a commit 7083848

File tree

9 files changed

+112
-98
lines changed

9 files changed

+112
-98
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,AbstractString)
534+
@deprecate BigInt(s::AbstractString) parse(BigInt,AbstractString)

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/mpfr.jl

+22-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,24 @@ 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+
96+
macro bigfloat_str(s)
97+
parse(BigFloat,s)
98+
end
99+
100+
macro big_str(s)
101+
n = tryparse(BigInt,s)
102+
!isnull(n) && return get(n)
103+
n = tryparse(BigFloat,s)
104+
!isnull(n) && return get(n)
105+
throw(ArgumentError("invalid number format $(repr(s)) for BigInt or BigFloat"))
106+
end
107+
96108
convert(::Type{Rational}, x::BigFloat) = convert(Rational{BigInt}, x)
97109
convert{S}(::Type{BigFloat}, x::Rational{S}) = BigFloat(x) # to resolve ambiguity
98110
convert(::Type{BigFloat}, x::Real) = BigFloat(x)

base/string.jl

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

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

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)

test/mpfr.jl

+34-34
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ y = BigFloat(BigInt(12))
1515
@test_approx_eq x y
1616
y = BigFloat(BigFloat(12))
1717
@test_approx_eq x y
18-
y = BigFloat("12")
18+
y = parse(BigFloat,"12")
1919
@test_approx_eq x y
2020
y = BigFloat(Float32(12.))
2121
@test_approx_eq x y
@@ -64,11 +64,11 @@ g = BigFloat(0.03125)
6464
@test +(a, b, c, d, f) == BigFloat(17.6875)
6565
@test +(a, b, c, d, f, g) == BigFloat(17.71875)
6666

67-
@test *(a, b) == BigFloat("2.8328125e+02")
68-
@test *(a, b, c) == BigFloat("-1.98296875e+03")
69-
@test *(a, b, c, d) == BigFloat("2.52828515625e+04")
70-
@test *(a, b, c, d, f) == BigFloat("5.214588134765625e+04")
71-
@test *(a, b, c, d, f, g) == BigFloat("1.6295587921142578125e+03")
67+
@test *(a, b) == parse(BigFloat,"2.8328125e+02")
68+
@test *(a, b, c) == parse(BigFloat,"-1.98296875e+03")
69+
@test *(a, b, c, d) == parse(BigFloat,"2.52828515625e+04")
70+
@test *(a, b, c, d, f) == parse(BigFloat,"5.214588134765625e+04")
71+
@test *(a, b, c, d, f, g) == parse(BigFloat,"1.6295587921142578125e+03")
7272

7373
# < / > / <= / >=
7474
x = BigFloat(12)
@@ -91,7 +91,7 @@ z = BigFloat(30)
9191
with_bigfloat_precision(4) do
9292
# default mode is round to nearest
9393
down, up = with_rounding(BigFloat,RoundNearest) do
94-
BigFloat("0.0938"), BigFloat("0.102")
94+
parse(BigFloat,"0.0938"), parse(BigFloat,"0.102")
9595
end
9696
with_rounding(BigFloat,RoundDown) do
9797
@test BigFloat(0.1) == down
@@ -133,18 +133,18 @@ y = BigFloat(1)
133133
@test isnan(y) == false
134134

135135
# convert to
136-
@test convert(BigFloat, 1//2) == BigFloat("0.5")
136+
@test convert(BigFloat, 1//2) == parse(BigFloat,"0.5")
137137
@test typeof(convert(BigFloat, 1//2)) == BigFloat
138-
@test convert(BigFloat, 0.5) == BigFloat("0.5")
138+
@test convert(BigFloat, 0.5) == parse(BigFloat,"0.5")
139139
@test typeof(convert(BigFloat, 0.5)) == BigFloat
140-
@test convert(BigFloat, 40) == BigFloat("40")
140+
@test convert(BigFloat, 40) == parse(BigFloat,"40")
141141
@test typeof(convert(BigFloat, 40)) == BigFloat
142-
@test convert(BigFloat, Float32(0.5)) == BigFloat("0.5")
142+
@test convert(BigFloat, Float32(0.5)) == parse(BigFloat,"0.5")
143143
@test typeof(convert(BigFloat, Float32(0.5))) == BigFloat
144-
@test convert(BigFloat, BigInt("9223372036854775808")) == BigFloat("9223372036854775808")
145-
@test typeof(convert(BigFloat, BigInt("9223372036854775808"))) == BigFloat
146-
@test convert(FloatingPoint, BigInt("9223372036854775808")) == BigFloat("9223372036854775808")
147-
@test typeof(convert(FloatingPoint, BigInt("9223372036854775808"))) == BigFloat
144+
@test convert(BigFloat, parse(BigInt,"9223372036854775808")) == parse(BigFloat,"9223372036854775808")
145+
@test typeof(convert(BigFloat, parse(BigInt,"9223372036854775808"))) == BigFloat
146+
@test convert(FloatingPoint, parse(BigInt,"9223372036854775808")) == parse(BigFloat,"9223372036854775808")
147+
@test typeof(convert(FloatingPoint, parse(BigInt,"9223372036854775808"))) == BigFloat
148148

149149
# convert from
150150
@test convert(Float64, BigFloat(0.5)) == 0.5
@@ -349,9 +349,9 @@ y = BigFloat(42)
349349
# round
350350
x = BigFloat(42.42)
351351
y = with_bigfloat_precision(256) do
352-
BigFloat("9223372036854775809.2324")
352+
parse(BigFloat,"9223372036854775809.2324")
353353
end
354-
z = BigInt("9223372036854775809")
354+
z = parse(BigInt,"9223372036854775809")
355355
@test round(Integer,x) == 42
356356
@test round(Integer,y) == z
357357
@test typeof(round(UInt8, x)) == UInt8 && round(UInt8, x) == 0x2a
@@ -447,12 +447,12 @@ with_bigfloat_precision(53) do
447447
end
448448

449449
# ceil / floor / trunc
450-
x = BigFloat("28273.7312487489135135135")
450+
x = parse(BigFloat,"28273.7312487489135135135")
451451
y = BigInt(28273)
452452
z = BigInt(28274)
453-
a = BigFloat("123456789012345678901234567890.2414")
454-
b = BigInt("123456789012345678901234567890")
455-
c = BigInt("123456789012345678901234567891")
453+
a = parse(BigFloat,"123456789012345678901234567890.2414")
454+
b = parse(BigInt,"123456789012345678901234567890")
455+
c = parse(BigInt,"123456789012345678901234567891")
456456
@test ceil(x) == z
457457
@test typeof(ceil(x)) == BigFloat
458458
@test floor(x) == y
@@ -547,8 +547,8 @@ c = BigInt("123456789012345678901234567891")
547547

548548
# basic arithmetic
549549
# Signed addition
550-
a = BigFloat("123456789012345678901234567890")
551-
b = BigFloat("123456789012345678901234567891")
550+
a = parse(BigFloat,"123456789012345678901234567890")
551+
b = parse(BigFloat,"123456789012345678901234567891")
552552
@test a+Int8(1) == b
553553
@test a+Int16(1) == b
554554
@test a+Int32(1) == b
@@ -669,10 +669,10 @@ b = BigFloat("123456789012345678901234567891")
669669
@test BigInt(1) * a == a
670670

671671
# Signed division
672-
c = BigInt("61728394506172839450617283945")
672+
c = parse(BigInt,"61728394506172839450617283945")
673673
# d = 2^200
674-
d = BigFloat("1606938044258990275541962092341162602522202993782792835301376")
675-
f = BigFloat("6.223015277861141707144064053780124240590252168721167133101116614789698834035383e-61")
674+
d = parse(BigFloat,"1606938044258990275541962092341162602522202993782792835301376")
675+
f = parse(BigFloat,"6.223015277861141707144064053780124240590252168721167133101116614789698834035383e-61")
676676

677677
@test a/Int8(2) == c
678678
@test a/Int16(2) == c
@@ -715,8 +715,8 @@ f = BigFloat("6.2230152778611417071440640537801242405902521687211671331011166147
715715
# old tests
716716
tol = 1e-12
717717

718-
a = BigFloat("12.34567890121")
719-
b = BigFloat("12.34567890122")
718+
a = parse(BigFloat,"12.34567890121")
719+
b = parse(BigFloat,"12.34567890122")
720720

721721
@test_approx_eq_eps a+1e-11 b tol
722722
@test !(b == a)
@@ -725,13 +725,13 @@ b = BigFloat("12.34567890122")
725725
@test !(b < a)
726726
@test !(b <= a)
727727

728-
c = BigFloat("24.69135780242")
728+
c = parse(BigFloat,"24.69135780242")
729729
@test typeof(a * 2) == BigFloat
730730
@test_approx_eq_eps a*2 c tol
731731
@test_approx_eq_eps (c-a) a tol
732732

733733

734-
d = BigFloat("-24.69135780242")
734+
d = parse(BigFloat,"-24.69135780242")
735735
@test typeof(d) == BigFloat
736736
@test_approx_eq_eps d+c 0 tol
737737

@@ -759,8 +759,8 @@ d = BigFloat("-24.69135780242")
759759
@test typeof(BigFloat(1//1)) == BigFloat
760760
@test typeof(BigFloat(one(Rational{BigInt}))) == BigFloat
761761

762-
f = BigFloat("1234567890.123")
763-
g = BigFloat("1234567891.123")
762+
f = parse(BigFloat,"1234567890.123")
763+
g = parse(BigFloat,"1234567891.123")
764764

765765
tol = 1e-3
766766

@@ -796,7 +796,7 @@ tol = 1e-3
796796

797797
# issue #3399
798798
i1 = BigInt(10)^Int32(1000)
799-
i2 = BigInt("10000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
799+
i2 = parse(BigInt,"10000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
800800
f = BigFloat(10)^Int32(1000)
801801
@test i1 != i2
802802
@test i1 != f
@@ -811,7 +811,7 @@ i3 = trunc(Integer,f)
811811
@test i3+1 >= f
812812

813813
err(z, x) = abs(z - x) / abs(x)
814-
@test 1e-60 > err(eta(BigFloat("1.005")), BigFloat("0.693945708117842473436705502427198307157819636785324430166786"))
814+
@test 1e-60 > err(eta(parse(BigFloat,"1.005")), parse(BigFloat,"0.693945708117842473436705502427198307157819636785324430166786"))
815815
@test 1e-60 > err(exp(eta(big(1.0))), 2.0)
816816

817817
# issue #8318

0 commit comments

Comments
 (0)