Re: Reading and writing doubles

From: Perry Way (no.delphipro.spam_at_no.spam.earthlink.net)
Date: 10/16/03


Date: Thu, 16 Oct 2003 09:18:47 -0700


"Chris Cowley" wrote in message

> What is the most efficient way to set and read the value of 'double'
> floating point values in Delphi (v5 in case that matters) using the
> internal 8-byte exponent+manstissa representation?

By using a double. :)

> At the moment I am using a TMemoryStream to convert between a double and
> an array of bytes, like this:-

Yikes. Why the backbreaking cartwheels?

> var
> ms: TMemoryStream;
> z: double;
> b: array[0..7] of byte;
> egin
> z := 3.141592;
>
> ms := TMemoryStream.Create;
> ms.Write(z,8);
> ms.Seek(0,soFromBeginning);
> ms.Read(b,8);
> ms.Free;
>
> This works, but I can't help but feel there must be a much more
> efficient way of doing it, using a pointer to the double or something.
> Any ideas?

You're already writing the double in your example above. To read it back
again, just call .Read.

By the way, I would not make constant value references for size in Read and
Write calls. A less error prone method would be to pass SizeOf() for simple
types and Length() for string buffers.

PW



Relevant Pages

  • Re: Delphi 8 info
    ... not floating like in Delphi. ... I like the floating window. ... > loudly to Borland about this. ... > you have to not only ship the .NET runtime, ...
    (borland.public.delphi.non-technical)
  • Re: Borland is neglecting Delphi for Win32 badly
    ... Hey Borland, are you listening? ... >outperform the native versions in Delphi. ... and have come to the conclusion that you don't need good floating ...
    (borland.public.delphi.non-technical)
  • Re: Bug in Delphi SimpleRoundTo function?
    ... Delphi code to extended seems to help. ... was that the Delphi routine seems to have a bug. ... trying to round a floating point number by using ... decimal fraction numbers to Delphi. ...
    (borland.public.delphi.non-technical)
  • Truncation of Very Large Numbers
    ... I am using Delphi 4 and having some real trouble with very ... large floating point numbers. ... and it is important that I do the calculations to a high degree ... I presume that this has to do with variable MyNewProduct being defined as ...
    (alt.comp.lang.borland-delphi)