Debian Codegraph @

trip/arith.rexx

raw
1 /*
2 * The Regina Rexx Interpreter
3 * Copyright (C) 1992 Anders Christensen <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 written = 0
21 towhere = ''
22 zero = 0
23
24 /* ======================= simple arithmetic ======================== */
25
26 call notify 'adding'
27
28 call ch 0.00 + 2, "2"
29 call ch 2.00 + 0, "2.00"
30 call ch 0.00 + 0.00, "0"
31 call ch 4.6 + 5.4, "10.0"
32 call ch 0. + .0, "0"
33 call ch "-1.0" + 1.00, "0"
34 call ch "-1.1" + 1, "-0.1"
35 call ch 3 + 4, "7"
36 call ch 0 + 0, "0"
37 call ch 3 + "- 4", "-1"
38 call ch " - 7 " + " + 3 ", "-4"
39
40 call ch "1.000000001" + "1.000000001", "2.00000000"
41 call ch "1.00" + "0.0000001", "1.0000001"
42 call ch "1.00" + "0.00000001", "1.00000001"
43 call ch "1.00" + "0.000000001", "1.00000000"
44 call ch "1.00" + "0.0000000001", "1.00000000"
45 call ch "1.00" + "0.00000000001", "1.00000000"
46 call ch "0.003" + "0.0000004", "0.0030004"
47
48 call ch "1.000000006" + "1.000000006", "2.00000001"
49 call ch "1.000000043" + "1.000000003", "2.00000005"
50 call ch "8.999999994" + "1.000000004", "10.0000000"
51 call ch "1.0000000006" + "1.0000000006", "2.00000000"
52 call ch "1.000000006" + "1.000000006", "2.00000001"
53 call ch "1.000000006" + "1.000000006", "2.00000001"
54
55 call ch "999999" + ".999999", "1000000.00"
56 call ch "999999000000" + "999999", "1.00000000E+12"
57
58
59 call notify 'multiplication'
60 call ch '5.00' * '4', '20.00'
61 call ch '3.14' * '4', "12.56"
62 call ch '1' * '1', '1'
63 call ch '.1' * '.1', '0.01'
64 call ch '.11' * '.11', '0.0121'
65 call ch '0' * '0', '0'
66
67 call ch '1.00001' * '1.0001', '1.00011000'
68 call ch '1.00001' * '1.00001', '1.00002000'
69 call ch '1.000001' * '1.000001', '1.00000200'
70 call ch '1.0000001' * '1.0000001', '1.00000020'
71 call ch '1.00000001' * '1.00000001', '1.00000002'
72 call ch '1.000000001' * '1.000000001', '1.00000000'
73 call ch '1.0000000001' * '1.0000000001', '1.00000000'
74 call ch '1.00000000001' * '1.00000000001', '1.00000000'
75
76 call ch '0.001' * '0.001', '0.000001'
77 call ch '0.0001' * '0.0001', '1E-8'
78 call ch '0.00001' * '0.00001', '1E-10'
79 call ch '0.000001' * '0.000001', '1E-12'
80 call ch '0.0000001' * '0.0000001', '1E-14'
81 call ch '0.00000001' * '0.00000001', '1E-16'
82 call ch '0.000000001' * '0.000000001', '1E-18'
83 call ch '0.0000000001' * '0.0000000001', '1E-20'
84 call ch '0.00000000001' * '0.00000000001', '1E-22'
85
86 call ch '123456' * '789012', '9.74082655E+10'
87 call ch '987654321' * '123456789', '1.21932631E+17'
88
89
90 call notify 'division'
91 call ch 30 / 5, 6
92 call ch 3 / 5, 0.6
93 call ch 0.3 / 5, 0.06
94 call ch 0.3 / 0.5, 0.6
95 call ch 9 / 3, 3
96 call ch 4 / 2, 2
97 call ch 2 / 4, 0.5
98 call ch 10 / 1, 10
99 call ch 11 / 1, 11
100 call ch 900 / 3, 300
101 call ch 963 / 3, 321
102 call ch 3 / 2, 1.5
103
104 call ch 10 / 3, 3.33333333
105 call ch 10 / 7, 1.42857143
106 call ch 123.456 / 7, 17.6365714
107 call ch 7 / 123.456, 0.0567003629
108
109 call ch 4 / 12, 0.333333333
110 call ch 3.69 / 1.23, 3
111 call ch 4.00 / 2, 2
112
113 signal on syntax
114 towhere = 'after_div'
115 call ch 4.00/zero, 123
116 call complain 'Didn''t catch division by zero in normal division'
117 after_div: towhere=''
118
119 call notify 'modulus'
120 call ch 7 % 3, 2
121 call ch 8 % 3, 2
122 call ch 9 % 3, 3
123 call ch 10 % 3, 3
124 call ch 11 % 3, 3
125 call ch 12 % 3, 4
126
127 call ch 4 % 3, 1
128 call ch 3 % 3, 1
129 call ch 2 % 3, 0
130 call ch 1 % 3, 0
131 call ch 0 % 3, 0
132 call ch -1 % 3, 0
133 call ch -2 % 3, 0
134 call ch -3 % 3, -1
135 call ch -4 % 3, -1
136
137 call ch 818 % 9, 90
138
139 signal on syntax
140 towhere = 'after_mod'
141 call ch 4.00%zero, 123
142 call complain 'Didn''t catch division by zero in integer division'
143 after_mod: towhere=''
144
145
146 call notify 'remainder'
147 call ch 7 // 3, 1
148 call ch 8 // 3, 2
149 call ch 9 // 3, 0
150 call ch 10 // 3, 1
151 call ch 11 // 3, 2
152 call ch 12 // 3, 0
153
154 call ch 4 // 3, 1
155 call ch 3 // 3, 0
156 call ch 2 // 3, 2
157 call ch 1 // 3, 1
158 call ch 0 // 3, 0
159 call ch -1 // 3, -1
160 call ch -2 // 3, -2
161 call ch -3 // 3, 0
162 call ch -4 // 3, -1
163
164 call ch -2.57 // 1.2, -0.17
165 call ch 12.34 // 1, 0.34
166 call ch 12.34 // 2, 0.34
167 call ch 12.34 // 3, 0.34
168 call ch 12.34 // 4, 0.34
169 call ch 12.34 // 5, 2.34
170 call ch 12.34 // 6, 0.34
171 call ch 12.34 // 7, 5.34
172 call ch 12.34 // 8, 4.34
173 call ch 12.34 // 9, 3.34
174 call ch 12.34 // 10, 2.34
175 call ch 12.34 // 11, 1.34
176 call ch 12.34 // 12, 0.34
177 call ch 12.34 // 13, 12.34
178 call ch 12.34 // 14, 12.34
179 call ch 12.34 // 15, 12.34
180 call ch 12.34 // 16, 12.34
181
182 signal on syntax
183 towhere = 'after_rem'
184 call ch 4.00//zero, 123
185 call complain 'Didn''t catch division by zero in reminder division'
186 after_rem: towhere=''
187
188
189 call notify 'power'
190 call ch 2 ** (-4), 0.0625
191 call ch 2 ** (-3), 0.125
192 call ch 2 ** (-2), 0.25
193 call ch 2 ** (-1), 0.5
194 call ch 2 ** 0, 1
195 call ch 2 ** 1, 2
196 call ch 2 ** 2, 4
197 call ch 2 ** 3, 8
198 call ch 2 ** 4, 16
199 call ch 2 ** 5, 32
200
201 call ch 2.2 ** 1, 2.2
202 call ch 2.2 ** 2, 4.84
203
204
205 call notify 'exponential'
206 call ch 1000 * 1000, 1000000
207 call ch 1000 * 100000, 100000000
208 call ch 1000 * 1000000, 1.00000000E+9
209 call ch 1000 * 10000000, 1.00000000E+10
210
211
212 call notify 'numeric'
213 numeric digits 1
214 a = ''
215 do i=8 to 15 for 9
216 a = a i
217 end
218
219 j = 8 * 8
220 numeric digits
221 call ch a, ' 8 9 1E+1 1E+1 1E+1 1E+1 1E+1 1E+1 1E+1'
222 call ch i, '1E+1'
223 call ch j, '6E+1'
224
225 say ' '
226
227
228 exit 0
229
230
231 ch: procedure expose sigl
232 parse arg first, second
233 if first \== second then do
234 say
235 say 'first = /'first'/'
236 say 'second = /'second'/'
237 say "FuncTrip: error in " sigl":" sourceline(sigl) ; end
238 return
239
240
241 notify:
242 parse arg word .
243 written = written + length(word) + 2
244 if written>75 then do
245 written = length(word)
246 say ' '
247 end
248 call charout , word || ', '
249 return
250
251
252 error:
253 say 'Error discovered in function insert()'
254 return
255
256
257 syntax:
258 if towhere\='' then
259 interpret 'signal' towhere
260
261 say 'Syntax error' rc 'in line' sigl
262 say sourceline( sigl )
263 exit