1988-02-19 15:54:01 +00:00
|
|
|
(*
|
|
|
|
(c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*)
|
|
|
|
|
1987-08-19 18:07:01 +00:00
|
|
|
(*$R-*)
|
1987-05-22 17:15:09 +00:00
|
|
|
IMPLEMENTATION MODULE RealConversions;
|
1988-02-19 15:54:01 +00:00
|
|
|
(*
|
|
|
|
Module: string-to-real and real-to-string conversions
|
|
|
|
Author: Ceriel J.H. Jacobs
|
1994-06-24 14:02:31 +00:00
|
|
|
Version: $Id$
|
1988-02-19 15:54:01 +00:00
|
|
|
*)
|
1987-05-22 17:15:09 +00:00
|
|
|
|
|
|
|
|
1987-05-27 10:05:01 +00:00
|
|
|
PROCEDURE RealToString(arg: REAL;
|
|
|
|
width, digits: INTEGER;
|
|
|
|
VAR str: ARRAY OF CHAR;
|
|
|
|
VAR ok: BOOLEAN);
|
|
|
|
BEGIN
|
|
|
|
LongRealToString(LONG(arg), width, digits, str, ok);
|
|
|
|
END RealToString;
|
|
|
|
|
1988-08-10 11:12:57 +00:00
|
|
|
TYPE
|
|
|
|
Powers = RECORD
|
|
|
|
pval: LONGREAL;
|
|
|
|
rpval: LONGREAL;
|
|
|
|
exp: INTEGER
|
|
|
|
END;
|
|
|
|
|
|
|
|
VAR Powers10: ARRAY[1..6] OF Powers;
|
|
|
|
|
1987-05-27 10:05:01 +00:00
|
|
|
PROCEDURE LongRealToString(arg: LONGREAL;
|
1987-05-22 17:15:09 +00:00
|
|
|
width, digits: INTEGER;
|
|
|
|
VAR str: ARRAY OF CHAR;
|
|
|
|
VAR ok: BOOLEAN);
|
|
|
|
VAR pointpos: INTEGER;
|
|
|
|
i: CARDINAL;
|
|
|
|
ecvtflag: BOOLEAN;
|
1988-08-10 11:12:57 +00:00
|
|
|
r: LONGREAL;
|
1987-05-22 17:15:09 +00:00
|
|
|
ind1, ind2 : CARDINAL;
|
|
|
|
sign: BOOLEAN;
|
|
|
|
ndigits: CARDINAL;
|
|
|
|
|
|
|
|
BEGIN
|
1987-05-27 10:05:01 +00:00
|
|
|
r := arg;
|
1987-05-22 17:15:09 +00:00
|
|
|
IF digits < 0 THEN
|
|
|
|
ecvtflag := TRUE;
|
|
|
|
ndigits := -digits;
|
|
|
|
ELSE
|
|
|
|
ecvtflag := FALSE;
|
|
|
|
ndigits := digits;
|
|
|
|
END;
|
1987-06-29 12:27:50 +00:00
|
|
|
IF (HIGH(str) < ndigits + 3) THEN
|
|
|
|
str[0] := 0C; ok := FALSE; RETURN
|
|
|
|
END;
|
1987-05-22 17:15:09 +00:00
|
|
|
pointpos := 0;
|
1987-05-27 10:05:01 +00:00
|
|
|
sign := r < 0.0D;
|
1987-05-22 17:15:09 +00:00
|
|
|
IF sign THEN r := -r END;
|
1993-02-24 15:12:49 +00:00
|
|
|
ok := TRUE;
|
1997-01-27 14:06:51 +00:00
|
|
|
IF (r <> 0.0D) AND NOT (r / 10.0D < r) THEN
|
1993-02-24 15:12:49 +00:00
|
|
|
(* assume Nan or Infinity *)
|
|
|
|
r := 0.0D;
|
|
|
|
ok := FALSE;
|
|
|
|
END;
|
1988-08-10 11:12:57 +00:00
|
|
|
IF r # 0.0D THEN
|
|
|
|
IF r >= 10.0D THEN
|
|
|
|
FOR i := 1 TO 6 DO
|
|
|
|
WITH Powers10[i] DO
|
|
|
|
WHILE r >= pval DO
|
|
|
|
r := r * rpval;
|
|
|
|
INC(pointpos, exp)
|
|
|
|
END;
|
1987-05-22 17:15:09 +00:00
|
|
|
END;
|
|
|
|
END;
|
|
|
|
END;
|
1988-08-10 11:12:57 +00:00
|
|
|
IF r < 1.0D THEN
|
|
|
|
FOR i := 1 TO 6 DO
|
|
|
|
WITH Powers10[i] DO
|
|
|
|
WHILE r*pval < 10.0D DO
|
|
|
|
r := r * pval;
|
|
|
|
DEC(pointpos, exp)
|
|
|
|
END;
|
|
|
|
END;
|
1987-05-22 17:15:09 +00:00
|
|
|
END;
|
|
|
|
END;
|
1988-08-10 11:12:57 +00:00
|
|
|
(* Now, we have r in [1.0, 10.0) *)
|
|
|
|
INC(pointpos);
|
1987-05-22 17:15:09 +00:00
|
|
|
END;
|
1988-08-10 11:12:57 +00:00
|
|
|
ind1 := 0;
|
|
|
|
ind2 := ndigits+1;
|
|
|
|
|
1987-05-22 17:15:09 +00:00
|
|
|
IF NOT ecvtflag THEN
|
1990-01-31 11:01:53 +00:00
|
|
|
IF INTEGER(ind2) + pointpos <= 0 THEN
|
|
|
|
ind2 := 1;
|
1987-05-22 17:15:09 +00:00
|
|
|
ELSE
|
|
|
|
ind2 := INTEGER(ind2) + pointpos
|
|
|
|
END;
|
|
|
|
END;
|
|
|
|
IF ind2 > HIGH(str) THEN
|
|
|
|
ok := FALSE;
|
|
|
|
str[0] := 0C;
|
|
|
|
RETURN;
|
|
|
|
END;
|
1988-08-10 11:12:57 +00:00
|
|
|
WHILE ind1 < ind2 DO
|
1987-05-22 17:15:09 +00:00
|
|
|
str[ind1] := CHR(TRUNC(r)+ORD('0'));
|
1988-08-10 11:12:57 +00:00
|
|
|
r := 10.0D * (r - FLOATD(TRUNC(r)));
|
1987-05-22 17:15:09 +00:00
|
|
|
INC(ind1);
|
|
|
|
END;
|
1988-08-10 11:12:57 +00:00
|
|
|
IF ind2 > 0 THEN
|
|
|
|
DEC(ind2);
|
|
|
|
ind1 := ind2;
|
|
|
|
str[ind2] := CHR(ORD(str[ind2])+5);
|
|
|
|
WHILE str[ind2] > '9' DO
|
|
|
|
str[ind2] := '0';
|
|
|
|
IF ind2 > 0 THEN
|
|
|
|
DEC(ind2);
|
|
|
|
str[ind2] := CHR(ORD(str[ind2])+1);
|
|
|
|
ELSE
|
|
|
|
str[ind2] := '1';
|
|
|
|
INC(pointpos);
|
|
|
|
IF NOT ecvtflag THEN
|
|
|
|
IF ind1 > 0 THEN str[ind1] := '0'; END;
|
|
|
|
INC(ind1);
|
|
|
|
END;
|
1987-05-22 17:15:09 +00:00
|
|
|
END;
|
|
|
|
END;
|
1990-01-31 11:01:53 +00:00
|
|
|
IF (NOT ecvtflag) AND (ind1 = 0) THEN
|
|
|
|
str[0] := CHR(ORD(str[0])-5);
|
|
|
|
INC(ind1);
|
|
|
|
END;
|
1987-05-22 17:15:09 +00:00
|
|
|
END;
|
|
|
|
IF ecvtflag THEN
|
|
|
|
FOR i := ind1 TO 2 BY -1 DO
|
|
|
|
str[i] := str[i-1];
|
|
|
|
END;
|
|
|
|
str[1] := '.';
|
|
|
|
INC(ind1);
|
|
|
|
IF sign THEN
|
|
|
|
FOR i := ind1 TO 1 BY -1 DO
|
|
|
|
str[i] := str[i-1];
|
|
|
|
END;
|
|
|
|
INC(ind1);
|
|
|
|
str[0] := '-';
|
|
|
|
END;
|
|
|
|
IF (ind1 + 4) > HIGH(str) THEN
|
|
|
|
str[0] := 0C;
|
|
|
|
ok := FALSE;
|
|
|
|
RETURN;
|
|
|
|
END;
|
|
|
|
str[ind1] := 'E'; INC(ind1);
|
1989-06-20 11:43:01 +00:00
|
|
|
IF arg # 0.0D THEN DEC(pointpos); END;
|
1987-05-22 17:15:09 +00:00
|
|
|
IF pointpos < 0 THEN
|
|
|
|
pointpos := -pointpos;
|
|
|
|
str[ind1] := '-';
|
|
|
|
ELSE
|
|
|
|
str[ind1] := '+';
|
|
|
|
END;
|
|
|
|
INC(ind1);
|
|
|
|
str[ind1] := CHR(ORD('0') + CARDINAL(pointpos DIV 100));
|
|
|
|
pointpos := pointpos MOD 100;
|
|
|
|
INC(ind1);
|
|
|
|
str[ind1] := CHR(ORD('0') + CARDINAL(pointpos DIV 10));
|
|
|
|
INC(ind1);
|
|
|
|
str[ind1] := CHR(ORD('0') + CARDINAL(pointpos MOD 10));
|
|
|
|
ELSE
|
|
|
|
IF pointpos <= 0 THEN
|
|
|
|
FOR i := ind1 TO 1 BY -1 DO
|
|
|
|
str[i+CARDINAL(-pointpos)] := str[i-1];
|
|
|
|
END;
|
|
|
|
FOR i := 0 TO CARDINAL(-pointpos) DO
|
|
|
|
str[i] := '0';
|
|
|
|
END;
|
|
|
|
ind1 := ind1 + CARDINAL(1 - pointpos);
|
|
|
|
pointpos := 1;
|
|
|
|
END;
|
|
|
|
FOR i := ind1 TO CARDINAL(pointpos+1) BY -1 DO
|
|
|
|
str[i] := str[i-1];
|
|
|
|
END;
|
|
|
|
IF ndigits = 0 THEN
|
|
|
|
str[pointpos] := 0C;
|
1990-01-31 11:01:53 +00:00
|
|
|
ind1 := pointpos - 1;
|
1987-05-22 17:15:09 +00:00
|
|
|
ELSE
|
|
|
|
str[pointpos] := '.';
|
1990-01-31 11:01:53 +00:00
|
|
|
IF INTEGER(ind1) > pointpos+INTEGER(ndigits) THEN
|
|
|
|
ind1 := pointpos+INTEGER(ndigits);
|
|
|
|
END;
|
|
|
|
str[pointpos+INTEGER(ndigits)+1] := 0C;
|
1987-05-22 17:15:09 +00:00
|
|
|
END;
|
|
|
|
IF sign THEN
|
|
|
|
FOR i := ind1 TO 0 BY -1 DO
|
|
|
|
str[i+1] := str[i];
|
|
|
|
END;
|
|
|
|
str[0] := '-';
|
|
|
|
INC(ind1);
|
|
|
|
END;
|
|
|
|
END;
|
1987-06-29 12:27:50 +00:00
|
|
|
IF (ind1+1) <= HIGH(str) THEN str[ind1+1] := 0C; END;
|
|
|
|
IF ind1 >= CARDINAL(width) THEN
|
1987-05-22 17:15:09 +00:00
|
|
|
ok := FALSE;
|
|
|
|
RETURN;
|
|
|
|
END;
|
1987-06-29 12:27:50 +00:00
|
|
|
IF width > 0 THEN
|
|
|
|
DEC(width);
|
|
|
|
END;
|
|
|
|
IF (width > 0) AND (ind1 < CARDINAL(width)) THEN
|
1987-05-22 17:15:09 +00:00
|
|
|
FOR i := ind1 TO 0 BY -1 DO
|
|
|
|
str[i + CARDINAL(width) - ind1] := str[i];
|
|
|
|
END;
|
|
|
|
FOR i := 0 TO CARDINAL(width)-(ind1+1) DO
|
|
|
|
str[i] := ' ';
|
|
|
|
END;
|
|
|
|
ind1 := CARDINAL(width);
|
1988-01-28 16:37:55 +00:00
|
|
|
IF (ind1+1) <= HIGH(str) THEN
|
|
|
|
FOR ind1 := ind1+1 TO HIGH(str) DO
|
|
|
|
str[ind1] := 0C;
|
|
|
|
END;
|
|
|
|
END;
|
1987-05-22 17:15:09 +00:00
|
|
|
END;
|
|
|
|
|
1987-05-27 10:05:01 +00:00
|
|
|
END LongRealToString;
|
1987-05-22 17:15:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE StringToReal(str: ARRAY OF CHAR;
|
|
|
|
VAR r: REAL; VAR ok: BOOLEAN);
|
1987-05-27 10:05:01 +00:00
|
|
|
VAR x: LONGREAL;
|
|
|
|
BEGIN
|
|
|
|
StringToLongReal(str, x, ok);
|
|
|
|
IF ok THEN
|
|
|
|
r := x;
|
|
|
|
END;
|
|
|
|
END StringToReal;
|
1987-05-22 17:15:09 +00:00
|
|
|
|
1987-05-27 10:05:01 +00:00
|
|
|
PROCEDURE StringToLongReal(str: ARRAY OF CHAR;
|
|
|
|
VAR r: LONGREAL; VAR ok: BOOLEAN);
|
|
|
|
CONST BIG = 1.0D17;
|
1987-05-22 17:15:09 +00:00
|
|
|
TYPE SETOFCHAR = SET OF CHAR;
|
|
|
|
VAR pow10 : INTEGER;
|
|
|
|
i : INTEGER;
|
1987-05-27 10:05:01 +00:00
|
|
|
e : LONGREAL;
|
1987-05-22 17:15:09 +00:00
|
|
|
ch : CHAR;
|
|
|
|
signed: BOOLEAN;
|
|
|
|
signedexp: BOOLEAN;
|
|
|
|
iB: CARDINAL;
|
|
|
|
|
|
|
|
BEGIN
|
1987-05-27 10:05:01 +00:00
|
|
|
r := 0.0D;
|
1987-05-22 17:15:09 +00:00
|
|
|
pow10 := 0;
|
|
|
|
iB := 0;
|
|
|
|
ok := TRUE;
|
|
|
|
signed := FALSE;
|
|
|
|
WHILE (str[iB] = ' ') OR (str[iB] = CHR(9)) DO
|
|
|
|
INC(iB);
|
|
|
|
IF iB > HIGH(str) THEN
|
|
|
|
ok := FALSE;
|
|
|
|
RETURN;
|
|
|
|
END;
|
|
|
|
END;
|
|
|
|
IF str[iB] = '-' THEN signed := TRUE; INC(iB)
|
|
|
|
ELSIF str[iB] = '+' THEN INC(iB)
|
|
|
|
END;
|
|
|
|
ch := str[iB]; INC(iB);
|
|
|
|
IF NOT (ch IN SETOFCHAR{'0'..'9'}) THEN ok := FALSE; RETURN END;
|
|
|
|
REPEAT
|
1987-06-19 09:25:08 +00:00
|
|
|
IF r>BIG THEN INC(pow10) ELSE r:= 10.0D*r+FLOATD(ORD(ch)-ORD('0')) END;
|
1987-05-22 17:15:09 +00:00
|
|
|
IF iB <= HIGH(str) THEN
|
|
|
|
ch := str[iB]; INC(iB);
|
|
|
|
END;
|
|
|
|
UNTIL (iB > HIGH(str)) OR NOT (ch IN SETOFCHAR{'0'..'9'});
|
|
|
|
IF (ch = '.') AND (iB <= HIGH(str)) THEN
|
|
|
|
ch := str[iB]; INC(iB);
|
|
|
|
IF NOT (ch IN SETOFCHAR{'0'..'9'}) THEN ok := FALSE; RETURN END;
|
|
|
|
REPEAT
|
1987-06-19 09:25:08 +00:00
|
|
|
IF r < BIG THEN
|
|
|
|
r := 10.0D * r + FLOATD(ORD(ch)-ORD('0'));
|
|
|
|
DEC(pow10);
|
|
|
|
END;
|
1987-05-22 17:15:09 +00:00
|
|
|
IF iB <= HIGH(str) THEN
|
|
|
|
ch := str[iB]; INC(iB);
|
|
|
|
END;
|
|
|
|
UNTIL (iB > HIGH(str)) OR NOT (ch IN SETOFCHAR{'0'..'9'});
|
|
|
|
END;
|
1987-06-26 15:59:52 +00:00
|
|
|
IF (ch = 'E') THEN
|
1987-05-22 17:15:09 +00:00
|
|
|
IF iB > HIGH(str) THEN
|
|
|
|
ok := FALSE;
|
|
|
|
RETURN;
|
|
|
|
ELSE
|
|
|
|
ch := str[iB]; INC(iB);
|
|
|
|
END;
|
|
|
|
i := 0;
|
|
|
|
signedexp := FALSE;
|
|
|
|
IF (ch = '-') OR (ch = '+') THEN
|
|
|
|
signedexp := ch = '-';
|
|
|
|
IF iB > HIGH(str) THEN
|
|
|
|
ok := FALSE;
|
|
|
|
RETURN;
|
|
|
|
ELSE
|
|
|
|
ch := str[iB]; INC(iB);
|
|
|
|
END;
|
|
|
|
END;
|
|
|
|
IF NOT (ch IN SETOFCHAR{'0'..'9'}) THEN ok := FALSE; RETURN END;
|
|
|
|
REPEAT
|
|
|
|
i := i*10 + INTEGER(ORD(ch) - ORD('0'));
|
|
|
|
IF iB <= HIGH(str) THEN
|
|
|
|
ch := str[iB]; INC(iB);
|
|
|
|
END;
|
|
|
|
UNTIL (iB > HIGH(str)) OR NOT (ch IN SETOFCHAR{'0'..'9'});
|
|
|
|
IF signedexp THEN i := -i END;
|
|
|
|
pow10 := pow10 + i;
|
|
|
|
END;
|
|
|
|
IF pow10 < 0 THEN i := -pow10; ELSE i := pow10; END;
|
1987-05-27 10:05:01 +00:00
|
|
|
e := 1.0D;
|
1987-05-22 17:15:09 +00:00
|
|
|
DEC(i);
|
1987-06-19 09:25:08 +00:00
|
|
|
WHILE i >= 10 DO
|
|
|
|
e := e * 10000000000.0D;
|
|
|
|
DEC(i,10);
|
|
|
|
END;
|
1987-05-22 17:15:09 +00:00
|
|
|
WHILE i >= 0 DO
|
1987-05-27 10:05:01 +00:00
|
|
|
e := e * 10.0D;
|
1987-05-22 17:15:09 +00:00
|
|
|
DEC(i)
|
|
|
|
END;
|
|
|
|
IF pow10<0 THEN
|
|
|
|
r := r / e;
|
|
|
|
ELSE
|
|
|
|
r := r * e;
|
|
|
|
END;
|
|
|
|
IF signed THEN r := -r; END;
|
|
|
|
IF (iB <= HIGH(str)) AND (ORD(ch) > ORD(' ')) THEN ok := FALSE; END
|
1987-05-27 10:05:01 +00:00
|
|
|
END StringToLongReal;
|
1987-05-22 17:15:09 +00:00
|
|
|
|
1988-08-10 11:12:57 +00:00
|
|
|
BEGIN
|
|
|
|
WITH Powers10[1] DO pval := 1.0D32; rpval := 1.0D-32; exp := 32 END;
|
|
|
|
WITH Powers10[2] DO pval := 1.0D16; rpval := 1.0D-16; exp := 16 END;
|
|
|
|
WITH Powers10[3] DO pval := 1.0D8; rpval := 1.0D-8; exp := 8 END;
|
|
|
|
WITH Powers10[4] DO pval := 1.0D4; rpval := 1.0D-4; exp := 4 END;
|
|
|
|
WITH Powers10[5] DO pval := 1.0D2; rpval := 1.0D-2; exp := 2 END;
|
|
|
|
WITH Powers10[6] DO pval := 1.0D1; rpval := 1.0D-1; exp := 1 END;
|
1987-05-22 17:15:09 +00:00
|
|
|
END RealConversions.
|