[fpc-pascal] two small ?s - high(real) and nearest 2^x

Matthias K. makadev at googlemail.com
Sat Oct 10 20:58:25 CEST 2009


On Sat, Oct 10, 2009 at 7:14 PM, David Emerson <dle3ab at angelbase.com> wrote:
> 1. Is there a way to get the highest and lowest real values? I can do
> high(longint) but high(real) gives me an error. Also of interest is a
> low(real) value (-m * 10^n) as distinct from the smallest real value
> (m * 10^-n)

As stated in the Documentation:
  http://www.freepascal.org/docs-html/ref/refsu6.html
The Real Type is Plattform dependend and either Single or Double.
And since its IEEE Floating Point Representation, Low = -High (just a
difference in sign bit)

You can find Max/Min Single/Double in unit math.
Sample:

--
uses math;
...

  WriteLn( 'Size: ', SizeOf(Real) );
  if SizeOf(Real)>=8 then
  begin
    WriteLn( 'Low: ', -MaxDouble );
    WriteLn( 'High: ', MaxDouble );
  end else
  begin
    WriteLn( 'Low: ', -MaxSingle );
    WriteLn( 'High: ', MaxSingle );
  end;
--

> 2. For the purposes of reserving memory in block sizes that can be
> easily reallocated, I like to use powers of two. So if I have, e.g., a
> dynamic array, I might start with a size of 1024 and then double it
> when it hits capacity. Hopefully this smoothes memory management, as I
> am using a lot of memory...
>
> Anyway, what I'd like is a simple function that can quickly identify the
> next power-of-two larger than an arbitrary number. So if I feed the
> function 472, it will return 512. Thus far I haven't been able to
> figure out how to do this without a big if-then-else nest. Is there
> some clever way to request that the processor return the position of
> the leftmost '1' in 00101101?

invalue := 412;
--
outvalue := 1;
while invalue <> 0 do
begin
  invalue := invalue div 2;
  outvalue := outvalue*2;
end;
--

BUT.. this "allocation algorithm" may be good in theory (amortized
performance for reallocation), but youre losing a lot of memory
because of prereservation.. just for the records ;)

regards,
  Matthias



More information about the fpc-pascal mailing list