View source code
Display the source code in std/numeric.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Alias std.numeric.CustomFloat

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.

alias CustomFloat(uint bits) = CustomFloat!(CustomFloatParams!bits);
alias CustomFloat(uint precision, uint exponentWidth, CustomFloatFlags flags = CustomFloatFlags.ieee) = CustomFloat!(CustomFloatParams!(precision,exponentWidth,flags));
struct CustomFloat(uint precision, uint exponentWidth, CustomFloatFlags flags, uint bias)
  
if (((flags & flags.signed) + precision + exponentWidth) % 8 == 0 && (precision + exponentWidth > 0));

Alias CustomFloat

Alias CustomFloat

Struct CustomFloat

Constructors

NameDescription
this Initialize from any real compatible type.

Properties

NameTypeDescription
dig[get] size_t
epsilon[get] CustomFloat
get[get] FFetches the stored value either as a float, double or real.
im[get] CustomFloat
infinity[get] CustomFloat
max[get] CustomFloat
max_10_exp[get] int
min_10_exp[get] int
min_normal[get] CustomFloat
nan[get] CustomFloat
re[get] CustomFloat

Methods

NameDescription
opAssign Self assignment
opAssign Assigns from any real compatible type.
opBinary Convert the CustomFloat to a real and perform the relevant operator on the result
opBinaryRight Convert the CustomFloat to a real and perform the relevant operator on the result
opCmp Convert the CustomFloat to a real and perform the relevant operator on the result
opOpAssign Convert the CustomFloat to a real and perform the relevant operator on the result
opUnary Convert the CustomFloat to a real and perform the relevant operator on the result

Aliases

NameDescription
opCast Fetches the stored value either as a float, double or real.

Templates

NameDescription
toString Convert the CustomFloat to a real and perform the relevant operator on the result

Example

import std.math : sin, cos;

// Define a 16-bit floating point values
CustomFloat!16                                x;     // Using the number of bits
CustomFloat!(10, 5)                           y;     // Using the precision and exponent width
CustomFloat!(10, 5,CustomFloatFlags.ieee)     z;     // Using the precision, exponent width and format flags
CustomFloat!(10, 5,CustomFloatFlags.ieee, 15) w;     // Using the precision, exponent width, format flags and exponent offset bias

// Use the 16-bit floats mostly like normal numbers
w = x*y - 1;

// Functions calls require conversion
z = sin(+x)           + cos(+y);                     // Use unary plus to concisely convert to a real
z = sin(x.get!float)  + cos(y.get!float);            // Or use get!T
z = sin(cast(float) x) + cos(cast(float) y);           // Or use cast(T) to explicitly convert

// Define a 8-bit custom float for storing probabilities
alias Probability = CustomFloat!(4, 4, CustomFloatFlags.ieee^CustomFloatFlags.probability^CustomFloatFlags.signed );
auto p = Probability(0.5);

Authors

Andrei Alexandrescu, Don Clugston, Robert Jacques, Ilya Yaroshenko

License

Boost License 1.0.