[Oberon] System-V - Float to Alpha

Jörg joerg.straube at iaeth.ch
Mon Feb 4 12:44:06 CET 2019


Something like that should do

          d[i] := 0X; Out.String(d); Out.Ln; (* = debug output *)
          DEC(i); D[j] := d[i]; D[j+1] := "."; INC(j, 2);
          WHILE i > 0 DO DEC(i); D[j] := d[i]; INC(j) END; (*reverse*)

Jörg
Am 04.02.19, 11:43 schrieb "Oberon im Auftrag von Tomas Kral" <oberon-bounces at lists.inf.ethz.ch im Auftrag von thomas.kral at email.cz>:

    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>
    --
    Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
    https://lists.inf.ethz.ch/mailman/listinfo/oberon
    




More information about the Oberon mailing list