This is bizarre. I branched into a separate project and created my own function to convert a string into a float without the Val code. And lo and behold, both functions worked! I then glanced and realized that I used an Extended variable here, and not a Single size variable.
I expanded my test to cover the four floating point types I'm familiar with. Real, Extended, Single, and Double. The results are far less than satisfactory, and now I'm certain there's a bug somewhere in floating-point types that aren't Extended. See it, compile it, and try it for yourself. Even floating point comparisons and expansions appear flawed right now.
My results:
Code:
Float Debugger
Value = 0.9
Custom converter results: true
Default converter results:
Single -> false(0.8999999762)
Double -> false(0.9)
Real -> false(0.9)
Extended -> true (0.9)
Code:
program floattest;
uses SysUtils, StrUtils, Math;
procedure CutStr(const s, delim: string; var s1,s2: string);
var i: integer;
flag: boolean;
begin
if length(s) = 0 then
exit;
i := 1;
flag := false;
s1 := '';
s2 := '';
while i <= length(s) do begin
if s[i] = '.' then begin
flag := true;
i += 1;
end;
if flag then
s2 := s2+s[i]
else
s1 := s1+s[i];
i += 1;
end;
end;
function CustStrToFloat(S: String): Extended;
var middle: integer;
whole,part: string;
begin
middle := Pos('.',S);
if middle <= 0 then
Exit(StrToInt(S))
else begin
CutStr(s,'.',whole,part);
exit(StrToInt(whole) + StrToInt(part)/power(10,Length(part)));
end;
end;
var
f1: single;
f2: double;
f3: real;
f4: extended;
BEGIN
writeln('Float Debugger');
writeln;
writeln('Value = 0.9');
write('Custom converter results: ');
if CustStrToFloat('0.9') = 0.9 then
writeln('true')
else
writeln('false');
writeln('Default converter results:');
f1 := StrToFloat('0.9');
f2 := StrToFloat('0.9');
f3 := StrToFloat('0.9');
f4 := StrToFloat('0.9');
write(' Single ->');
if f1 = 0.9 then
writeln(' true (',FloatToStr(f1),')')
else
writeln(' false(',FloatToStr(f1),')');
write(' Double ->');
if f2 = 0.9 then
writeln(' true (',FloatToStr(f2),')')
else
writeln(' false(',FloatToStr(f2),')');
write(' Real ->');
if f3 = 0.9 then
writeln(' true (',FloatToStr(f3),')')
else
writeln(' false(',FloatToStr(f3),')');
write(' Extended ->');
if f4 = 0.9 then
writeln(' true (',FloatToStr(f4),')')
else
writeln(' false(',FloatToStr(f4),')');
readln;
END.
Bookmarks