|
100 | 100 | end
|
101 | 101 | end
|
102 | 102 |
|
| 103 | + @testset "to float" begin |
| 104 | + # Convert the rational 5//7 into a FixedDecimal with as much precision as we can |
| 105 | + # without using BigInt. |
| 106 | + T = Int128 |
| 107 | + f = FixedPointDecimals.max_exp10(T) |
| 108 | + powt = FixedPointDecimals.coefficient(FD{T,f}) |
| 109 | + val = T(trunc(BigInt, widemul(5//7, powt))) |
| 110 | + |
| 111 | + fd = reinterpret(FD{T,f}, val) |
| 112 | + @test convert(Float64, fd) != convert(BigFloat, fd) |
| 113 | + @test convert(Float64, fd) == T(val) / T(powt) |
| 114 | + @test convert(BigFloat, fd) == BigInt(val) / BigInt(powt) |
| 115 | + end |
| 116 | + |
103 | 117 | @testset "invalid" begin
|
104 | 118 | @test_throws InexactError convert(FD2, FD4(0.0001))
|
105 | 119 | @test_throws InexactError convert(FD4, typemax(FD2))
|
|
129 | 143 |
|
130 | 144 | @test_throws InexactError convert(FD{T,f}, T(1))
|
131 | 145 |
|
| 146 | + # Converting to a floating-point |
| 147 | + fd = reinterpret(FD{T,f}, typemax(T)) |
| 148 | + @test convert(Float32, fd) == Float32(typemax(T) / powt) |
| 149 | + @test convert(Float64, fd) == Float64(typemax(T) / powt) |
| 150 | + @test convert(BigFloat, fd) == BigInt(typemax(T)) / powt |
| 151 | + |
| 152 | + fd = reinterpret(FD{T,f}, typemin(T)) |
| 153 | + @test convert(Float32, fd) == Float32(typemin(T) / powt) |
| 154 | + @test convert(Float64, fd) == Float64(typemin(T) / powt) |
| 155 | + @test convert(BigFloat, fd) == BigInt(typemin(T)) / powt |
| 156 | + |
132 | 157 | # Adjust number of decimal places allowed so we can have `-10 < x < 10` where x is
|
133 | 158 | # a FD{T,f}. Only needed to test `convert(::FD, ::Integer)`
|
134 | 159 | f = FixedPointDecimals.max_exp10(T)
|
|
0 commit comments