Should this be correct behaviour?
Derek Parnell
derek at psych.ward
Thu Nov 29 13:14:58 PST 2007
On Thu, 29 Nov 2007 12:37:22 -0800, Walter Bright wrote:
> Janice Caron wrote:
>> Should this be correct behaviour?
>
> Yes.
>
>> float[] f = new float[1];
>> float[] g = f.dup;
>> assert(f == g); /* Passes */
>> assert(f[0] == g[0]); /* Fails */
>>
>> Certainly it is correct for the second assert to fail, because f[0]
>> and g[0] both contain nan, and as we all know, (nan != nan).
>> Essentially, the second assert (correctly) fails because the elements
>> have not been initialised, and so we can't do the compare.
>>
>> My question is, shouldn't the first assert also fail?
>>
>> Put another way, how can two arrays be considered equal, if their
>> elements are not considered equal?
>
> You are comparing two array *references* which point to the same array.
> The two references clearly are the same. (f == g) does not compare the
> contents of the arrays.
I don't get this. Doesn't the .dup function create a NEW array and
therefore the .ptr values inside the references are different. Thus if
(f.ptr != g.ptr) how can (f == g)?
--
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell
More information about the Digitalmars-d
mailing list