1 import std.math : sin, cos; 2 3 // Define a 16-bit floating point values 4 CustomFloat!16 x; // Using the number of bits 5 CustomFloat!(10, 5) y; // Using the precision and exponent width 6 CustomFloat!(10, 5,CustomFloatFlags.ieee) z; // Using the precision, exponent width and format flags 7 CustomFloat!(10, 5,CustomFloatFlags.ieee, 15) w; // Using the precision, exponent width, format flags and exponent offset bias 8 9 // Use the 16-bit floats mostly like normal numbers 10 w = x*y - 1; 11 12 // Functions calls require conversion 13 z = sin(+x) + cos(+y); // Use unary plus to concisely convert to a real 14 z = sin(x.get!float) + cos(y.get!float); // Or use get!T 15 z = sin(cast(float) x) + cos(cast(float) y); // Or use cast(T) to explicitly convert 16 17 // Define a 8-bit custom float for storing probabilities 18 alias Probability = CustomFloat!(4, 4, CustomFloatFlags.ieee^CustomFloatFlags.probability^CustomFloatFlags.signed ); 19 auto p = Probability(0.5);
Allows user code to define custom floating-point formats. These formats are for storage only; all operations on them are performed by first implicitly extracting them to real first. After the operation is completed the result can be stored in a custom floating-point value via assignment.