binary coding of Pascal "Real" type needed, please
Author Message
binary coding of Pascal "Real" type needed, please

Shalom,

Hopefully not a faq/rtfm -question... ;-)

Can you tell me the binary coding of type REAL in Pascal, please?
Which bits are mantissa, which bits are exponent, how it fits together?

Need this for "deciphering" a Pascal binary file into Unix-C.

thx,
markus.

Sun, 30 Mar 2003 14:48:31 GMT
binary coding of Pascal "Real" type needed, please

Quote:
> Shalom,

Happy new year.

Quote:
> Hopefully not a faq/rtfm -question... ;-)

> Can you tell me the binary coding of type REAL in Pascal, please?
> Which bits are mantissa, which bits are exponent, how it fits
together?

> Need this for "deciphering" a Pascal binary file into Unix-C.

The simplest way to do this might be to write the decipherer in
Pascal. The Real type predates the standard floating point types used
by the 8087 but versions since (IIRC) TP4 have supported the standard
Single, Double and Extended types as well as continuing support for
Real. So the translation is built in. Read a Real from one file, write
a Double to the other. You need to set {\$N+} to enable the standard
floating point types. You can get TP5.5 free from the Borland Museum.
The address is in the mini-FAQ, Dr. John has a mirror of that at
http://www.merlyn.demon.co.uk/clpb-faq.txt

If you really want to do it the hard way, chapter 21 ("Memory Issues")
of the BP7 Language Guide says

Width in bits
1          39            8
+-+-------------------+-------+
|s|         f         |   e   |
+-+-------------------+-------+
msb             lsb msb lsb

The vale v of the number is determined by the following:
s    (e-129)
if 0 < e <= 255, then v = (-1)  * 2        *(1.f).
if e=0, then v=0.

The Real type can't store denormals, NaNs and infinities.
(Hope those superscripts came out OK!)

FP

Wed, 18 Jun 1902 08:00:00 GMT
binary coding of Pascal "Real" type needed, please

Quote:

>Can you tell me the binary coding of type REAL in Pascal, please?
>Which bits are mantissa, which bits are exponent, how it fits together?

>Need this for "deciphering" a Pascal binary file into Unix-C.

<URL: http://www.merlyn.demon.co.uk/pas-real.htm>

Sign, 1 bit.
Magnitude, 39 bits, fractional part with implicit leading "1.".
Exponent, 8 bits, biased upwards by 129.

--

<URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
<URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

Wed, 18 Jun 1902 08:00:00 GMT
binary coding of Pascal "Real" type needed, please

Quote:

> Width in bits
>   1          39            8
>  +-+-------------------+-------+
>  |s|         f         |   e   |
>  +-+-------------------+-------+
>     msb             lsb msb lsb

> The vale v of the number is determined by the following:
>                                 s    (e-129)
>   if 0 < e <= 255, then v = (-1)  * 2        *(1.f).
>   if e=0, then v=0.

> The Real type can't store denormals, NaNs and infinities.
> (Hope those superscripts came out OK!)

Actually, I frequently store NaNs (Not a Number) in reals by using
the fact that e=0 implies zero no matter what f/s.  So...

const
NAN_ : array[1..6] of byte = (\$00,\$FF,\$FF,\$FF,\$FF,\$7F);
var
NAN  : real  absolute NAN_;

A value can then be set to NAN and tested but the instant it is used
in a calculation, it assumes a value of zero.  I've even used these
"free bytes" in real values to represent all sorts of things (CHAR,
BYTE, WORD, INTEGER, LONGINT, STRING (ok so its short) and POINTER)
so that a single REAL variable can then be interpreted as a multi-
type value.

Lester Hanger

Mon, 31 Mar 2003 10:44:21 GMT
binary coding of Pascal "Real" type needed, please
On Thu, 12 Oct 2000 04:44:21 +0200, "Lester Hanger"

Quote:

>Actually, I frequently store NaNs (Not a Number) in reals by using
>the fact that e=0 implies zero no matter what f/s.  So...

>const
>  NAN_ : array[1..6] of byte = (\$00,\$FF,\$FF,\$FF,\$FF,\$7F);
>var
>  NAN  : real  absolute NAN_;

