11import "stdlib/math.jou"
22import "stdlib/io.jou"
33
4+ # TODO: move these to stdlib/math.jou
5+ #
6+ # Here are some more readable ways to construct non-finite values. Note that
7+ # there are multiple different NaN values (see also: nan boxing), and the NaN
8+ # constructed here is just one of them.
9+ #
10+ # Like M_PI and M_E, these are doubles. Use "as float" as needed.
11+ @public
12+ const INFINITY: double = 1.0 / 0.0
13+ @public
14+ const NAN: double = 0.0 / 0.0
15+
416def main() -> int:
517 printf("%f\n", M_PI) # Output: 3.141593
618 printf("%d\n", M_PI == acos(-1)) # Output: 1
@@ -18,23 +30,41 @@ def main() -> int:
1830 printf("%f\n", fmax(1.2, 3.4)) # Output: 3.400000
1931 printf("%f\n", fmaxf(1.2 as float, 3.4 as float)) # Output: 3.400000
2032
21- test_doubles = [123.0, -123.0, 1.0 / 0.0, -1.0 / 0.0, 0.0 / 0.0, -0.0 / 0.0]
33+ test_doubles = [
34+ 0.0, -0.0, 123.0, -123.0,
35+ # Parentheses are intentionally a bit weirdly here. It shouldn't matter
36+ # how they are and I want to test that.
37+ INFINITY, 1.0/0.0, -INFINITY, (-1.0)/0.0,
38+ NAN, 0.0/0.0, -NAN, 0.0/(-0.0),
39+ ]
2240
2341 # Output: 1 0 0
2442 # Output: 1 0 0
43+ # Output: 1 0 0
44+ # Output: 1 0 0
45+ # Output: 0 1 0
2546 # Output: 0 1 0
2647 # Output: 0 1 0
48+ # Output: 0 1 0
49+ # Output: 0 0 1
50+ # Output: 0 0 1
2751 # Output: 0 0 1
2852 # Output: 0 0 1
2953 for d = &test_doubles[0]; d < &test_doubles[array_count(test_doubles)]; d++:
3054 printf("%d %d %d\n", isfinite(*d), isinf(*d), isnan(*d))
3155
3256 # Output: 1 0 0
3357 # Output: 1 0 0
58+ # Output: 1 0 0
59+ # Output: 1 0 0
60+ # Output: 0 1 0
61+ # Output: 0 1 0
3462 # Output: 0 1 0
3563 # Output: 0 1 0
3664 # Output: 0 0 1
3765 # Output: 0 0 1
66+ # Output: 0 0 1
67+ # Output: 0 0 1
3868 for d = &test_doubles[0]; d < &test_doubles[array_count(test_doubles)]; d++:
3969 printf("%d %d %d\n", isfinite(*d as float), isinf(*d as float), isnan(*d as float))
4070
@@ -106,4 +136,10 @@ def main() -> int:
106136 printf("%f\n", erf(2)) # Output: 0.995322
107137 printf("%f\n", erff(2)) # Output: 0.995322
108138
139+ # This wouldn't work if INFINITY is a float. Then the compiler would infer
140+ # that my_inf is a float and refuse to assign a double to it.
141+ my_inf = INFINITY
142+ my_inf = 12.34
143+ printf("%f\n", my_inf) # Output: 12.340000
144+
109145 return 0
0 commit comments