1 # Tests corresponding to the texinfo node `Arithmetic Evaluation'
9 0:basic integer arithmetic
14 (( light = 3.1415 )) &&
15 let 'there = light' &&
16 print -- $(( rnd = there * 10000 ))
17 # save rounding problems by converting to integer
18 0:basic floating point arithmetic
21 print $(( 0x10 + 0X01 + 2#1010 ))
41 (( i = 4 - - 3 * 7 << 1 & 7 ^ 1 | 16 ** 2 ))
43 0:precedence (arithmetic)
47 setopt localoptions c_precedences
49 (( i = 4 - - 3 * 7 << 1 & 7 ^ 1 | 16 ** 2 ))
53 0:precedence (arithmetic, with C_PRECEDENCES)
56 print $(( 1 < 2 || 2 < 2 && 3 > 4 ))
57 0:precedence (logical)
60 print $(( 1 + 4 ? 3 + 2 ? 4 + 3 ? 5 + 6 ? 4 * 8 : 0 : 0 : 0 : 0 ))
61 0:precedence (ternary)
66 ?(eval):1: ':' expected
68 print $(( 3 ? 2 : 1 : 4 ))
70 ?(eval):1: ':' without '?'
72 print $(( 0, 4 ? 3 : 1, 5 ))
77 print $(( ##A + ##\C-a + #foo + $#foo ))
82 1:## without following character
83 ?(eval):1: character missing after ##
86 0:## followed by a space
90 (( i = 3 + 5 * 1.75 ))
100 0:use of scalars to store integers and floats
104 (( newarray[unsetvar] = 1 ))
105 2:error using unset variable as index
106 ?(eval):1: newarray: assignment to invalid subscript range
109 (( newarray[setvar]++ ))
110 (( newarray[setvar]++ ))
111 print ${(t)newarray} ${#newarray} ${newarray[1]}
112 0:setting array elements in math context
117 print ${(t)xarr} $xarr
118 0:converting type from array
123 ?(eval):1: lvalue required
128 0:assigning to scalar which contains non-math string
132 0:empty math parse e.g. $(( )) acts like a zero
137 ?(eval):1: bad math expression: operand expected at `'
140 1:empty right hand of comma
141 ?(eval):1: bad math expression: operand expected at `'
144 1:empty middle of comma
145 ?(eval):1: bad math expression: operand expected at `,4 '
147 print $(( (3 + 7, 4), 5 ))
148 0:commas and parentheses, part 1
151 print $(( 5, (3 + 7, 4) ))
152 0:commas and parentheses, part 1
155 (setopt octalzeroes; print $(( 08#77 )))
156 0:octalzeroes doesn't affect bases
160 0:bases up to 36 work
164 1:bases beyond 36 don't work
165 ?(eval):1: invalid base (must be 2 to 36 inclusive): 37
167 print $(( 3 + "fail" ))
168 1:parse failure in arithmetic
169 ?(eval):1: bad math expression: operand expected at `"fail" '
172 print $(( 3 + "OK"); echo "Worked")
173 0:not a parse failure because not arithmetic
178 print $(( [#16] 255 ))
179 print $(( [##16] 255 ))
181 print $(( [#16] 255 ))
182 print $(( [##16] 255 ))
185 0:doubled # in base removes radix