Add Promoted to std.traits.#4419
Conversation
std/traits.d
Outdated
| if(isScalarType!T) | ||
| { | ||
| static if (isFloatingPoint!T) | ||
| enum int precision = T.mant_dig; |
There was a problem hiding this comment.
static assert(is(typeof(real.mant_dig) == int));8af73c7 to
d9435f0
Compare
std/traits.d
Outdated
| /** | ||
| Detect whether `T` is a scalar type that promotes to an integral type (that is, a built-in | ||
| integral, character, or boolean type). | ||
| */ |
There was a problem hiding this comment.
Is "fixed point" the right term for this? I would expect a number with a fractional part (with fixed precision) behind that name.
There was a problem hiding this comment.
Is "fixed point" the right term for this?
I intended it as the opposite of isFloatingPoint, since isFixedPoint == (isScalarType && !isFloatingPoint). Do you have a suggestion for a better name?
I would expect a number with a fractional part (with fixed precision) behind that name.
All matching types have a fixed precision of zero places after the decimal point.
There was a problem hiding this comment.
Interesting rationale, but the converse of floating point is not fixed point; the latter is a mere alternative for implementing approximations of real numbers. Integrals and their ilk are a distinct matter.
How frequent is this likely to be? Should it include integral enum types?
There was a problem hiding this comment.
Interesting rationale, but the converse of floating point is not fixed point...
I am not particularly attached to the isFixedPoint name - I'm just waiting for someone to suggest something better: descriptive and meaningfully shorter than the implementation.
How frequent is this likely to be [used]?
When converting functions into templates, people tend to replace the parameter int a with A a ... if (isIntegral!A). The two are not fully compatible, though, because int a will accept bool, char, and wchar as well.
The range of types accepted by A a ... if (isFixedPoint!A) is much closer to those accepted by int a. In my opinion most template functions with isIntegral in the constraints are candidates for conversion to isFixedPoint, instead.
Of course, the whole thing is just a hack to work around a weakness in the language: the inability to declare a template function in such a way that type deduction works properly when implicit conversions are involved.
Should it include integral
enumtypes?
I considered it unwise to deviate from the behaviour of the existing isWhatever traits, so it does the same thing that isIntegral does: it gives the same answer for an enum type as it would for the enum's base type.
|
LGTM You should email Andrei as these are name additions |
std/traits.d
Outdated
| /** | ||
| Returns the number of full binary digits of precision representable by the scalar type `T`. | ||
| */ | ||
| template precision(T) |
There was a problem hiding this comment.
What would be an example of unifying FP and integral precision?
There was a problem hiding this comment.
A floating-point type F can losslessly represent all possible values of an integral type N if-and-only-if precision!T >= precision!N.
This trait is used by smartOp to help compute the optimal common numeric type for multi-argument functions like binary().
|
Anyone else? |
Unresolved:
|
|
To move forward with this PR, I suggest it be reduced to only add |
|
(removing my label after posting my response) |
|
Ping @tsbockman |
d9435f0 to
f9a8405
Compare
isFixedPoint, precision, and Promoted to std.traits.Promoted to std.traits.
f9a8405 to
8f0a6d4
Compare
|
@JackStouffer I have removed |
8f0a6d4 to
8711a11
Compare
|
Andrei has approved the addition and the code, no one else has objected, so I'm pulling |
|
Auto-merge toggled on |
Add a
Promoted!Ttrait which evaluates to the type used for (non-unary) arithmetic operations on typeT. Example:static assert(is(typeof(Promoted!ubyte == int)));.