(See also news://news.digitalmars.com:119/[email protected])
Test case:
-----------------------------------------
import std.math, std.conv;
void main() {
float a = 1.0f;
float b = 0.0f;
float c = a/b;
assert(!isFinite(c), to!string(c));
}
-----------------------------------------
$ dmd x
$ ./x
$ dmd -O x
$ ./x
[email protected](6): 1.42171e-38
----------------
5 x 0x00009495 onAssertErrorMsg + 73
6 x 0x0001343e _d_assert_msg + 26
7 x 0x0000288d _Dmain + 73
8 x 0x00013b3b extern (C) int rt.dmain2.main(int, char**).void runMain() + 23
9 x 0x000136e5 extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate()) + 29
10 x 0x00013b83 extern (C) int rt.dmain2.main(int, char**).void runAll() + 59
11 x 0x000136e5 extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate()) + 29
12 x 0x0001367f main + 179
13 x 0x00002839 start + 53
----------------
-----------------------------------------
The same happens for 'double' and 'real'.
Comment #1 by kennytm — 2011-04-29T00:36:35Z
Actually the whole divide-by-zero scene is broken. With integers this should raise a Floating Point Exception, but instead it is set to the address of some variable in the backend.
---------------------------------------------
import std.stdio;
void main() {
int a = 1;
int b = 0;
int c = a/b;
writefln("%x", c);
}
---------------------------------------------
$ dmd x
$ ./x
Floating point exception
$ dmd -O x
$ ./x
9a9d34
---------------------------------------------
Comment #2 by jens.k.mueller — 2011-05-05T15:03:25Z
I would have expected that
-----------------------------------------
import std.math, std.conv;
void main() {
float c = 1.0f/0.0f;
assert(!isFinite(c), to!string(c));
}
-----------------------------------------
fails, too.
But it doesn't.
Comment #3 by clugdbug — 2011-05-06T00:46:52Z
In the backend, divide by zero goes to an error message which is commented out.
Comment #4 by kekeniro2 — 2013-11-05T18:50:10Z
Raised Severity.
This prevents safe programmming.
Comment #5 by mingodad — 2014-07-20T14:11:42Z
Actually floating point exception doesn't seem to be trapped:
Floating point exception (core dumped)
--------
import std.stdio;
void func1()
{
int x, z = 2/x;
}
void func2()
{
try { func1(); }
catch(Exception e) { writeln("catch %s", e.msg);}
finally {writeln("finally: func2"); throw new Exception("on func2");}
}
void func3()
{
func2();
}
void main()
{
func3();
}
--------