Three floating point questions
bearophile
bearophileHUGS at lycos.com
Wed Aug 25 16:56:05 PDT 2010
This program prints (dmd 2.048):
7ff4000000000000
7ffc000000000000
// program #1
import std.stdio: writeln, writefln;
import std.string: format;
void main() {
string toHex(T)(T x) {
string result;
ubyte* ptr = cast(ubyte*)&x;
foreach (i; 0 .. T.sizeof)
result = format("%02x", ptr[i]) ~ result;
return result;
}
writeln(toHex(double.init));
writefln("%x", cast(ulong)double.init);
}
Do you know what cast(ulong) is doing here?
---------------------------
This program prints (dmd 2.048)::
-nan
// program #2
import std.stdio: writeln;
void main() {
writeln(0.0 / 0.0);
}
Is it a bug of writeln?
---------------------------
Do you know why this isn't raising runtime errors?
// program #3
import std.math: FloatingPointControl;
import std.c.stdlib: atof;
void main() {
double d3 = atof("3.0");
double x; // nan
FloatingPointControl fpc;
fpc.enableExceptions(FloatingPointControl.severeExceptions);
double r = x * d3;
}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list