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

146 lines, 56 significant
1## oils_failures_allowed: 1
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#### INFINITY NAN floatEquals()
121
122echo TODO
123
124## STDOUT:
125## END
126
127#### Regression: 1/3 gives 0.3+
128
129# We were using float precision, not double
130
131shopt --set ysh:upgrade
132
133pp line (1/3) | read --all
134if (_reply ~ / '0.' '3'+ / ) {
135 echo one-third
136}
137
138pp line (2/3) | read --all
139if (_reply ~ / '0.' '6'+ '7' / ) {
140 echo two-thirds
141}
142
143## STDOUT:
144one-third
145two-thirds
146## END