File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 393
393
The highest value of `x` which does not result in an overflow when evaluating `T(10)^x`.
394
394
"""
395
395
function max_exp10 {T <: Integer} (:: Type{T} )
396
- length (digits (typemax (T))) - 1
396
+ W = widen (T)
397
+ type_max = W (typemax (T))
398
+
399
+ powt = one (W)
400
+ ten = W (10 )
401
+ exponent = 0
402
+
403
+ while type_max > powt
404
+ powt *= ten
405
+ exponent += 1
406
+ end
407
+
408
+ exponent - 1
397
409
end
398
410
399
411
"""
Original file line number Diff line number Diff line change @@ -74,6 +74,21 @@ if VERSION < v"0.6.0-dev.1849"
74
74
Base.:/ (x:: UInt128 , y:: BigInt ) = / (promote (x, y)... )
75
75
end
76
76
77
+ @testset " max_exp10" begin
78
+ @test FixedPointDecimals. max_exp10 (Int8) == 2
79
+ @test FixedPointDecimals. max_exp10 (Int64) == 18
80
+ @test FixedPointDecimals. max_exp10 (Int128) == 38
81
+ @test FixedPointDecimals. max_exp10 (UInt8) == 2
82
+ @test FixedPointDecimals. max_exp10 (UInt64) == 19
83
+ @test FixedPointDecimals. max_exp10 (UInt128) == 38
84
+ @test_throws MethodError FixedPointDecimals. max_exp10 (BigInt)
85
+
86
+ for T in CONTAINER_TYPES
87
+ x = FixedPointDecimals. max_exp10 (T)
88
+ @test T (10 )^ x == widen (T (10 ))^ x
89
+ end
90
+ end
91
+
77
92
# ensure that the coefficient multiplied by the highest and lowest representable values of
78
93
# the container type do not result in overflow.
79
94
@testset " coefficient" begin
You can’t perform that action at this time.
0 commit comments