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

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