Patch: Error in the real2str.inc which cause wrong display of floating-point numbers [by writeln() or by Str()]
Original Reporter info from Mantis: babysam
-
Reporter name: Sam Cheng
Original Reporter info from Mantis: babysam
- Reporter name: Sam Cheng
Description:
The error occurs when a float is displayed using writeln (without formatting).
e.g. When using this program
program test;
var i:double;
j:single;
begin
readln(i);
j:=i;
writeln(i);
writeln(j);
i:=j;
writeln(i);
writeln(j);
readln;
end.
0.5 (which should be displayed exactly as 5.000000000E-01) will incorrectly shown as 5.000001192E-01 in the single precision but correctly in double precision. Other float are also shown incorrectly. (in different degrees)
A better illustration to the problem is when you use Str() to convert a single to string and try to convert the string back to a single. The number display in the resulting float will be offseted by some amount (which should not be caused by a precision problem)
//Another program for continuous checking of floating-point number in different precisions
program test2;
uses dos,crt;
//The i's are used to have a better understanding of what is actually happening...
var i,new_i:longword;
j,new_j:single;
k,new_k:double;
s:string;
Err:integer;
count:int64;
begin
randomize;
count:=0;
clrscr;
writeln('Validating...');
writeln('Total number of double checked=');
repeat
//As k is set to be a single-precision number, there should not be
//any rounding off or truncation problem...
k:=2*random-1;
j:=k;
asm
mov edx,j
mov i,edx
end;
Str(j,s);
Val(s,new_j,Err);
asm
mov edx,new_j
mov new_i,edx
end;
count:=count+1;
Gotoxy(32,2);
write(count);
until new_i<>i;
writeln;
writeln('Error occurs');
writeln;
writeln(' i=',i);
writeln(' j=',j);
writeln(' k=',k);
writeln;
writeln(' s=',s);
writeln;
writeln('new_i=',new_i);
writeln('new_j=',new_j);
writeln(' k=',k);
readln;
end.
The program ends immediately after one cycle when it is compiled by fpc 2.2.2 or later (including 2.2.4) but runs continuously if it is compiled by fpc 2.2.0.
After checking with the sources of 2.2.0 and 2.2.4, the suspected part which cause the error is the line 370-371 of the file \rtl\inc\real2str.inc (for the latest version). By removing that part, the recompiled fpc shows no appearent problem in the above programs.
Mantis conversion info:
- Mantis ID: 14230
- Version: 2.2.4
- Fixed in version: 2.4.0
- Fixed in revision: 13717 (#0698121f)