This repository was archived by the owner on Dec 16, 2022. It is now read-only.
forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfloat16.jl
97 lines (83 loc) · 2.47 KB
/
float16.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
f = float16(2.)
g = float16(1.)
@test f >= g
@test f > g
@test g < f
@test g <= g
@test all([g g] .< [f f])
@test all([g g] .<= [f f])
@test all([f f] .> [g g])
@test all([f f] .>= [g g])
@test isless(g, f)
@test !isless(f, g)
@test -f === float16(-2.)
@test f+g === float16(3f0)
@test f-g === float16(1f0)
@test f*g === float16(2f0)
@test f/g === float16(2f0)
@test f^g === float16(2f0)
@test f^-g === float16(0.5f0)
@test f + 2 === float32(4f0)
@test f - 2 === float32(0f0)
@test f*2 === float32(4f0)
@test f/2 === float32(1f0)
@test f + 2. === 4.
@test f - 2. === 0.
@test f*2. === 4.
@test f/2. === 1.
@test_approx_eq sin(f) sin(2f0)
@test isnan(NaN16)
@test isnan(-NaN16)
@test !isnan(Inf16)
@test !isnan(-Inf16)
@test !isnan(float16(2.6))
@test NaN16 != NaN16
@test isequal(NaN16, NaN16)
@test repr(NaN16) == "NaN16"
@test sprint(showcompact, NaN16) == "NaN"
@test isinf(Inf16)
@test isinf(-Inf16)
@test !isinf(NaN16)
@test !isinf(-NaN16)
@test !isinf(float16(2.6))
@test Inf16 == Inf16
@test Inf16 != -Inf16
@test -Inf16 < Inf16
@test isequal(Inf16, Inf16)
@test repr(Inf16) == "Inf16"
@test sprint(showcompact, Inf16) == "Inf"
for z1 in (float16(0.0), float16(-0.0)), z2 in (float16(0.0), float16(-0.0))
@test z1 == z2
@test isequal(z1, z1)
@test z1 === z1
for elty in (Float32, Float64)
z3 = convert(elty, z2)
@test z1==z3
end
end
@test float16(2.5) == float16(2.5)
@test float16(2.5) != float16(2.6)
@test isequal(float16(0.0), float16(0.0))
@test !isequal(float16(-0.0), float16(0.0))
@test !isequal(float16(0.0), float16(-0.0))
@test isnan(reinterpret(Float16,0x7c01))
@test !isinf(reinterpret(Float16,0x7c01))
@test nextfloat(Inf16) === Inf16
@test prevfloat(-Inf16) === -Inf16
# rounding in conversions
let
for f in [.3325f0, -.3325f0]
f16 = float16(f)
# need to round away from 0. make sure we picked closest number.
@test abs(f-f16) < abs(f-nextfloat(f16))
@test abs(f-f16) < abs(f-prevfloat(f16))
end
# halfway between and last bit is 1
f = reinterpret(Float32, 0b00111110101010100011000000000000)
@test float32(float16(f)) === reinterpret(Float32, 0b00111110101010100100000000000000)
# halfway between and last bit is 0
f = reinterpret(Float32, 0b00111110101010100001000000000000)
@test float32(float16(f)) === reinterpret(Float32, 0b00111110101010100000000000000000)
end
# issue #5948
@test string(reinterpret(Float16, 0x7bff)) == "65504.0"