home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
pascal
/
library
/
dos
/
bix
/
packreal.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1986-08-04
|
932b
|
40 lines
{pack 6-byte REALS into 2-byte integers with greater range than MAXINT}
{TITLE: Data Compression Nifty!}
program tr;
type both = record case integer of
0:(r:real);
1:(b:array[0..5] of byte)
end;
str3=string[3];
var r:real;i:integer;
function r2i(r:real):integer;
var x:both;
i:integer;
begin
x.r:=r+2;
i:=(x.b[0] and $1f) shl 11; {5 lsb of exponent}
i:=i or ( (x.b[5] and $7f) shl 4); {hi 7 bits of precision}
i:=i or (x.b[4] shr 4); {lo 4 bits of precision}
r2i:=i
end;
function i2r(i:integer):real;
var j:integer;x:both;
begin
x.r:=0;
x.b[0]:=$80 or ((i shr 11) and $1f);
x.b[5] := (i shr 4) and $7f; {hi 7 bits precision}
x.b[4] := (i and $000f) shl 4; {lo 4 bits precision}
i2r:=x.r-2
end;
begin
repeat
write('enter real number.....');readln(r);
writeln('orig=',r:12:6,' int=',r2i(r),' packed/unpacked=',i2r(r2i(r)):12:6);
until r=0;
end.