Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Floating point exception on DXE10.2 but not on D7. Why?


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 9 - Last Post: Feb 2, 2018 10:36 AM Last Post By: Chad Hower
Samuel Herzog

Posts: 16
Registered: 9/10/00
Floating point exception on DXE10.2 but not on D7. Why?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 21, 2018 11:12 PM
Hello Forum,

while migrating from D7 to DXE10.2 one of our testcases does behave diffferent.
I have extracted the topic to a simple one-click example:

The following code triggers an exception on DXE10.2 but not on D7.

procedure TForm1.Button1Click(Sender: TObject);
var
_v:variant;
_e:extended;
_Data: TDoubleDynArray;  // from unit Types
begin
  _v:=MaxDouble;
  _e:=strtofloat(_v);
  Setlength(_Data,1);
  _Data[0]:= _e;         // <<<--- floating point overflow on DXE10.2 but not on D7
end;


Does someone have an explaination for this difference?
Has something changed in the default project settings between D7 and DXE about Exception-Mask ?

Thank you for your suggestions.

Sam
Nigel Thomas

Posts: 15
Registered: 4/26/07
Re: Floating point exception on DXE10.2 but not on D7. Why?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 25, 2018 1:19 PM   in response to: Samuel Herzog in response to: Samuel Herzog
Samuel Herzog wrote:

Hello Forum,

while migrating from D7 to DXE10.2 one of our testcases does behave
diffferent. I have extracted the topic to a simple one-click example:

The following code triggers an exception on DXE10.2 but not on D7.

procedure TForm1.Button1Click(Sender: TObject);
var
_v:variant;
_e:extended;
_Data: TDoubleDynArray;  // from unit Types
begin
  _v:=MaxDouble;
  _e:=strtofloat(_v);
  Setlength(_Data,1);
  _Data[0]:= _e;         // <<<--- floating point overflow on DXE10.2
but not on D7 end;


Does someone have an explaination for this difference?
Has something changed in the default project settings between D7 and
DXE about Exception-Mask ?

Thank you for your suggestions.

Sam

Check the Compiler option Overflow Checking. I believe in later Delphi
versions this is set to ON in Debug branch, but OFF in Release branch.
Check the setting in your D7 IDE: if it is OFF, turn it on and see if
the exception is then raised. Then you can decide what, if anything,
you intend to do about it.

Nigel
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Floating point exception on DXE10.2 but not on D7. Why?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 25, 2018 1:36 PM   in response to: Samuel Herzog in response to: Samuel Herzog
Samuel Herzog wrote:
while migrating from D7 to DXE10.2 one of our testcases does behave diffferent.

On which platform(s)? Did you check the generated machine code for differences? Have you tried stepping into the RTL source code to look for logical differences?

Does the problem only happen when using TDoubleDynArray specifically, or does any Extended-to-Double assignment cause the same problem? For instance:

var
  _e: Extended;
  _Data: Double;
begin
  _e := MaxDouble;
  _Data := _e; // <-- is there a floating point overflow here?
end;


--
Remy Lebeau (TeamB)
Samuel Herzog

Posts: 16
Registered: 9/10/00
Re: Floating point exception on DXE10.2 but not on D7. Why?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 26, 2018 6:48 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hello Remy,

thank you for your answer.

I created a new VCL-Project in DXE 10.2.2 and Overflow Checking is OFF for all configurations.

This works without overflow exception.
var
_e:extended;
_Data: TDoubleDynArray;
begin
  _e:=MaxDouble;
  Setlength(_Data,1);
  _Data[0]:=_e;


So difference comes somehow from the variant to extended convertion in relation with "MaxDouble".
var
_e:extended;
_v:variant;
_Data: TDoubleDynArray;  // from unit Types
begin
  _v:=MaxDouble;
  _e:=strtofloat(_v);
  Setlength(_Data,1);
  _Data[0]:=_e;
end;
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Floating point exception on DXE10.2 but not on D7. Why?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 26, 2018 7:16 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

Samuel Herzog wrote:
while migrating from D7 to DXE10.2 one of our testcases does behave
diffferent.

On which platform(s)? Did you check the generated machine code for
differences? Have you tried stepping into the RTL source code to
look for logical differences?

Does the problem only happen when using TDoubleDynArray specifically,
or does any Extended-to-Double assignment cause the same problem?
For instance:

var
  _e: Extended;
  _Data: Double;
begin
  _e := MaxDouble;
  _Data := _e; // <-- is there a floating point overflow here?
end;

This has something to do with StrToFloat.

I've tried the following code:

procedure TForm1.FormCreate(Sender: TObject);
var e: extended;
    v: variant;
begin
 
//  TFloatFormat
  memo1.lines.Add('MaxDouble = '+FloatToStr(maxdouble));
  e:=StrToFloat('1.79769313486232E+308');
  memo1.lines.Add('e = '+ FloatToStr(e));
  memo1.lines.Add('e - maxdouble = ' + FloatToStr(e-MaxDouble));
