Recent

Author Topic: treader and twriter and floating point types  (Read 5038 times)

Yogi

  • New Member
  • *
  • Posts: 42
treader and twriter and floating point types
« on: September 08, 2010, 12:19:10 am »
 :o Hello All

A simple program to demonstrate my problem

Code: [Select]
program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes,
  { you can add units after this }
  sysutils;

{$R *.res}

var
  f : tmemorystream;
  w : twriter;
  r : treader;
  d1,d2 : tdatetime;
  c1,c2 : currency;
  e1,e2 : extended;
  s1,s2 : single;

begin
  f:=tmemorystream.Create; // Does not matter which kind of stream
  w:=twriter.Create(f,255);
  d1:=encodedate(2010,1,1);
  w.WriteDate(d1);
  c1:=123.45;
  w.WriteCurrency(c1);
  e1:=12345.678;
  w.WriteFloat(e1);
  s1:=456.78;
  w.WriteSingle(s1);
  w.Free;
  f.Seek(0,soFromBeginning);
  r:=treader.Create(f,255);
  d2:=r.ReadDate;
  c2:=r.ReadCurrency;
  e2:=r.ReadFloat;
  s2:=r.ReadSingle;
  r.Free;
  f.Free;
  writeln;
  writeln('Expected:');
  writeln(datetostr(d1));
  writeln(c1,'   ',currtostrf(c1,fffixed,2));
  writeln(e1,'   ',floattostrf(e1,fffixed,9,3));
  writeln(s1,'   ',floattostrf(s1,fffixed,9,2));
  writeln;
  writeln('Result:');
  writeln(datetostr(d2));
  writeln(c2,'   ',currtostrf(c2,fffixed,2));
  writeln(e2,'   ',floattostrf(e2,fffixed,9,3));
  writeln(s2,'   ',floattostrf(s2,fffixed,9,2));
  writeln;
end.

Running this program gives the following output:

Expected:
1-1-10
 1.234500000000000000E+02   123.45
 1.2345678000000000E+0004   12345.678
 4.567799988E+02   456.78

Result:
30-12-99
 1.234500000000000000E+06   1234500.00
 1.2345678000000000E+0004   12345.678
 1.139041280E+09   1139041280.00


Only the combination of twriter.writefloat and treader.readfloat is producing the expected correct result
The other combinations dont procduce right results.

Ok, I can use writefloat/readfloat on the other types too, because they are also floating point types, but why should I do so when there are procedures available?


Anybody else remarked these incorrect behaviour or is it special to my computer :P
Lazarus 1.3 SVN 44197 * FPC 2.7.1 SVN 26822
Linux: 3.2.0-4-686-pae * Debian wheezy
Windows: W98

Yogi

  • New Member
  • *
  • Posts: 42
Re: treader and twriter and floating point types
« Reply #1 on: September 08, 2010, 12:13:07 pm »
 ::) Hello again

Anybody out there who can confirm this or am I doing something wrong?
Lazarus 1.3 SVN 44197 * FPC 2.7.1 SVN 26822
Linux: 3.2.0-4-686-pae * Debian wheezy
Windows: W98

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11076
  • FPC developer.
Re: treader and twriter and floating point types
« Reply #2 on: September 08, 2010, 12:19:01 pm »
I'm not sure, but I suspect that this is because a write<x> also writes an enum type, and only then data, while the read<x> only reads data. (because other parts of the reader already read the type).

I've no time to correct the code though.

Yogi

  • New Member
  • *
  • Posts: 42
Re: treader and twriter and floating point types
« Reply #3 on: September 08, 2010, 12:42:17 pm »
 ;D Thanks; that sounds good because then it is not me

Should I write a issue in the bugtracker?
Lazarus 1.3 SVN 44197 * FPC 2.7.1 SVN 26822
Linux: 3.2.0-4-686-pae * Debian wheezy
Windows: W98

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11076
  • FPC developer.
Re: treader and twriter and floating point types
« Reply #4 on: September 08, 2010, 01:47:53 pm »
;D Thanks; that sounds good because then it is not me

Should I write a issue in the bugtracker?

No, this is normal.  If you don't want this, you shouldn't be using twriter/treader but e.g. a TStream descendant directly.

The whole point of treader and twriter is this kind of typing. These functions are exposed because e.g. customer properties need direct access to them.

 

TinyPortal © 2005-2018