(Tested on Windows 7, 32-bit)

Val procedure (and

StrToFloat as well) fails with SIGFPE with Math.MaxDouble.

As Val should never raise exception (but only return non-zero value in third parameter), this is even more annoying.

First, take a look at this simple programme. It works with FPC 3.0.4, but not with FPC 3.2:

program project1;

{$mode objfpc}{$H+}

uses SysUtils, Math;

var

D1, D2: Double;

S: AnsiString;

E: Integer;

begin

D1 := Math.MaxDouble;

WriteLn('D1 = ', D1);

S := FloatToStr(D1);

// If you replace the previous line with any of the following two, then everything works!

// But the difference is only in + sign in front of exponent.

//WriteStr(S, D1);

//Str(D1, S);

WriteLn('S = ', S);

Val(S, D2, E); // SIGFPE here!

//D2 := StrToFloat(S); // same with this, as it uses Val internally.

WriteLn('D2 = ', D2);

ReadLn;

end.

Now, let's see:

First, the declaration of Math.MaxDouble changed in FPC 3.2

In 3.0.4 it was:

MaxDouble = 1.7e+308;

And in 3.2 it is:

MaxDouble = 1.7976931348623157e+308;

Now, if we use old Pascal Str function, or WriteStr, then we get '1.7976931348623157e+308' as result.

FloatToStr gives us '1.7976931348623157e308' (the difference is only in + sign in the exponent).

Then without this + sign, Val gives SIGFPE!

However, it seems that this bug in Val only appears when string value has 16 digits (and MaxDouble has it in FPC 3.2).