end;

The result is:

MaxDouble = 1.79769313486232E308
e         = 1.79769313486232E308
e - maxdouble = 4.29183629062056E293
Alex Belo

Posts: 626
Registered: 10/8/06
Re: Floating point exception on DXE10.2 but not on D7. Why?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 26, 2018 10:29 PM   in response to: Lajos Juhasz in response to: Lajos Juhasz
Lajos Juhasz wrote:

I've tried the following code:
...
e:=StrToFloat('1.79769313486232E+308');

Mantissa of this value is slightly bigger than in actual
System.Math.MaxDouble:

your value 1.79769313486232 E+308
MaxDouble = 1.7976931348623157081e+308;

The difference in ^308 is very big.

FPU registers:
MaxDouble: ST0 Valid 43FE FFFF FFFF FFFF F800
your value: ST0 Valid 43FF 8000 0000 0000 5204 (overflow in fact)

--
Alex
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: Floating point exception on DXE10.2 but not on D7. Why?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 28, 2018 11:18 PM   in response to: Alex Belo in response to: Alex Belo
Alex Belo wrote:

Lajos Juhasz wrote:

I've tried the following code:
...
e:=StrToFloat('1.79769313486232E+308');

Mantissa of this value is slightly bigger than in actual
System.Math.MaxDouble:

your value 1.79769313486232 E+308
MaxDouble = 1.7976931348623157081e+308;

The difference in ^308 is very big.

FPU registers:
MaxDouble: ST0 Valid 43FE FFFF FFFF FFFF F800
your value: ST0 Valid 43FF 8000 0000 0000 5204 (overflow in fact)

I have to admit that I didn't checked the actual maxdouble vlaue I've
used the value from FloatToStr(maxdouble).
Alex Belo

Posts: 626
Registered: 10/8/06
Re: Floating point exception on DXE10.2 but not on D7. Why?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 26, 2018 11:16 PM   in response to: Samuel Herzog in response to: Samuel Herzog
Samuel Herzog wrote:

var
_v:variant;
_e:extended;
_Data: TDoubleDynArray; // from unit Types
begin
_v:=MaxDouble;
_e:=strtofloat(_v);
Setlength(_Data,1);
_Data[0]:= _e; // <<<--- floating point overflow on DXE10.2
// but not on D7
end;

procedure TForm1.Button2Click(Sender: TObject);
var
v: variant;
e: extended;
d: double;
s: string;
begin
v:=MaxDouble;
{
fld tbyte ptr [$005d3110]
FPU registers:
ST0 Valid 1.79769313486232E308 (43FE FFFF FFFF FFFF F800)
(correct)
lea eax,[ebp-$28]
call @VarFromReal
}
s:=v;
Memo1.Lines.Add(s);
// 1.79769313486232E308
// Rounded up (> MaxDouble=1.7976931348623157081e+308)

e:=StrToFloat(s);
{
lea eax,[ebp-$3c]
lea edx,[ebp-$28]
call @VarToUStr
mov eax,[ebp-$3c]
mov edx,[$005e0ed4]
call StrToFloat
fstp tbyte ptr [ebp-$10]
FPU registers:
ST0 Valid 1.79769313486232E308 (43FF 8000 0000 0000 5204)
(overflow)
}
d:=e; // exception
end;

Diagnosis:
- accuracy of the string representation in "s:=v" is too low, returned
string is rounded up;
- back conversion generates wrong value (as expected).

I have no idea why it works in D7 (no D7 in hand).
Check this example in D7 and see what happens...

--
Alex
Samuel Herzog

Posts: 16
Registered: 9/10/00
Re: Floating point exception on DXE10.2 but not on D7. Why?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 12:19 AM   in response to: Alex Belo in response to: Alex Belo
Hello Alex,

thank you for your example.

On D7 I do not get an floating point overflow exception and the memo shows then 1.7E308
On DXE 10.2.2 I get an floating point overflow exception and if I continue, then the memo shows 1.79769313486232E308

I have opened a bug-report: [RSP-19790|https://quality.embarcadero.com/browse/RSP-19790]

Let's see if Embarcadero will find out...

Sam
Chad Hower

Posts: 613
Registered: 3/2/07
Re: Floating point exception on DXE10.2 but not on D7. Why?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 2, 2018 10:36 AM   in response to: Samuel Herzog in response to: Samuel Herzog
On 1/29/2018 4:19 AM, Samuel Herzog wrote:
Hello Alex,

thank you for your example.

On D7 I do not get an floating point overflow exception and the memo shows then 1.7E308
On DXE 10.2.2 I get an floating point overflow exception and if I continue, then the memo shows 1.79769313486232E308

I have opened a bug-report: [RSP-19790|https://quality.embarcadero.com/browse/RSP-19790]

Let's see if Embarcadero will find out...

Doesnt seem same, but maybe worth a look. I had to use this in one
project when .NET is hosted.

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/System_Set8087CW.html
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02