Skip to content

Commit 961642f

Browse files
authored
mpfr.jl: don't add extra zero when pretty-printing BigFloats (#40513)
* mpfr.jl: don't add extra zero when pretty-printing BigFloats * Cleanup MPFR tests - Make blank lines consistent: none around testset blocks, but present around standalone tests - Clarify name of testset: 'precision' --> 'setprecision' - Merge two redundant testsets on comparison operators
1 parent 5162023 commit 961642f

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

base/mpfr.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ function _prettify_bigfloat(s::String)::String
982982
neg = startswith(int, '-')
983983
neg == true && (int = lstrip(int, '-'))
984984
@assert length(int) == 1
985-
string(neg ? '-' : "", '0', '.', '0'^(-expo-1), int, frac)
985+
string(neg ? '-' : "", '0', '.', '0'^(-expo-1), int, frac == "0" ? "" : frac)
986986
end
987987
else
988988
string(mantissa, 'e', exponent)

test/mpfr.jl

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -338,23 +338,6 @@ end
338338
@test *(a, b, c, d, f) == parse(BigFloat,"5.214588134765625e+04")
339339
@test *(a, b, c, d, f, g) == parse(BigFloat,"1.6295587921142578125e+03")
340340
end
341-
@testset "< / > / <= / >=" begin
342-
x = BigFloat(12)
343-
y = BigFloat(42)
344-
z = BigFloat(30)
345-
@test y > x
346-
@test y >= x
347-
@test y > z
348-
@test y >= z
349-
@test x < y
350-
@test x <= y
351-
@test z < y
352-
@test z <= y
353-
@test y - x >= z
354-
@test y - x <= z
355-
@test !(x >= z)
356-
@test !(y <= z)
357-
end
358341
@testset "rounding modes" begin
359342
setprecision(4) do
360343
# default mode is round to nearest
@@ -371,7 +354,6 @@ end
371354
end
372355
end
373356
end
374-
375357
@testset "copysign / sign" begin
376358
x = BigFloat(1)
377359
y = BigFloat(-1)
@@ -473,10 +455,11 @@ end
473455
@test isnan(nextfloat(BigFloat(NaN), 1))
474456
@test isnan(prevfloat(BigFloat(NaN), 1))
475457
end
458+
476459
# sqrt DomainError
477460
@test_throws DomainError sqrt(BigFloat(-1))
478461

479-
@testset "precision" begin
462+
@testset "setprecision" begin
480463
old_precision = precision(BigFloat)
481464
x = BigFloat(0)
482465
@test precision(x) == old_precision
@@ -512,7 +495,6 @@ end
512495
@test !isinteger(-BigFloat(Inf))
513496
@test !isinteger(BigFloat(NaN))
514497
end
515-
516498
@testset "comparisons" begin
517499
x = BigFloat(1)
518500
y = BigFloat(-1)
@@ -521,9 +503,11 @@ end
521503
imi = BigFloat(-Inf)
522504
@test x > y
523505
@test x >= y
506+
@test !(y >= x)
524507
@test x >= x
525508
@test y < x
526509
@test y <= x
510+
@test !(x <= y)
527511
@test y <= y
528512
@test x < ipl
529513
@test x <= ipl
@@ -675,14 +659,17 @@ end
675659
end
676660
setprecision(21) do
677661
@test string(parse(BigFloat, "0.1")) == "0.10000002"
662+
@test string(parse(BigFloat, "0.5")) == "0.5"
678663
@test string(parse(BigFloat, "-9.9")) == "-9.9000015"
679664
end
680665
setprecision(40) do
681666
@test string(parse(BigFloat, "0.1")) == "0.10000000000002"
667+
@test string(parse(BigFloat, "0.5")) == "0.5"
682668
@test string(parse(BigFloat, "-9.9")) == "-9.8999999999942"
683669
end
684670
setprecision(123) do
685671
@test string(parse(BigFloat, "0.1")) == "0.0999999999999999999999999999999999999953"
672+
@test string(parse(BigFloat, "0.5")) == "0.5"
686673
@test string(parse(BigFloat, "-9.9")) == "-9.8999999999999999999999999999999999997"
687674
end
688675
end
@@ -927,6 +914,7 @@ end
927914
@test i3+1 > f
928915
@test i3+1 >= f
929916
end
917+
930918
# issue #8318
931919
@test convert(Int64,big(500_000_000_000_000.)) == 500_000_000_000_000
932920

@@ -935,6 +923,7 @@ end
935923
@test MPFR.get_emin() == MPFR.get_emin_min()
936924
@test MPFR.get_emax() == MPFR.get_emax_max()
937925
end
926+
938927
# issue #10994: handle embedded NUL chars for string parsing
939928
@test_throws ArgumentError parse(BigFloat, "1\0")
940929

@@ -1025,7 +1014,6 @@ end
10251014
@test to_string(big"-1.0") == "-1.0"
10261015
end
10271016
end
1028-
10291017
@testset "big(::Type)" begin
10301018
for x in (2f0, pi, 7.8, big(ℯ))
10311019
@test big(typeof(x)) == typeof(big(x))

0 commit comments

Comments
 (0)