1 | ## oils_failures_allowed: 0
|
2 |
|
3 | #### Pound char literal (is an integer TODO: could be ord())
|
4 | const a = #'a'
|
5 | const A = #'A'
|
6 | echo "$a $A"
|
7 | ## STDOUT:
|
8 | 97 65
|
9 | ## END
|
10 |
|
11 | #### The literal #''' isn't accepted (use \' instead)
|
12 |
|
13 | # This looks too much like triple quoted strings!
|
14 |
|
15 | echo nope
|
16 | const bad = #'''
|
17 | echo "$bad"
|
18 |
|
19 | ## status: 2
|
20 | ## STDOUT:
|
21 | nope
|
22 | ## END
|
23 |
|
24 | #### Float Literals with e-1
|
25 |
|
26 | shopt -s ysh:upgrade
|
27 | # 1+2 2.3
|
28 | var x = 1.2 + 23.0e-1 # 3.5
|
29 | if (3.4 < x and x < 3.6) {
|
30 | echo ok
|
31 | }
|
32 | ## STDOUT:
|
33 | ok
|
34 | ## END
|
35 |
|
36 | #### Float Literal with _
|
37 |
|
38 | shopt -s ysh:upgrade
|
39 |
|
40 | # 1+2 + 2.3
|
41 | # add this _ here
|
42 | var x = 1.2 + 2_3.0e-1 # 3.5
|
43 | if (3.4 < x and x < 3.6) {
|
44 | echo ok
|
45 | }
|
46 |
|
47 | ## STDOUT:
|
48 | ok
|
49 | ## END
|
50 |
|
51 |
|
52 | #### Period requires digit on either side, not 5. or .5
|
53 | echo $[0.5]
|
54 | echo $[5.0]
|
55 | echo $[5.]
|
56 | echo $[.5]
|
57 |
|
58 | ## status: 2
|
59 | ## STDOUT:
|
60 | 0.5
|
61 | 5.0
|
62 | ## END
|
63 |
|
64 | #### Big float Literals with _
|
65 |
|
66 | # C++ issue: we currently print with snprintf %g
|
67 | # Pars
|
68 |
|
69 | echo $[42_000.000_500]
|
70 |
|
71 | echo $[42_000.000_500e1]
|
72 | echo $[42_000.000_500e-1]
|
73 |
|
74 | ## STDOUT:
|
75 | 42000.0005
|
76 | 420000.005
|
77 | 4200.00005
|
78 | ## END
|
79 |
|
80 | #### Big floats like 1e309 and -1e309 go to Inf / -Inf
|
81 |
|
82 | # Notes
|
83 | # - Python float() and JS parseFloat() agree with this behavior
|
84 | # - JSON doesn't have inf / -inf
|
85 |
|
86 | echo $[1e309]
|
87 | echo $[-1e309]
|
88 |
|
89 | ## STDOUT:
|
90 | inf
|
91 | -inf
|
92 | ## END
|
93 |
|
94 | #### Tiny floats go to zero
|
95 |
|
96 | shopt -s ysh:upgrade
|
97 | # TODO: need equivalent of in YSh
|
98 | # '0' * 309
|
99 | # ['0'] * 309
|
100 |
|
101 | # 1e-324 == 0.0 in Python
|
102 |
|
103 | var zeros = []
|
104 | for i in (1 .. 324) {
|
105 | call zeros->append('0')
|
106 | }
|
107 |
|
108 | #= zeros
|
109 |
|
110 | var s = "0.$[join(zeros)]1"
|
111 | #echo $s
|
112 |
|
113 | echo float=$[float(s)]
|
114 |
|
115 | ## STDOUT:
|
116 | float=0.0
|
117 | ## END
|
118 |
|
119 |
|
120 | #### floatEquals() INFINITY NAN
|
121 |
|
122 | shopt --set ysh:upgrade
|
123 | source $LIB_YSH/list.ysh
|
124 |
|
125 | # Create inf
|
126 | var big = repeat('12345678', 100) ++ '.0'
|
127 |
|
128 | var inf = fromJson(big)
|
129 | var neg_inf = fromJson('-' ++ big)
|
130 |
|
131 | if (floatsEqual(inf, INFINITY)) {
|
132 | echo inf
|
133 | }
|
134 |
|
135 | if (floatsEqual(neg_inf, -INFINITY)) {
|
136 | echo neg_inf
|
137 | }
|
138 |
|
139 | if (floatsEqual(NAN, INFINITY)) {
|
140 | echo bad
|
141 | }
|
142 |
|
143 | if (floatsEqual(NAN, NAN)) {
|
144 | echo bad
|
145 | }
|
146 |
|
147 | if (not floatsEqual(NAN, NAN)) {
|
148 | echo 'nan is not nan'
|
149 | }
|
150 |
|
151 | ## STDOUT:
|
152 | inf
|
153 | neg_inf
|
154 | nan is not nan
|
155 | ## END
|
156 |
|
157 | #### Regression: 1/3 gives 0.3+
|
158 |
|
159 | # We were using float precision, not double
|
160 |
|
161 | shopt --set ysh:upgrade
|
162 |
|
163 | pp line (1/3) | read --all
|
164 | if (_reply ~ / '0.' '3'+ / ) {
|
165 | echo one-third
|
166 | }
|
167 |
|
168 | pp line (2/3) | read --all
|
169 | if (_reply ~ / '0.' '6'+ '7' / ) {
|
170 | echo two-thirds
|
171 | }
|
172 |
|
173 | ## STDOUT:
|
174 | one-third
|
175 | two-thirds
|
176 | ## END
|
177 |
|
178 | #### Number of digits in 1/3
|
179 | shopt --set ysh:upgrade
|
180 |
|
181 | # - Python 2 and bin/ysh: 14
|
182 | # - Python 3: 18
|
183 | # - YSH C++: 19 - see mycpp/float_test.cc, tip from Bruce Dawson
|
184 |
|
185 | var s = str(1/3)
|
186 | #echo "ysh len $[len(s)]"
|
187 | #echo ysh=$s
|
188 |
|
189 | # Don't bother to distinguish OSH Python vs C++ here
|
190 | case (len(s)) {
|
191 | (14) { echo pass }
|
192 | (19) { echo pass }
|
193 | (else) { echo FAIL }
|
194 | }
|
195 |
|
196 | exit
|
197 |
|
198 | var py2 = $(python2 -c 'print(1.0/3)')
|
199 | echo "py2 len $[len(py2)]"
|
200 | echo py2=$py2
|
201 |
|
202 | var py3 = $(python3 -c 'print(1/3)')
|
203 | echo "py3 len $[len(py3)]"
|
204 | echo py3=$py3
|
205 |
|
206 | ## STDOUT:
|
207 | pass
|
208 | ## END
|