1 #### Pound char literal (is an integer TODO: could be ord())
2 const a = #'a'
3 const A = #'A'
4 echo "$a $A"
5 ## STDOUT:
6 97 65
7 ## END
8
9 #### The literal #''' isn't accepted (use \' instead)
10
11 # This looks too much like triple quoted strings!
12
13 echo nope
14 const bad = #'''
15 echo "$bad"
16
17 ## status: 2
18 ## STDOUT:
19 nope
20 ## END
21
22 #### Float Literals with e-1
23
24 shopt -s ysh:upgrade
25 # 1+2 2.3
26 var x = 1.2 + 23.0e-1 # 3.5
27 if (3.4 < x and x < 3.6) {
28 echo ok
29 }
30 ## STDOUT:
31 ok
32 ## END
33
34 #### Float Literal with _
35
36 shopt -s ysh:upgrade
37
38 # 1+2 + 2.3
39 # add this _ here
40 var x = 1.2 + 2_3.0e-1 # 3.5
41 if (3.4 < x and x < 3.6) {
42 echo ok
43 }
44
45 ## STDOUT:
46 ok
47 ## END
48
49
50 #### Period requires digit on either side, not 5. or .5
51 echo $[0.5]
52 echo $[5.0]
53 echo $[5.]
54 echo $[.5]
55
56 ## status: 2
57 ## STDOUT:
58 0.5
59 5.0
60 ## END
61
62 #### Big float Literals with _
63
64 # C++ issue: we currently print with snprintf %g
65 # Pars
66
67 echo $[42_000.000_500]
68
69 echo $[42_000.000_500e1]
70 echo $[42_000.000_500e-1]
71
72 ## STDOUT:
73 42000.0005
74 420000.005
75 4200.00005
76 ## END
77
78 #### Big floats like 1e309 and -1e309 go to Inf / -Inf
79
80 # Notes
81 # - Python float() and JS parseFloat() agree with this behavior
82 # - JSON doesn't have inf / -inf
83
84 echo $[1e309]
85 echo $[-1e309]
86
87 ## STDOUT:
88 inf
89 -inf
90 ## END
91
92 #### Tiny floats go to zero
93
94 shopt -s ysh:upgrade
95 # TODO: need equivalent of in YSh
96 # '0' * 309
97 # ['0'] * 309
98
99 # 1e-324 == 0.0 in Python
100
101 var zeros = []
102 for i in (1 .. 324) {
103 call zeros->append('0')
104 }
105
106 #= zeros
107
108 var s = "0.$[join(zeros)]1"
109 #echo $s
110
111 echo float=$[float(s)]
112
113 ## STDOUT:
114 float=0.0
115 ## END
116