Skip to content

What does it mean to have an RDom with a negative extent? #4385

Description

@alexreinking

I asked @abadams this question earlier and he said "That's a great question. I don't know". I'm opening this issue to determine an answer to that question.

Based on some limited manual testing, it appears that the current implementation computes a loop upper bound of min + extent - 1, which will be less than min if extent < 1. This makes sense for extent == 0, of course, but there are several options for how we could define the behavior of extent < 0.

  • It could be an error. Maybe we want to (or actually do) store extents in size_t at some point. A negative extent in a signed type would be cast to a very large positive number, or invoke UB if the unsigned type were too small.
  • It could be a no-op. This makes sense with the mathematical convention of half-open intervals being empty if the upper bound is not greater than the lower bound.
  • It could traverse the loop backwards. This is very weird, but other languages do similar things. Think of the xs[::-1] trick in Python.

Worth noting that "underflow", ie. casting a negative signed extent to an unsigned value is defined behavior in C++. See the C++11 standard, 4.7p2

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf

If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2^n where n is the number of bits used to represent the unsigned type). [Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation).— end note]

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew user-visible features or improvements to existing features.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions