|
97 | 97 | x = N0f8(0.5)
|
98 | 98 | @test convert(N0f8, x) === x
|
99 | 99 |
|
100 |
| - @test convert(N0f8, 1.1/typemax(UInt8)) == eps(N0f8) |
101 |
| - @test convert(N6f10, 1.1/typemax(UInt16)*64) == eps(N6f10) |
102 |
| - @test convert(N4f12, 1.1/typemax(UInt16)*16) == eps(N4f12) |
103 |
| - @test convert(N2f14, 1.1/typemax(UInt16)*4) == eps(N2f14) |
104 |
| - @test convert(N0f16, 1.1/typemax(UInt16)) == eps(N0f16) |
105 |
| - @test convert(Normed{UInt32,16}, 1.1/typemax(UInt32)*2^16) == eps(Normed{UInt32,16}) |
106 |
| - @test convert(Normed{UInt64,3}, 1.1/typemax(UInt64)*UInt64(2)^61) == eps(Normed{UInt64,3}) |
107 |
| - @test convert(Normed{UInt128,7}, 1.1/typemax(UInt128)*UInt128(2)^121) == eps(Normed{UInt128,7}) |
108 |
| - |
109 |
| - @test convert(N0f8, 1.1f0/typemax(UInt8)) == eps(N0f8) |
110 |
| - |
111 |
| - @test convert(N0f8, 1//255) === eps(N0f8) |
112 |
| - @test convert(N0f8, Rational{Int8}(3//5)) === N0f8(3/5) |
113 |
| - @test convert(N0f8, Rational{UInt8}(3//5)) === N0f8(3/5) |
114 |
| - @test_throws ArgumentError convert(N0f8, typemax(Rational{UInt8})) |
| 100 | + @test convert(N0f8, 1.1/typemax(UInt8)) === eps(N0f8) |
| 101 | + @test convert(N0f8, 1.1f0/typemax(UInt8)) === eps(N0f8) |
| 102 | + @test convert(N6f10, 1.1/typemax(UInt16)*64) === eps(N6f10) |
| 103 | + @test convert(N4f12, 1.1/typemax(UInt16)*16) === eps(N4f12) |
| 104 | + @test convert(N2f14, 1.1/typemax(UInt16)*4) === eps(N2f14) |
| 105 | + @test convert(N0f16, 1.1/typemax(UInt16)) === eps(N0f16) |
| 106 | + @test convert(N16f16, 1.1/typemax(UInt32)*2^16) === eps(N16f16) |
| 107 | + @test convert(N61f3, 1.1/typemax(UInt64)*UInt64(2)^61) === eps(N61f3) |
| 108 | + @test convert(Normed{UInt128,7}, 1.1/typemax(UInt128)*UInt128(2)^121) === eps(Normed{UInt128,7}) |
115 | 109 |
|
116 | 110 | @test convert(N0f8, Base.TwicePrecision(1.0)) === 1N0f8
|
117 | 111 |
|
118 |
| - @test convert(Float64, eps(N0f8)) == 1/typemax(UInt8) |
119 |
| - @test convert(Float32, eps(N0f8)) == 1.0f0/typemax(UInt8) |
120 |
| - @test convert(BigFloat, eps(N0f8)) == BigFloat(1)/typemax(UInt8) |
121 |
| - # TODO: migrate to separate testsets |
122 |
| - for T in target(Normed) |
123 |
| - @test convert(Bool, zero(T)) == false |
124 |
| - @test convert(Bool, one(T)) == true |
125 |
| - eps(T) != 1 && @test_throws InexactError convert(Bool, convert(T, 0.2)) |
126 |
| - @test convert(Int, one(T)) == 1 |
127 |
| - @test convert(Integer, one(T)) == 1 |
128 |
| - @test convert(Rational, one(T)) == 1 |
129 |
| - end |
130 | 112 | @test convert(N0f16, one(N0f8)) === one(N0f16)
|
131 |
| - @test convert(N0f16, N0f8(0.5)).i === 0x8080 |
132 |
| - @test convert(Normed{UInt16,7}, Normed{UInt8,7}(0.504)) === Normed{UInt16,7}(0.504) |
| 113 | + @test convert(N0f16, N0f8(0.5)) === reinterpret(N0f16, 0x8080) |
| 114 | + @test convert(N9f7, N1f7(0.504)) === N9f7(0.504) |
133 | 115 |
|
134 | 116 | # avoiding overflow with Float16
|
135 | 117 | @test N0f16(Float16(1.0)) === N0f16(1.0)
|
136 | 118 | @test Float16(1.0) % N0f16 === N0f16(1.0)
|
137 | 119 | end
|
138 | 120 |
|
| 121 | +@testset "bool conversions" begin |
| 122 | + @testset "$N to/from Bool" for N in target(Normed) |
| 123 | + @test convert(Bool, zero(N)) === false |
| 124 | + @test convert(Bool, oneunit(N)) === true |
| 125 | + eps(N) < 1 && @test_throws InexactError convert(Bool, convert(N, 0.2)) |
| 126 | + @test convert(N, true) === oneunit(N) |
| 127 | + @test convert(N, false) === zero(N) |
| 128 | + end |
| 129 | + @test Bool(1N0f8) === true |
| 130 | +end |
| 131 | + |
139 | 132 | @testset "integer conversions" begin
|
| 133 | + @testset "$N to/from integer" for N in target(Normed) |
| 134 | + @test convert(Int, oneunit(N)) === 1 |
| 135 | + @test convert(Integer, oneunit(N)) === oneunit(rawtype(N)) |
| 136 | + @test convert(N, 1) === oneunit(N) |
| 137 | + @test convert(N, 0x0) === zero(N) |
| 138 | + end |
140 | 139 | @test convert(UInt, 1N1f7) === UInt(1)
|
141 |
| - @test convert(Integer, 1N1f7) === 0x01 |
142 |
| - @test convert(Int, 1N1f7) === 1 |
143 | 140 | @test_throws InexactError convert(Integer, 0.5N1f7)
|
144 | 141 | @test_throws InexactError convert(Int8, 256N8f8)
|
145 | 142 | end
|
146 | 143 |
|
147 | 144 | @testset "rational conversions" begin
|
| 145 | + @testset "$N to/from rational" for N in target(Normed) |
| 146 | + @test convert(Rational, oneunit(N)) == 1//1 |
| 147 | + @test convert(Rational{Int}, zero(N)) === 0//1 |
| 148 | + @test convert(N, 1//1) === oneunit(N) |
| 149 | + end |
| 150 | + @test convert(N0f8, 1//255) === eps(N0f8) |
| 151 | + @test convert(N0f8, Rational{Int8}(3//5)) === N0f8(3/5) |
| 152 | + @test convert(N0f8, Rational{UInt8}(3//5)) === N0f8(3/5) |
| 153 | + @test_throws ArgumentError convert(N0f8, typemax(Rational{UInt8})) |
| 154 | + |
148 | 155 | @test convert(Rational, 0.5N0f8) === Rational{UInt8}(0x80//0xff)
|
149 | 156 | @test convert(Rational, 0.5N4f12) === Rational{UInt16}(0x800//0xfff)
|
150 | 157 | @test convert(Rational{Int}, 0.5N0f8) === Rational{Int}(0x80//0xff)
|
|
157 | 164 |
|
158 | 165 | @testset "BigFloat conversions" begin
|
159 | 166 | @test convert(BigFloat, 0.5N0f8)::BigFloat == 128 / big"255"
|
| 167 | + @test convert(BigFloat, eps(N0f8))::BigFloat == 1 / big"255" |
160 | 168 |
|
161 | 169 | @test big(N7f1) === BigFloat # !== BigInt
|
162 | 170 | @test big(0.5N4f4)::BigFloat == 8 / big"15"
|
|
0 commit comments