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