myvalue:=0/0 return Nan and no exception

Hello,

By default 0/0 doesn't throw and exception.
Example :

var
  myvalue:integer;
begin
 myvalue:=0/0; // <= no exception
 Showmessage(myvalue);
end;

Will show "Nan". That's a problem if you want to store this value in dataset.

Does it exist a way to handle an exception EVariantInvalidOpError or something similar like Delphi do for 0 division ?

Actually Delphi behaves like that. The following code shows NaN twice:

procedure TForm2.Button3Click(Sender: TObject);
var
  V1, V2: Variant;
  I1, I2: Integer;
begin
  V1 := 0;
  V2 := 0;
  ShowMessage(V1 / V2);

  I1 := 0;
  I2 := 0;
  ShowMessage(FloatToStr(I1 / I2));
end;
procedure TForm2.Button3Click(Sender: TObject);
var
  V1, V2: Variant;
  I1, I2: Integer;
begin
  V1 := 0;
  V2 := 0;
  ShowMessage(V1 / V2);

  I1 := 0;
  I2 := 0;
  ShowMessage(FloatToStr(I1 / I2));
end;

Your Delphi is very strange, tested on 4 different versions of delphi (10.4 and 11.3 pro and enterprise) with "default compilation option" :
Both showmessage, give me an access violation EinvalidOp with message 'Invalide floating point operation at adress 043C52F"

Like described here ;
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Floating-Point_Exceptions

See attached project.

TestDivisionByZero.zip (5.7 KB)

Yes I confirm it raise EInvalidOP exception with your project (or not). Do you have a customized version of Delphi ? Because your project raise EInvalidOP exception "Floating point invalid operation" :

003ec28f +063 TestDivisionByZero.exe System.Variants     4099   +7 RealOp
003ecbeb +0c7 TestDivisionByZero.exe System.Variants     4427  +15 SimpleVarOp
003ece0f +027 TestDivisionByZero.exe System.Variants     4509   +6 @VarOp
005c97a1 +04d TestDivisionByZero.exe Unit2                 33   +3 TForm2.Button1Click
0052910b +073 TestDivisionByZero.exe Vcl.Controls        7707   +9 TControl.Click
0054216e +01e TestDivisionByZero.exe Vcl.StdCtrls        5953   +3 TCustomButton.Click
00543310 +00c TestDivisionByZero.exe Vcl.StdCtrls        6586   +1 TCustomButton.CNCommand
00528bb2 +2be TestDivisionByZero.exe Vcl.Controls        7591  +91 TControl.WndProc
0052dd79 +665 TestDivisionByZero.exe Vcl.Controls       10644 +170 TWinControl.WndProc
00541de0 +06c TestDivisionByZero.exe Vcl.StdCtrls        5786  +13 TButtonControl.WndProc
005287e8 +024 TestDivisionByZero.exe Vcl.Controls        7369  +10 TControl.Perform
0052ded3 +023 TestDivisionByZero.exe Vcl.Controls       10713  +12 DoControlMsg
0052e997 +00b TestDivisionByZero.exe Vcl.Controls       10991   +1 TWinControl.WMCommand
005b7089 +045 TestDivisionByZero.exe Vcl.Forms           6972   +6 TCustomForm.WMCommand
00528bb2 +2be TestDivisionByZero.exe Vcl.Controls        7591  +91 TControl.WndProc
0052dd79 +665 TestDivisionByZero.exe Vcl.Controls       10644 +170 TWinControl.WndProc
005b384c +710 TestDivisionByZero.exe Vcl.Forms           5017 +222 TCustomForm.WndProc
004b6df8 +014 TestDivisionByZero.exe System.Classes     18517   +8 StdWndProc
0052d284 +02c TestDivisionByZero.exe Vcl.Controls       10321   +3 TWinControl.MainWndProc
004b6df8 +014 TestDivisionByZero.exe System.Classes     18517   +8 StdWndProc

---------------------------

Tested on 4 différents versions of Delphi (XE, 10.3, 10.4 and 11.3) with or without Gexpert, madexcept etc. !


Like described here :
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Floating-Point_Exceptions

PS :
If I change V1:=10;
Project raise an EZeroDivide Exception !

I don't have a customized version of Delphi. It's clear this is not a Scripter issue, but something regarding Math operations and exceptions handling.

I'm not expert in math operations, maybe it has to do with the underlying Windows OS as well, I don't know. I know you can set exception mask flags in Math lib to enable/disable exceptions.

I have checked this with other developers and confirmed that NaN is displayed.

Tested on both VM (VMware) or physical Windows 10 and 11 in runtime => Same error.

But what about this :
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Floating-Point_Exceptions

Floating-point exceptions that can occur in Delphi or C++ applications are listed in the following table.
Undefined floating-point instruction or invalid mathematical operation.
For example, Sqrt(-1), ArcTan(10), 0/0, or Partial Loss of Precision.

And the problem it is in same Delphi TMS Scripter doesn't raise the same exception...

That's some math library configuration. Scripter doesn't do anything else besides dividing two Variant values.

Hello Wagner,

The project in attachment :
Each 0/0 :

Raise exception EInvalidOP

Each 1/0 :

Raise exception Floating point divide by 0

And I have :
GetExceptionMask=[exInvalidOp,exDenormalized,exUnderflow]

Do you have the same result ?

Does somebody get different result ?

ExceptionDivisionBy0.zip (1,7 Ko)

I tried your last project, but then I did it run in two different computers. I was using Delphi on a Windows ARM. Then I tested in a regular Windows x64.

Your project raises no exceptions from any button when running in Windows ARM. And it raises exception from all buttons in Windows x64.

But, I then tried to run your original script that tries to divide by 0, and it also raises exception in the Windows x64. So everything works as expected.

This raises exception ?

procedure TForm2.Button3Click(Sender: TObject);
var
  V1, V2: Variant;
  I1, I2: Integer;
begin
  V1 := 0;
  V2 := 0;
  ShowMessage(V1 / V2);

  I1 := 0;
  I2 := 0;
  ShowMessage(FloatToStr(I1 / I2));
end;

In which platform? Using which exception masks? To avoid miscommunication please send the whole project.

I speak about Win32 (+VCL)

Yes nice idea, in attachment a full project VCL WIN32 (no Firemonkey).

Both button (Delphi and scripter) raise Divide by zero exception. It's good.

But I found why :
uCEFApplicationCore.pas (CEF For Delphi)
And I have added it to scripter
do :
SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]);

TestDivisionByZerov2.zip (6,1 Ko)

You sample project raises exception in both buttons (scripter and Delphi). I assume all is solved now?

Yes my problem is about SetExceptionMask done by one of my libraries I use with scripter.
Finally It's doesn't come from CEF For Delphi but another one because I use more than 30 libraries.
Do you think that force ExceptionMask could be a good idea, like that :

try
 CurrentMask:=SetExceptionMask([ exDenormalized,  exOverflow, exUnderflow, exPrecision]); 
 Scripter.execute(..
finally
 SetExceptionMask(CurrentMask);
end;

I don't know, if that solves a issue for you, why not?