//{$UNITPATH /usr/local/fpc2/units}
{$H+} // ansi strings
{$C+} // assertions on
program grad;
// NOTE this code is very weak and kludgy; I really don't like it !
// It sort of works and produce acceptable tesults but
// one would like a cleaner mathematically sane algorithm

function grd(xmin, xmax: double): string;

var
  ln10: double;
  x, d, pas, log10p: double;
  k, n, nd: longint;
  s0, s: string;
  i: longint;
begin
  ln10 := ln(10.001); // burp why ?
  d := xmax-xmin;
  pas := 1.0;
  k := 0;
  n := trunc(d/pas);
  while n < 3 do begin
    case k of
      0,2: pas := pas / 2.0;
      1:   pas := pas / 2.5;
    end;
    k := (k+1) mod 3;
    n := trunc(d/pas);
  end;
  while n > 5 do begin
    case k of
      0,2: pas := pas * 2.0;
      1: pas := pas * 2.5;
    end;
    k := (k+1) mod 3;
    n := trunc(d/pas);
  end;
  log10p := ln(pas)/ln10;
  if log10p>0 then begin
    nd := 0;
  end else begin
    nd := trunc(-log10p)+1
  end;
  // ech := power(10,nd); TODO check this
  n := trunc(xmin/pas);
  x := pas*n;
  i := 0;
  s := '[';
  while (x<xmax) and (i<5) do begin
      if x>xmin then begin
        inc(i);
        str(x:0:nd,s0);
        s := s+'('+s0+')';
      end;
      x := x + pas;
  end;
  s := s+']';
  grd:=s;
end;

var
  x1, x2: single;
  code: integer;

begin
  if paramcount=2 then begin
    val(paramstr(1),x1,code);
    assert( code=0 );
    val(paramstr(2),x2,code);
    assert( code=0 );
    writeln(grd(x1,x2))
  end else
    writeln('usage: grad -23.25 101.2')
end.
