OILS / spec / ysh-int-float.test.sh View on Github | oils.pub

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