summaryrefslogtreecommitdiff
path: root/system/base/unknown/src/real
blob: a2ab9c395de7f9cf3214263f378069a8a0e55648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
 
PACKET real DEFINES                            (* Autor: J.Liedtke *) 
                                               (* Stand: 30.04.80  *) 
   text , 
   int ,
   real ,
   round ,
   floor , 
   frac ,
   INCR ,
   DECR ,
   abs , 
   ABS , 
   sign ,
   SIGN ,
   MOD ,
   min ,
   max ,
   put , 
   get , 
   max real ,
   small real :

LET mantissa length = 13 ;

TEXT VAR mantissa ;

ROW 10 REAL VAR real digit ;
 
INT VAR i ; REAL VAR d := 0.0 ;
FOR i FROM 1 UPTO 10 REP
  real digit (i) := d ;
  d := d + 1.0
PER ;
 
REAL PROC max real : 9.999999999999e126 ENDPROC max real ;
 
REAL PROC small real : 1.0e-12 ENDPROC small real ;
 
PROC sld (INT CONST in, REAL VAR real, INT VAR out) :
  EXTERNAL 96 
ENDPROC sld ; 
 
INT PROC decimal exponent (REAL CONST mantissa) : 
  EXTERNAL 97 
ENDPROC decimal exponent ; 
 
PROC set exp (INT CONST exponent, REAL VAR number) : 
  EXTERNAL 98 
ENDPROC set exp ; 
 
REAL PROC tenpower (INT CONST exponent) :
  REAL VAR result := 1.0 ;
  set exp (exponent, result) ;
  result
ENDPROC tenpower ;

REAL PROC floor (REAL CONST real) : 
  EXTERNAL 99 
ENDPROC floor ; 
 
REAL PROC round (REAL CONST real, INT CONST digits) : 
 
  REAL VAR result := real ;
  IF (real <> 0.0) CAND (decimal exponent (real) + digits < mantissa length)
    THEN round result ;
  FI ;
  result .

round result :
  set exp (decimal exponent (result) + digits, result) ;
  IF result >= 0.0
    THEN result := floor (result + 0.5)
    ELSE result := floor (result - 0.5)
  FI ;
  set exp (decimal exponent (result) - digits, result) .

ENDPROC round ; 
 
TEXT VAR result ;
 
TEXT PROC text (REAL CONST real) : 
 
  REAL VAR value := rounded to seven digits ; 
  IF value = 0.0 
    THEN "0.0" 
    ELSE 
      process sign ; 
      get mantissa (value) ; 
      INT CONST exponent := decimal exponent (value) ; 
      get short mantissa ; 
      IF exponent > 7 OR exponent < LENGTH short mantissa - 7 
        THEN scientific notation 
        ELSE short notation 
      FI 
    FI . 
 
rounded to seven digits : 
  round ( real * tenpower( -decimal exponent(real) ) , 6 ) 
        * tenpower ( decimal exponent(real) ) . 
 
process sign : 
  IF value < 0.0 
    THEN result := "-" ; 
         value := - value 
    ELSE result := "" 
  FI . 
 
get short mantissa : 
  INT VAR i := 7 ; 
  WHILE (mantissa SUB i) = "0" REP 
    i DECR 1 
  UNTIL i=1 END REP ; 
  TEXT CONST short mantissa := subtext (mantissa, 1, i) . 
 
scientific notation : 
  result CAT (mantissa SUB 1) ; 
  result CAT "." ; 
  result CAT subtext (mantissa, 2, 7) ; 
  result + "e" + text (exponent) . 
 
short notation : 
  result CAT subtext (short mantissa, 1, exponent+1) ; 
  result CAT (exponent+1 - LENGTH short mantissa) * "0" ; 
  result CAT "." ; 
  IF exponent < 0 
    THEN result + (-exponent-1) * "0" + short mantissa 
    ELSE result + subtext (short mantissa, exponent+2) 
  FI . 
 
ENDPROC text ; 
 
PROC get mantissa (REAL CONST number) :

  REAL VAR real mantissa := number ;
  mantissa := "" ;
  INT VAR i , digit ;
  FOR i FROM 1 UPTO mantissa length REP
    sld (0, real mantissa, digit) ;
    mantissa CAT code (digit + 48)
  PER ;

ENDPROC get mantissa ;

PROC put (REAL CONST real) : 
 
  put (text (real) ) 
 
ENDPROC put ; 
 
TEXT PROC text (REAL CONST real, INT CONST length, fracs) :
 
  REAL VAR value := round (real, fracs) ;
  INT VAR  exponent  := decimal exponent (value) ;
           IF value = 0.0 THEN exponent := 0 FI ;
  INT VAR  floors    := exponent + 1 ,
           floor length := length - fracs - 1 ;
           IF value < 0.0 THEN floor length DECR 1 FI ;
 
  IF value too big
    THEN length * "*"
    ELSE transformed value
  FI .
 
