         Edinburgh IMP77 Compiler - Version 8.4

    1  ! Print a floating point number out, along the lines of
    2  ! +/-nnn.nnn@+/-nn, to occupy Places character spaces.
    3  ! Note - there's a bug in this code such that it does not
    4  ! round the number properly.  EG 3.999999999 to 4 places
    5  ! should be 4.00 but we print 3.99
    6  
    7  %external %routine print(%longreal x, %integer places)
    8     %integer exponent, digit, point, printexpo
    9     !%externalintegerfnspec int pt(%real x)
   10  
   11     %if x = 0 %start
   12        printstring("0.0")
   13        %while places > 3 %cycle
   14           printsymbol('0')
   15           places = places - 1
   16        %repeat
   17        %return
   18     %finish
   19  
   20     %if x < 0 %then printsymbol('-') %and x = -x %and places = places - 1
   21  
   22     %if places < 3 %then places = 3
   23     
   24     exponent = 0
   25     printexpo = 0
   26  
   27     %while x < 1 %cycle
   28        x = x * 10
   29        exponent = exponent - 1
   30     %repeat
   31  
   32     %while x >= 10 %cycle
   33        x = x / 10
   34        exponent = exponent + 1
   35     %repeat
   36  
   37     ! Now X is between 1.0 and 9.99 and exponent is set accordingly
   38     ! If the exponent is "large" we will use scientific notation
   39     point = places - 2;     ! for useful digits after the "0."
   40     %if exponent >= places %or exponent < -point %start
   41        printexpo = exponent
   42        exponent = 0
   43        places = places - 2
   44     %finish
   45  
   46     ! Now the exponent is small-ish
   47     %if exponent < 0 %start;      ! 0.nnnn
   48        printsymbol('0'); printsymbol('.'); places = places - 2
   49        %while exponent < -1 %cycle
   50           printsymbol('0')
   51           exponent = exponent + 1
   52           places = places - 1
   53        %repeat
   54        point = -1; ! because we've already passed that
   55     %else;                        ! nnn.nnn
   56        point = exponent
   57     %finish
   58  
   59     %while places > 0 %cycle
   60        digit = int pt(x)
   61        ! Rounding as we go through this loop can "oversize" the digit.  This
   62        ! of course tells us that we should have printed (eg) 40000 but we
   63        ! are now stuck with printing 39999
   64        %if digit > 9 %then digit = 9
   65        printsymbol(digit + '0')
   66        x = (x - digit)*10
   67        %if point = 0 %then printsymbol('.') %and places = places - 1
   68        point = point - 1
   69        places = places - 1
   70     %repeat
   71  
   72     %if printexpo # 0 %start
   73        printsymbol('@')
   74        write(printexpo, 1)
   75     %finish
   76  %end
   77  
   78  %begin
   79  %end
   80  
   81  %endoffile

   58 Statements compiled
