CoercionSafeFloat

Wraps a floating point type that doesn't follow D permissive float conversion rules.

In D, as in C++, implicit conversion from double to float is allowed, leading to a possible precision loss. This can't happen when using this wrapper.

struct CoercionSafeFloat (
T
) if (
isFloatingPoint!T
) {}

Constructors

this
this(V v)

Prevent Coercion from construction.

Alias This

_value

Members

Functions

opAssign
void opAssign(V v)

Prevent Coercion from assignation.

opOpAssign
void opOpAssign(V v)

Prevent Coercion from operator assignation.

Examples

1 alias Float = CoercionSafeFloat!float;
2 alias Double = CoercionSafeFloat!double;
3 alias Real = CoercionSafeFloat!real;
4 
5 Float f; Double d; Real r;
6 
7 import std.math;
8 static assert(!__traits(compiles, f = (atan(1.0) * 4) / 2));
9 static assert( __traits(compiles, f = (atan(1.0f) * 4f) / 2f));
10 static assert(!__traits(compiles, d = (atan(1.0L) * 4L) / 2L));
11 static assert(__traits(compiles, d = f));
12 static assert(!__traits(compiles, f = d));
13 static assert(!__traits(compiles, d = r));
14 static assert(!__traits(compiles, d += r));
15 static assert(__traits(compiles, r *= d));

Meta