Board index » delphi » Re: Chicken sacrificed

Re: Chicken sacrificed


2005-10-02 04:29:11 AM
delphi77
"Florent Ouchet" <XXXX@XXXXX.COM>wrote
Quote
Ben Hochstrasser writes:
>Florent Ouchet writes:
>>so ((A mod N) + N) mod N = (A mod N) mod N = A mod N
>I...am...NomAd...
Exact only with positive numbers...
Anyway it is faster to do: Abs(A) mod N
Abs will mirror, not wrap.
 
 

Re: Chicken sacrificed

somebody writes:
Quote
Even worse. Then it simply will reference the wrong value for
underflows (unless your array happens to be exactly 256, 65536 or
2^32 elements long).
Sure, if you dec(i) at 0 you will get some ludicrous (probably out-of-bounds)
index value, but who says (i mod N) is any better? If the index value is
essentially uncontrolled, you will get some unpredictable in-bounds value, but
so what? Does that make your program more correct?
Your original question was 'how to wrap indices across array bounds' - I
guess I don't really understand what you're trying to achieve, and why.
--
Kristofer
 

Re: Chicken sacrificed

somebody writes:
Quote
Abs will mirror, not wrap.
My abs don't. They just sort of hang there. ;-)
--
Ben
 

Re: Chicken sacrificed

While you obviously see this as such an important feature that "it ought to
be in the language", I get the impression most folks here don't see this.
However, help *may* be on the way...
C++ allows you to 'overload' the [] operator, so you can write your own
function to perform array access. This is how most C++ programmers would
recommend you implement the required "feature"
"Dexter" apparently will include operator overload for delphi in (at least)
the Win32 personality. I don't know if the array access operator will be
overloadable (Danny???), but if so, then you should be able to define a
class with an array that wraps/mirrors or whatever you want it to do.
I admit I am not a huge fan of operator overloading, as it is frequently
abused. At one level it is just a shorthand for function calls, with the
result that the programmer using the overloaded operators frequently fails
to appreciate that there's more going on than meets the eye...
- Roddy
 

Re: Chicken sacrificed

somebody writes:
Quote
What do you guys for wrapping indices across array bounds? The safe
one is A[((i mod N) + N) mod N] where N=High(A)+1 but that is too ugly
(and inefficient, and gets uglier for more efficient methods). Any
fast (ASM maybe) way of an always positive mod function (when N>0)?
More importantly, wouldn't such a mod make sense as a language
feature?
I would suspect that
if (i>0)
i mod N
else
N - (abs(i) mod N)
is slightly faster.
--
Jan Nordén
Senior Software Architect
MDA products group