[Oberon] System-V - Float to Alpha
Tomas Kral
thomas.kral at email.cz
Mon Feb 4 11:39:27 CET 2019
Hi,
I am borrowing this code from Files.WriteReal(), it should convert
r:REAL to D[0..16] string. However decimal point always overwrites last
digit (*first one after reverse*).
1.23123E+02, becomes .23123E+02
Can you please help to see what I do wrong, any improvement welcome?
Many Thanks
PROCEDURE String(r: REAL);
VAR e, i, j, m: INTEGER; d: ARRAY 16 OF CHAR;
BEGIN i := 0; j := 0; e := ASR(ORD(r), 23) MOD 100H; (*binary exponent*)
IF e # 0 THEN
IF r < 0.0 THEN D[j] := "-"; r := ABS(r); INC(j) END ;
e := (e - 127) * 77 DIV 256 - 6; (*decimal exponent*)
IF e >= 0 THEN r := r / Ten(e) ELSE r := Ten(-e) * r END ;
m := FLOOR(r + 0.5);
IF m >= 10000000 THEN INC(e); m := m DIV 10 END ;
REPEAT d[i] := CHR(m MOD 10 + 30H); m := m DIV 10; INC(i) UNTIL m = 0;
d[i] := 0X; Out.String(d); Out.Ln; DEC(i); d[i] := "."; (* "." replaces 1st digit !! *)
REPEAT D[j] := d[i]; DEC(i); INC(j) UNTIL i < 0 ; (*reverse*)
D[j] := "E"; INC(e, 6); INC(j);
IF e < 0 THEN D[j] := "-"; e := -e ELSE D[j] := "+" END ;
D[j+1] := CHR(e DIV 10 + 30H); D[j+2] := CHR(e MOD 10 + 30H)
END
END String;
--
Tomas Kral <thomas.kral at email.cz>
More information about the Oberon
mailing list