Frank Heckenbach wrote :
[...]
So, why is B slighly inaccurate? Well, if you look at the file, it contains the text:
1.030000000000000e+02
And that's exactly what ReadLn sees. I.e., first the number 1.03 which cannot be represented exactly since floating point numbers (at least IEEE and most other popular formats) are binary, not decimal, and 1.03 in binary is periodic.
Afterwards, it finds `e+02' and multiplies the result by 100 which can be done exactly, but the error was already made.
[...]
Is this really computer dependent or does it depend of the program ? I think that reading 1.030000000000000e+02 as 1030000000000000 * 10^(-15+2) would suppress the problem. Wouldn't it be better ?
What I don't understand is that there is no problem if Readln reads 10.3e+01 in the file (10.3 isn't periodic in binary). And if it reads 103000e-03 (103000 IS periodic), the value is slightly wrong... (I tested this strange behaviour with the program shown below.)
I'm wondering if there's a way to write a binary (or hexadecimal) representation of the LongReal type in a text file, a clear and understandable representation that could be read back by the program without any difference with the original number. I wrote LongReal numbers to a File Of LongReal, and I translated this file into an hexadecimal representation, but the result was really unclear to me and I didn't understand the logic behind those numbers. I found and read this web page : http://www.infobiogen.fr/docs/info/SUNWspro/pascal/lang_ref/ref_data.doc.htm l (about another Pascal compiler) but it seems the GPC LongReal type is different from what is described there.
Program Bug09; Var A,B,C,D,E,F,G,H,I : LongReal; TextFile : Text; Begin Assign(TextFile,'tmp'); Rewrite(TextFile); Writeln(TextFile,'1030000e-04'); Writeln(TextFile,'103000e-03'); Writeln(TextFile,'10300e-02'); Writeln(TextFile,'1030e-01'); Writeln(TextFile,'103'); Writeln(TextFile,'10.3e+01'); Writeln(TextFile,'1.03e+02'); Writeln(TextFile,'0.103e+03'); Writeln(TextFile,'0.0103e+04'); Close(TextFile); Reset(TextFile); Readln(TextFile,A); Readln(TextFile,B); Readln(TextFile,C); Readln(TextFile,D); Readln(TextFile,E); Readln(TextFile,F); Readln(TextFile,G); Readln(TextFile,H); Readln(TextFile,I); Close(TextFile); Erase (TextFile); Writeln; Writeln('A = ',A:28); {Inaccurate} Writeln('B = ',B:28); {Inaccurate} Writeln('C = ',C:28); {OK} Writeln('D = ',D:28); {OK} Writeln('E = ',E:28); {OK} Writeln('F = ',F:28); {OK} Writeln('G = ',G:28); {Inaccurate} Writeln('H = ',H:28); {Inaccurate} Writeln('I = ',I:28); {Inaccurate} End.
-- «Couperin»