ProtoObject and comparison for equality and ordering

H. S. Teoh hsteoh at quickfur.ath.cx
Wed May 15 16:09:03 UTC 2019


On Wed, May 15, 2019 at 11:11:00AM -0400, Andrei Alexandrescu via Digitalmars-d wrote:
> On 5/15/19 1:30 AM, H. S. Teoh wrote:
> > On Tue, May 14, 2019 at 09:02:49PM -0400, Andrei Alexandrescu via Digitalmars-d wrote:
> > [...]
> > > Well there would be in some instances. People often implement
> > > comparisons as a - b currently, where a and b are int expressions.
> > [...]
> > 
> > FYI, the result of that is actually incorrect in some cases.
> > (Consider what happens when there is overflow involved, such as
> > int.max - int.min, and remember the type of the result.)
> 
> Thanks.
> 
> > So, not exactly the kind of code we should recommend, let alone bend
> > over backwards to support.
> 
> All integral arithmetic is subject to overflow, at every step of the
> way. A D coder implementing opCmp would need to figure when the range
> of the operators does not put comparisons at risk, and where it does,
> use e.g.  CheckedInt or a more elaborate approach.

In the case of integers, it's really just a matter of using the right
constructs that translate to the correct hardware instruction(s) that do
the right thing.


T

-- 
What do you call optometrist jokes? Vitreous humor.


More information about the Digitalmars-d mailing list