transformed value :
  process leading blanks and sign ;
  get mantissa (value) ;
  result CAT subtext (mantissa, 1, floors) ;
  IF LENGTH mantissa < floors
    THEN result CAT (floors - LENGTH mantissa) * "0"
  FI ;
  result CAT "." ;
  IF exponent < 0
    THEN result CAT (-floors) * "0" ;
         result CAT subtext (mantissa, 1, length - LENGTH result)
    ELSE result CAT subtext (mantissa, floors+1, floors + fracs)
  FI ;
  IF LENGTH result < length
    THEN result CAT (length - LENGTH result) * "0"
  FI ;
  result .
 
process leading blanks and sign :
  result := (floor length - max(floors,0)) * " " ;
  IF value < 0.0
    THEN result CAT "-" ;
         value := - value
  FI .
 
value too big :
  floors > floor length .
 
ENDPROC text ;
 
REAL PROC real (TEXT CONST text) : 
 
  skip leading blanks ;
  sign ; 
  mantissa part ; 
  exponent ; 
  check correct conversion ;
  result . 
 
skip leading blanks :
  INT VAR pos := 1 ;
  skip blanks .
 
skip blanks :
  WHILE (text SUB pos) = " " REP
    pos INCR 1
  PER .
 
sign : 
  BOOL VAR negative ; 
  IF (text SUB pos) = "-" 
    THEN negative := TRUE ; 
         pos INCR 1
  ELIF (text SUB pos) = "+" 
    THEN negative := FALSE ; 
         pos INCR 1
  ELSE   negative := FALSE
  FI . 
 
mantissa part: 
  REAL VAR value := 0.0 ; 
  INT VAR exponent pos := 0 ;
  WHILE pos <= LENGTH text REP 
    TEXT VAR digit := text SUB pos ; 
    IF digit <= "9" AND digit >= "0" 
      THEN value := value * 10.0 + real digit (code (digit) - 47) ;
           pos INCR 1 
    ELIF digit = "." 
      THEN pos INCR 1 ;
           exponent pos := pos
    ELSE LEAVE mantissa part 
    FI 
  END REP . 
 
exponent : 
  INT VAR exp ;
  IF exponent pos > 0
    THEN exp := exponent pos - pos
    ELSE exp := 0
  FI ;
  IF (text SUB pos) = "e" 
    THEN exp INCR int (subtext(text,pos+1)) 
  FI . 
 
check correct conversion :
  skip blanks ;
  IF pos > LENGTH text
    THEN set conversion (TRUE)
    ELSE set conversion (FALSE)
  FI .
 
result : 
  value := value * tenpower (exp) ; 
  IF negative 
    THEN - value 
    ELSE value 
  FI . 
 
ENDPROC real ; 
 
TEXT VAR word ;
 
PROC get (REAL VAR value) : 
 
  get (word) ; 
  value := real (word) 
 
ENDPROC get ; 
 
REAL PROC abs (REAL CONST value) : 
 
  IF value >= 0.0 
    THEN  value 
    ELSE -value 
  FI 
 
ENDPROC abs ; 
 
REAL OP ABS (REAL CONST value) : 
 
  abs (value) 
 
ENDOP ABS ; 
 
INT PROC sign (REAL CONST value) :
 
  IF   value < 0.0 THEN -1
  ELIF value = 0.0 THEN  0
  ELSE                   1
  FI
 
ENDPROC sign ;
 
INT OP SIGN (REAL CONST value) :
 
  sign (value)
 
ENDOP SIGN ;
 
REAL OP MOD (REAL CONST left, right) :
 
  REAL VAR result := left - floor (left/right) * right ;
  IF left < 0.0
    THEN result + abs (right)
    ELSE result
  FI
 
ENDOP MOD ;
 
REAL PROC frac (REAL CONST value) :
 
  value - floor (value)
 
ENDPROC frac ;
 
REAL PROC max (REAL CONST a, b) :
 
  IF a > b THEN a ELSE b FI
 
ENDPROC max ;
 
REAL PROC min (REAL CONST a, b) :
 
  IF a < b THEN a ELSE b FI
 
ENDPROC min ;
 
OP INCR (REAL VAR dest, REAL CONST increment) :
 
  dest := dest + increment
 
ENDOP INCR ;
 
OP DECR (REAL VAR dest, REAL CONST decrement) : 
 
  dest := dest - decrement
 
ENDOP DECR ;
 
INT PROC int (REAL CONST value) :
 
  INT VAR result := 0, digit ,i ;
  REAL VAR mantissa := value ;
 
  FOR i FROM 0 UPTO decimal exponent (value) REP
    sld (0, mantissa, digit) ;
    result := result * 10 + digit 
  PER ;
 
  IF value < 0.0
    THEN - result
    ELSE   result
  FI
 
ENDPROC int ;
 
REAL PROC real (INT CONST value) :
 
  IF value < 0
    THEN - real (-value)
  ELIF value < 10
    THEN real digit (value+1)
    ELSE split value into head and last digit ;
         real (head) * 10.0 + real digit (last digit+1)
  FI .
 
split value into head and last digit :
  INT CONST
  head := value DIV 10 ,
  last digit := value - head * 10 .
 
ENDPROC real ;
 
ENDPACKET real ;