>A value can then be set to NAN and tested but the instant it is used
>in a calculation, it assumes a value of zero.  I've even used these
>"free bytes" in real values to represent all sorts of things (CHAR,
>BYTE, WORD, INTEGER, LONGINT, STRING (ok so its short) and POINTER)
>so that a single REAL variable can then be interpreted as a multi-
>type value.

Interesting, I can certainly see the value of NAN in working with
floating point values, but can you provide some examples of how you
used the other mechanisms you describe?

Stephen Posey

Tue, 01 Apr 2003 12:06:04 GMT
binary coding of Pascal "Real" type needed, please

Quote:

> > Width in bits
> >   1          39            8
> >  +-+-------------------+-------+
> >  |s|         f         |   e   |
> >  +-+-------------------+-------+
> >     msb             lsb msb lsb

> > The vale v of the number is determined by the following:
> >                                 s    (e-129)
> >   if 0 < e <= 255, then v = (-1)  * 2        *(1.f).
> >   if e=0, then v=0.

> > The Real type can't store denormals, NaNs and infinities.
> > (Hope those superscripts came out OK!)

> Actually, I frequently store NaNs (Not a Number) in reals by using
> the fact that e=0 implies zero no matter what f/s.  So...

you snipped the bit which said that most of my post which you quoted
was in fact a quote from the BP7 manual. (The only bit which wasn't a
quote was the bit in brackets which said I hope I quoted it legibly.)
So what you do with your reals is probably not relevant to Markus'
question of how to interpret a legacy file of reals.

FP

Wed, 18 Jun 1902 08:00:00 GMT
binary coding of Pascal "Real" type needed, please

Quote:

> Interesting, I can certainly see the value of NAN in working with
> floating point values, but can you provide some examples of how you
> used the other mechanisms you describe?

The particular application involved capturing various types of data
ranging from Yes/No to numeric to text and of course floating point
values too.  The real problem lay in the handling of text and this
is really where this solution came to the fore. (Boolean or Integer
values could obvioulsy be handled directly by the real type anyway).
To be honest, the other data types were just there for the fun of
it but didn't really get used.

When a string value was entered, it was written to the file as up to
43 consecutive reals (43*6 = 2+1+255 bytes).  The first value gave
the string length and successive values gave the remainder of
the string.

From memory, it was coded something like:

type
tDataType = (isBOOL,isBYTE,isINT,isWORD,isLONG,isPTR,isCHAR,isSTR);
tGenFile = file of real;
tGeneral = record  case byte of
0: (isREAL  : boolean;
case DataType: tDataType of
isBOOL:(AsBool: boolean);
isBYTE:(AsByte: byte);
isINT :(AsInt : integer);
isWORD:(AsWord: word);
isLONG:(AsLong: longint);
isPTR :(AsPtr : pointer);
isCHAR:(AsChar: char);
isSTR :(AsStr : string);
);
1: (AsReal  : real);
2: (AsReals : array[0..42] of real);  {covers 255 char string}
end; {tGeneral}

{ writes general purpose data to file }
procedure WriteValue(var F: tGenFile; var Gen: tGeneral);
var
i: integer;
begin
if Gen.isREAL OR (Gen.DataType <> isSTR) then
Write(F, Gen.AsReal)  { single value }
else for i := 0 to (length(Gen.AsStr)+2) div Sizeof(Real) do
Write(F, Gen.AsReals[i]);  { multiple values for string }
end; {WriteValue}

{ reads general purpose data from file }
procedure ReadValue(var F: tGenFile; var Gen: tGeneral);
var
i: integer;
begin
if NOT Gen.isREAL AND (Gen.DataType = isSTR) then
for i := 1 to (length(Gen.AsStr)+3) div Sizeof(Real) do
Read(F, Gen.AsReals[i]);  { multiple values for string }

{ writes general purpose data for display }
procedure ShowValue(var Gen: tGeneral);
begin
with Gen do
if isREAL then  write(AsReal:0:3)
else case DataType of
isBOOL: write(AsBool);
isBYTE: write(AsByte);
isINT : write(AsInt);
isWORD: write(AsWord);
isLONG: write(AsLong);
isCHAR: write(AsChar);
isSTR : write(AsStr);
end;
end; {ShowValue}

Wed, 18 Jun 1902 08:00:00 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages