Ben Bacarisse
4/12/2011 4:46:00 PM
jacob navia <jacob@spamsink.net> writes:
> In the context of the C Containers library I am now implementing "ValArray"
>
> These containers store the basic numeric types (integers and floats)
> of the language.
>
> They support the basic operations that can be done with those types.
>
> The question is:
>
> ValArray A,B,C;
>
> // ...
>
> C = iValArrayDouble.SumTo(B,A);
>
> This is the equivalent of B += A in C++.
This confuses me. What's C? If it means B += A, there would be no
third sequence.
> The question is, what happens when Size(A) != Size(B). C++
> leaves that as "undefined", probably crashing. My library
> throws an error and does nothing.
>
> An alternative solution would be to use the length of B for the
> operation in question, extending the shorter array if necessary
> or discarding results that go beyond the length of B.
>
> Operation Identity elem.
> + 0
> - 0
> * 1
> / 1
> & 1
> | 0
> % 1 (for integers only)
> Xor ????
The identity for &, | and Xor is -1 converted to the width of whatever
integer type is involved. However, I may have missed something you are
getting at because Xor is written ^ and I'd expect the restriction to
apply to other operations and not just %. The identity for % is not 1.
There are lots of identities for % (at least for positive integers) but
none representable in the type of the operand since. For some type T,
one choice would be Tmax + 1.
If these are listed in order to show how to extend a short array, why
not just say that the elements of B beyond the length of A remain
unchanged?
> What do you think?
I'd look into providing a function to do the work rather than have SumTo
and MulTo and all the rest. For one thing, how can you know what
operations people will want to perform?
[By the way, it is likely this post will duplicate what others have
written by now, but I forgot to send it before getting distracted by
other tasks and I don't want to waste the typing so I will post it
without my usual check.]
<snip>
--
Ben.