Can an operation defined as implementation-defined behavior have multi possible execution?

173 Views Asked by At

[conv.fpint] p2 says

If the value being converted is in the range of values that can be represented but the value cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value.

[intro.abstract] p2 says

Certain aspects and operations of the abstract machine are described in this document as implementation-defined (for example, sizeof(int)). These constitute the parameters of the abstract machine. Each implementation shall include documentation describing its characteristics and behavior in these respects. Such documentation shall define the instance of the abstract machine that corresponds to that implementation (referred to as the “corresponding instance” below).

[intro.abstract] p5 says

A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input. However, if any such execution contains an undefined operation, this document places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation).

Consider that there is an implementation, which cannot represent the value 16777217 exactly in an object of float type, and the choice for such a value can be either 16777216 or 16777218. For all odd times of the evaluation of the conversion, it chooses the lower representable value, and for all even times of evaluation of the conversion, it chooses the higher representable value. Is this a conforming implementation?

float a1 = 16777217; // 16777216
float a2 = 16777217; // 16777218
.
.
.
float a<2n-1> = 16777217; // 16777216
float a<2n> = 16777217; // 16777218

Update:

A resemble case:

sizeof(int);  // 4
sizeof(int);  // 8
.
.
.
sizeof(int);  // 4
sizeof(int);  // 8

For all odd times of the evaluation of sizeof(int), the results are all 4, and for all even times of evaluation of sizeof(int), the results are all 8.

1

There are 1 best solutions below

9
On

Technically, yes. You already quoted the relevant text so my answer will not include any further quotes. implementation-defined means that the implementation must document the behaviour that it implements.

There is no further constraint on the behaviour than specified in the text you quoted. It would be a quality-of-implementation issue whether the implementation documents a single outcome or a range of outcomes and under what circumstances, etc.