Should this be correct behaviour?
Walter Bright
newshound1 at digitalmars.com
Thu Nov 29 12:37:22 PST 2007
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 realise that everything is behaving according to spec. But is it sensible?
Yes:
float a;
float b = a;
assert(b == a); // fails
And this is how floating point works.
More information about the Digitalmars-d
mailing list