Do you like bounded integrals?

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Wed Aug 24 08:58:36 PDT 2016


On 08/24/2016 05:05 AM, Lodovico Giaretta wrote:
> On Wednesday, 24 August 2016 at 00:40:15 UTC, Andrei Alexandrescu wrote:
>> That wouldn't work for e.g. NaN. A NaN wants to "steal" a value but
>> only if that's not available. It's complicated.
>
> Ok, I get your point. WithNaN should not waste the "official range" when
> there is a huge wasted representable range that can be exploited to
> choose the special NaN value.

Yes, thanks for divining the meaning from the poor explanation.

> While this would be a very good thing, it
> poses a problem.
>
> If a hook is allowed to special-case values outside the official range,
> then it may happen that composing two hooks, they both special-case the
> same value, leading to wrong results.
> For example, in some statistical oriented environments, the special
> value NA (not available), very similar to NaN, is used to represent
> missing input data; a WithNA hook may decide to use the same policy used
> by WithNaN to reserve its NA value, causing corruption, as the same
> value has two meanings.

Yes, that is indeed a potential problem.

> So, the first solution is that hooks should always reserve special
> values from the edges of the official range, and expose to the higher
> layer a correctly reduced official range.
>
> The second solution is having two ranges: the "official range" and the
> "usable range", where the usable range is the full representable range
> minus the special values already used. Hooks must take special values
> from the usable range and reduce it accordingly, while leaving the
> official range intact.
>
> The third (more complex) solution, which leaves the official range
> intact, is that hooks should take special values from wherever outside
> the official range and in some way communicate to the higher level which
> values are already taken. This is not impossible nor conceptually
> difficult, but it may not be worth.

The fourth solution is to document hooks appropriately and acknowledge 
the fact (which is already true) that not all hooks can work together.


Andrei



More information about the Digitalmars-d mailing list