[Issue 8672] %% operator

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Sep 16 17:05:36 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8672



--- Comment #5 from bearophile_hugs at eml.cc 2012-09-16 17:06:26 PDT ---
(In reply to comment #4)
> The point is, the programmer has to take "extra care" regardless.

I am not the only one making mistakes with the C-style modulus, I know of other
people that have had similar bugs. And I know teachers of C/Java languages that
have to take care of explaining the not intuitive nature of the C modulus.

But as usual I don't have real statistics that show that the Python-style
modulus leads to less bugs.


> It may be natural for your particular code example,

What I have shown is not an arbitrary and very specific example, it's an
example of a common usage. 


> The diversity of definitions for it strongly suggests that there is no
> "natural" defintion.

That's the not-adaptationist explanation
(http://en.wikipedia.org/wiki/Adaptationism ). An alternative explanation is
that they are just copying the semantics from precedent (C, Algol?) designs.
In the case of D the explanation is that it has copied C99, for practical
backward compatibility purposes. It's a scan of a cyclic structure (a cirular
array), where sometimes you go forward and sometimes you go backward, with
negative steps. While the presence of an array is specific, the need to walk
circular sequences is a common enough use case for the modulus among negative
numbers.


> D improves on the C/C++ situation by making it defined behavior, rather than
> undefined.

C makes its behavour defined since C99 :-)

I will keep using my function in D, because it has avoided me some bugs. Thank
you for your answers.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list