Watch, Follow, &
Connect with Us

ID: 26274, TryStrToFloat may raise exception for conversions into double

by Michael Braun Email: Anonymous


TryStrToFloat('1e999',d) with d as double raises a floating point overflow exception instead of simply returning "false".
Download Details
0 bytes
CDN Login Required to Download. (You will be redirected to the login page if you click on the Download Link)
To download this, you must have registered:
A free membership

For Delphi, Version 6.0  to 10.0 0 downloads
Copyright: No significant restrictions


Size: 0 bytes
Updated on Fri, 14 Nov 2008 01:22:11 GMT
Originally uploaded on Fri, 14 Nov 2008 01:24:10 GMT
Description
TryStrToFloat('1e999',d) with d as double is intended to return a "false", because 1e999 does not fit into a double. However, the routine raises an overflow exception.
The reason is obvious (see excerpt from SysUtils.pas):

function TryStrToFloat(const S: string; out Value: Double): Boolean;
var
LValue: Extended;
begin
Result := TextToFloat(PChar(S), LValue, fvExtended);
if Result then
Value := LValue;
end;

The routine first translates the string into an extended (which is possible for 1e999), and then it copies the extended into the double, without any further range check. LValue should be checked, if it fits into a double. If not, the routine should return a "False".
At least, that is my interpretation of this routine.
Of course, a similar problem will be found in the conversion routines for singles.

   Latest Comments  View All Add New

Move mouse over comment to see the full text

Server Response from: ETNACDC01