OILS / spec / ysh-int-float.test.sh View on Github | oilshell.org

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