Debian Codegraph @

gcc/m2/gm2-libs-log/RealConversions.mod

raw
1 (* RealConversions.mod provides a Logitech-3.0 compatible module.
2
3 Copyright (C) 2005-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE RealConversions ;
28
29 FROM DynamicStrings IMPORT String, InitString, KillString, CopyOut, Length,
30 ConCat, ConCatChar, Mark, RemoveWhitePrefix,
31 InitStringChar, Mult, Slice, Index, char, string,
32 InitStringDB, InitStringCharStarDB,
33 InitStringCharDB, MultDB, DupDB, SliceDB ;
34
35 FROM StringConvert IMPORT LongrealToString, StringToLongreal,
36 StringToInteger, itos ;
37
38 FROM ASCII IMPORT nul ;
39 FROM Builtins IMPORT logl, log10l ;
40 FROM libm IMPORT powl ;
41 FROM libc IMPORT printf ;
42
43
44 CONST
45 Debugging = FALSE ;
46 DefaultExponentDigits = 0 ;
47
48 VAR
49 ExponentDigits: CARDINAL ;
50
51 (*
52 #define InitString(X) InitStringDB(X, __FILE__, __LINE__)
53 #define InitStringCharStar(X) InitStringCharStarDB(X, __FILE__, __LINE__)
54 #define InitStringChar(X) InitStringCharDB(X, __FILE__, __LINE__)
55 #define Mult(X,Y) MultDB(X, Y, __FILE__, __LINE__)
56 #define Dup(X) DupDB(X, __FILE__, __LINE__)
57 #define Slice(X,Y,Z) SliceDB(X, Y, Z, __FILE__, __LINE__)
58 *)
59
60
61 (*
62 IsNan - return TRUE if x is a nan (which never are equal to themselves).
63 *)
64
65 PROCEDURE IsNan (x: LONGREAL) : BOOLEAN ;
66 BEGIN
67 RETURN x # x
68 END IsNan ;
69
70
71 (*
72 logl10 - this is a local implementation of log10l, currently the ppe64le
73 builtin log10l is broken.
74 *)
75
76 PROCEDURE logl10 (r: LONGREAL) : LONGREAL ;
77 BEGIN
78 IF Debugging
79 THEN
80 printf ("logl10 (%lf) = %lf, logl/logl(10.0) = %lf\n",
81 r, log10l (r), logl(r)/logl(10.0))
82 END ;
83 RETURN logl(r)/logl(10.0)
84 END logl10 ;
85
86
87 (*
88 logi10 -
89 *)
90
91 PROCEDURE logi10 (i: INTEGER) : INTEGER ;
92 VAR
93 j: INTEGER ;
94 BEGIN
95 j := 0 ;
96 IF i<0
97 THEN
98 WHILE i<-9 DO
99 DEC(j) ;
100 i := i DIV 10
101 END
102 ELSE
103 WHILE i>9 DO
104 INC(j) ;
105 i := i DIV 10
106 END
107 END ;
108 RETURN j
109 END logi10 ;
110
111
112 (*
113 powl10 -
114 *)
115
116 PROCEDURE powl10 (i: INTEGER) : LONGREAL ;
117 VAR
118 r: LONGREAL ;
119 BEGIN
120 r := 1.0 ;
121 IF i<0
122 THEN
123 WHILE i<0 DO
124 r := r/10.0 ;
125 INC(i)
126 END
127 ELSE
128 WHILE i>0 DO
129 r := r*10.0 ;
130 DEC(i)
131 END
132 END ;
133 RETURN r
134 END powl10 ;
135
136
137 (*
138 doPowerOfTen - safely returns the exponent of a LONGREAL as an INTEGER.
139 *)
140
141 PROCEDURE doPowerOfTen (r: LONGREAL) : INTEGER ;
142 VAR
143 i : INTEGER ;
144 c, d: LONGREAL ;
145 BEGIN
146 IF r=0.0
147 THEN
148 RETURN( 0 )
149 ELSE
150 IF r<0.0
151 THEN
152 c := -r
153 ELSE
154 c := r
155 END ;
156 IF c>=1.0
157 THEN
158 RETURN VAL (INTEGER, log10l (c))
159 ELSE
160 RETURN VAL (INTEGER, log10l (c)) -1
161 END
162 END
163 END doPowerOfTen ;
164
165
166 (*
167 SetNoOfExponentDigits - sets the number of exponent digits to be
168 used during future calls of LongRealToString
169 and RealToString providing that the width
170 is sufficient.
171 If this value is set to 0 (the default) then
172 the number digits used is the minimum necessary.
173 *)
174
175 PROCEDURE SetNoOfExponentDigits (places: CARDINAL) ;
176 BEGIN
177 ExponentDigits := places
178 END SetNoOfExponentDigits ;
179
180
181 (*
182 Pad - prefixes spaces in front of, s, so that width characters are used.
183 *)
184
185 PROCEDURE Pad (s: String; width: CARDINAL) : String ;
186 BEGIN
187 IF Length(s)<width
188 THEN
189 RETURN( ConCat(Mult(InitStringChar(' '), width-Length(s)), Mark(s)) )
190 ELSE
191 RETURN( s )
192 END
193 END Pad ;
194
195
196 (*
197 MakeNanString -
198 *)
199
200 PROCEDURE MakeNanString (VAR str: ARRAY OF CHAR; width: CARDINAL) ;
201 VAR
202 s: String ;
203 BEGIN
204 s := Pad(InitString('nan'), width) ;
205 IF Length(s)<=width
206 THEN
207 CopyOut(str, s)
208 ELSE
209 str[0] := nul
210 END ;
211 s := KillString(s)
212 END MakeNanString ;
213
214
215 (*
216 RealToString - converts a real, r, into a right justified string, str.
217 The number of digits to the right of the decimal point
218 is given in, digits. The value, width, represents the
219 maximum number of characters to be used in the string,
220 str.
221
222 If digits is negative then exponental notation is used
223 whereas if digits is positive then fixed point notation
224 is used.
225
226 If, r, is less than 0.0 then a '-' preceeds the value,
227 str. However, if, r, is >= 0.0 a '+' is not added.
228
229 If the conversion of, r, to a string requires more
230 than, width, characters then the string, str, is set
231 to a nul string and, ok is assigned FALSE.
232
233 For fixed point notation the minimum width required is
234 ABS(width)+8
235
236 For exponent notation the minimum width required is
237 ABS(digits)+2+log10(magnitude).
238
239 if r is a NaN then the string 'nan' is returned formatted
240 and ok will be FALSE.
241 *)
242
243 PROCEDURE RealToString (r: REAL; digits, width: INTEGER;
244 VAR str: ARRAY OF CHAR; VAR ok: BOOLEAN) ;
245 VAR
246 l: LONGREAL ;
247 BEGIN
248 (* --fixme-- *)
249 (* IF IsNan(r)
250 THEN
251 ok := FALSE ;
252 MakeNanString(str, width) ;
253 ELSE
254 ...
255 END
256 *)
257 l := VAL(LONGREAL, r) ;
258 LongRealToString(l, digits, width, str, ok)
259 END RealToString ;
260
261
262 (*
263 doLongRealToString -
264 *)
265
266 PROCEDURE doLongRealToString (r: LONGREAL; digits, width, powerOfTen: INTEGER; VAR ok: BOOLEAN) : String ;
267 VAR
268 sign, s , e : String ;
269 point, len: INTEGER ;
270 BEGIN
271 IF digits>0
272 THEN
273 ok := TRUE ;
274 RETURN( Slice(Mark(LongrealToString(r, width, digits)), 0, width) )
275 ELSE
276 digits := ABS(digits) ;
277 IF r>=0.0
278 THEN
279 sign := InitString('')
280 ELSE
281 sign := InitString('-') ;
282 r := -r
283 END ;
284 s := InitString('') ;
285 r := r*powl10(-powerOfTen) ;
286 IF width>=VAL(INTEGER, Length(s))+2
287 THEN
288 s := ConCat(s, Mark(RemoveWhitePrefix(Mark(LongrealToString(r, width+1, width))))) ;
289 IF Debugging
290 THEN
291 printf('value returned was %s\n', string(s))
292 END ;
293 point := Index(s, '.', 0) ;
294 IF point>=0
295 THEN
296 (* remove the '.' *)
297 s := ConCat(Slice(Mark(s), 0, point), Mark(Slice(Mark(s), point+1, 0))) ;
298 s := Slice(Mark(s), 0, width) ;
299 IF Debugging
300 THEN
301 printf('value returned was %s\n', string(s))
302 END ;
303 point := powerOfTen ;
304 (* now strip off trailing '0's *)
305 WHILE (Length(s)>2) AND (char(s, -1)='0') DO
306 s := Slice(Mark(s), 0, -1)
307 END ;
308 len := Length(s) ;
309 IF Debugging
310 THEN
311 printf('point = %d, powerOfTen = %d, len = %d, len-point = %d\n',
312 point, powerOfTen, len, len-point) ;
313 printf('value returned was %s\n', string(s))
314 END ;
315 WHILE len<2 DO
316 s := ConCat(s, Mark(InitString('0'))) ;
317 len := Length(s)
318 END ;
319 point := 1 ;
320 IF digits>width-point-2
321 THEN
322 (* need to round the result *)
323 digits := width-point-2
324 END ;
325 s := ConCat(Slice(s, 0, point),
326 Mark(ConCat(InitStringChar('.'),
327 Mark(Slice(Mark(s), point, point+digits))))) ;
328 IF Debugging
329 THEN
330 printf("value returned was '%s'\n", string(s))
331 END ;
332 (* and add trailing '0's if needed *)
333 IF VAL(INTEGER, Length(s))-point<digits+1
334 THEN
335 s := ConCat(s, Mark(Mult(Mark(InitString('0')), digits+1-(VAL(INTEGER, Length(s))-point)))) ;
336 IF Debugging
337 THEN
338 printf("value returned was '%s'\n", string(s))
339 END
340 END ;
341 IF powerOfTen-point+1>=0
342 THEN
343 e := ConCat(InitString('E+'),
344 Mark(itos(powerOfTen-point+1, ExponentDigits, '0', FALSE)))
345 ELSE
346 e := ConCat(InitString('E-'),
347 Mark(itos(ABS(powerOfTen-point+1), ExponentDigits, '0', FALSE)))
348 END ;
349 IF Debugging
350 THEN
351 printf("value returned was '%s' and '%s'\n", string(s), string(e))
352 END
353 END ;
354 s := ConCat(sign, Mark(ConCat(s, Mark(e)))) ;
355 ok := TRUE
356 ELSE
357 s := InitString('') ;
358 ok := FALSE
359 END
360 END ;
361 RETURN( s )
362 END doLongRealToString ;
363
364
365 (*
366 LongRealToString - converts a real, r, into a right justified string, str.
367 The number of digits to the right of the decimal point
368 is given in, digits. The value, width, represents the
369 maximum number of characters to be used in the string,
370 str.
371
372 If digits is negative then exponent notation is used
373 whereas if digits is positive then fixed point notation
374 is used.
375
376 If, r, is less than 0.0 then a '-' preceeds the value,
377 str. However, if, r, is >= 0.0 a '+' is not added.
378
379 If the conversion of, r, to a string requires more
380 than, width, characters then the string, str, is set
381 to a nul string and, ok is assigned FALSE.
382
383 For fixed point notation the minimum width required is
384 ABS(width)+8
385
386 For exponent notation the minimum width required is
387 ABS(digits)+2+log10(magnitude).
388
389 Examples:
390 RealToString(100.0, 10, 10, a, ok) -> '100.000000'
391 RealToString(100.0, -5, 12, a, ok) -> ' 1.00000E+2'
392
393 RealToString(123.456789, 10, 10, a, ok) -> '123.456789'
394 RealToString(123.456789, -5, 13, a, ok) -> ' 1.23456E+2'
395
396 RealToString(123.456789, -2, 15, a, ok) -> ' 1.23E+2'
397
398 if r is a NaN then the string 'nan' is returned formatted and
399 ok will be FALSE.
400 *)
401
402 PROCEDURE LongRealToString (r: LONGREAL; digits, width: INTEGER;
403 VAR str: ARRAY OF CHAR; VAR ok: BOOLEAN) ;
404 VAR
405 s : String ;
406 powerOfTen: INTEGER ;
407 BEGIN
408 IF IsNan (r)
409 THEN
410 ok := FALSE ;
411 MakeNanString (str, width) ;
412 RETURN
413 END ;
414 powerOfTen := doPowerOfTen(r) ;
415 IF (powerOfTen=MAX(INTEGER)) OR (powerOfTen=MIN(INTEGER))
416 THEN
417 ok := FALSE ;
418 MakeNanString(str, width) ;
419 RETURN
420 END ;
421 s := doLongRealToString(r, digits, width, powerOfTen, ok) ;
422 ok := TRUE ;
423 IF VAL(INTEGER, Length(s))<=width
424 THEN
425 s := ConCat(Mult(Mark(InitStringChar(' ')), width-VAL(INTEGER, Length(s))), Mark(s)) ;
426 IF Debugging
427 THEN
428 printf('value returned was %s\n', string(s))
429 END ;
430 CopyOut(str, s)
431 ELSE
432 str[0] := nul ;
433 ok := FALSE
434 END ;
435 s := KillString(s)
436 END LongRealToString ;
437
438
439 (*
440 StringToLongReal - converts, str, into a LONGREAL, r. The parameter, ok, is
441 set to TRUE if the conversion was successful.
442 *)
443
444 PROCEDURE StringToLongReal (str: ARRAY OF CHAR; VAR r: LONGREAL; VAR ok: BOOLEAN) ;
445 VAR
446 s: String ;
447 BEGIN
448 s := InitString(str) ;
449 r := StringToLongreal(s, ok) ;
450 s := KillString(s)
451 END StringToLongReal ;
452
453
454 (*
455 StringToReal - converts, str, into a REAL, r. The parameter, ok, is
456 set to TRUE if the conversion was successful.
457 *)
458
459 PROCEDURE StringToReal (str: ARRAY OF CHAR; VAR r: REAL; VAR ok: BOOLEAN) ;
460 VAR
461 l: LONGREAL ;
462 BEGIN
463 StringToLongReal(str, l, ok) ;
464 IF ok
465 THEN
466 r := VAL(REAL, l)
467 END
468 END StringToReal ;
469
470
471 BEGIN
472 ExponentDigits := DefaultExponentDigits
473 END RealConversions.