Skip to content

Commit c57c4f8

Browse files
committed
Merge pull request scala#2352 from soc/nan-tests
Add float version of the double NaN tests
2 parents f09aaa3 + 497b0cb commit c57c4f8

6 files changed

+71
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
if (NaN == NaN) is good
2+
if (x == x) is good
3+
if (x == NaN) is good
4+
if (NaN != NaN) is good
5+
if (x != x) is good
6+
if (NaN != x) is good
7+
x matching was good
8+
NaN matching was good
9+
loop with NaN was goood
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-optimise
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
object Test extends App {
2+
import Float.NaN
3+
4+
// NaN must not equal NaN no matter what optimizations are applied
5+
// All the following will seem redundant, but to an optimizer
6+
// they can appear different
7+
8+
val x = NaN
9+
10+
if (NaN == NaN)
11+
println("if (NaN == NaN) is broken")
12+
else
13+
println("if (NaN == NaN) is good")
14+
15+
if (x == x)
16+
println("if (x == x) is broken")
17+
else
18+
println("if (x == x) is good")
19+
20+
if (x == NaN)
21+
println("if (x == NaN) is broken")
22+
else
23+
println("if (x == NaN) is good")
24+
25+
if (NaN != NaN)
26+
println("if (NaN != NaN) is good")
27+
else
28+
println("if (NaN != NaN) broken")
29+
30+
if (x != x)
31+
println("if (x != x) is good")
32+
else
33+
println("if (x != x) broken")
34+
35+
if (NaN != x)
36+
println("if (NaN != x) is good")
37+
else
38+
println("if (NaN != x) is broken")
39+
40+
x match {
41+
case 0.0f => println("x matched 0!")
42+
case NaN => println("x matched NaN!")
43+
case _ => println("x matching was good")
44+
}
45+
46+
NaN match {
47+
case 0.0f => println("NaN matched 0!")
48+
case NaN => println("NaN matched NaN!")
49+
case _ => println("NaN matching was good")
50+
}
51+
52+
var z = 0.0f
53+
var i = 0
54+
while (i < 10) {
55+
if (i % 2 == 0) z = NaN
56+
else z = NaN
57+
i += 1
58+
}
59+
if (z.isNaN && i == 10) println("loop with NaN was goood")
60+
else println("loop with NaN was broken")
61+
}

0 commit comments

Comments
 (0)