How to resolve the algorithm Extreme floating point values step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Extreme floating point values step by step in the D programming language
Table of Contents
Problem Statement
The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity. The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe other), extreme floating point values in your language and assign them to variables. Print the values of these variables if possible; and show some arithmetic with these values and variables. If your language can directly enter these extreme floating point values then show it.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Extreme floating point values step by step in the D programming language
Source code in the d programming language
// Compile this module without -O
import std.stdio: writeln, writefln;
import std.string: format;
import std.math: NaN, getNaNPayload;
void show(T)() {
static string toHex(T x) {
string result;
auto ptr = cast(ubyte*)&x;
foreach_reverse (immutable i; 0 .. T.sizeof)
result ~= format("%02x", ptr[i]);
return result;
}
enum string name = T.stringof;
writeln("Computed extreme ", name, " values:");
T zero = 0.0;
T pos_inf = T(1.0) / zero;
writeln(" ", name, " +oo = ", pos_inf);
T neg_inf = -pos_inf;
writeln(" ", name, " -oo = ", neg_inf);
T pos_zero = T(1.0) / pos_inf;
writeln(" ", name, " +0 (pos_zero) = ", pos_zero);
T neg_zero = T(1.0) / neg_inf;
writeln(" ", name, " -0 = ", neg_zero);
T nan = zero / pos_zero;
writefln(" " ~ name ~ " zero / pos_zero = %f %s", nan, toHex(nan));
writeln();
writeln("Some ", T.stringof, " properties and literals:");
writeln(" ", name, " +oo = ", T.infinity);
writeln(" ", name, " -oo = ", -T.infinity);
writeln(" ", name, " +0 = ", T(0.0));
writeln(" ", name, " -0 = ", T(-0.0));
writefln(" " ~ name ~ " nan = %f %s", T.nan, toHex(T.nan));
writefln(" " ~ name ~ " init = %f %s", T.init, toHex(T.init));
writeln(" ", name, " epsilon = ", T.epsilon);
writeln(" ", name, " max = ", T.max);
writeln(" ", name, " -max = ", -T.max);
writeln(" ", name, " min_normal = ", -T.min_normal);
writeln("-----------------------------");
}
void main() {
show!float;
show!double;
show!real;
writeln("Largest possible payload for float, double and real NaNs:");
immutable float f1 = NaN(0x3F_FFFF);
writeln(getNaNPayload(f1));
immutable double f2 = NaN(0x3_FFFF_FFFF_FFFF);
writeln(getNaNPayload(f2));
immutable real f3 = NaN(0x3FFF_FFFF_FFFF_FFFF);
writeln(getNaNPayload(f3));
}
import std.math: FloatingPointControl;
void main() {
// Enable hardware exceptions for division by zero, overflow
// to infinity, invalid operations, and uninitialized
// floating-point variables.
FloatingPointControl fpc;
fpc.enableExceptions(FloatingPointControl.severeExceptions);
double f0 = 0.0;
double y1 = f0 / f0; // generates hardware exception
// unless it's compiled with -O)
}
You may also check:How to resolve the algorithm Draw a clock step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Largest proper divisor of n step by step in the BCPL programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the PowerBASIC programming language
You may also check:How to resolve the algorithm Square-free integers step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Chowla numbers step by step in the V (Vlang) programming language