www.digitalmars.com         C & C++   DMDScript  

D - [bug] typeinfo (array compare)

[bug] typeinfo (array compare)

in my view, typeinfo is used array operation.
to compare array, "compare" function in typeinfo is used for each element in
array.
some compare function returns
(int)(*(type *)p1 - *(type *)p2)
this imprement has problem.

test program:

int main(char args[][]) {
long[1][1] a;
long[1][1] b;
a[0][0] = 0x100000000;
b[0][0] = 1;
if (a<b) {
printf("a<b");
} else if (a>b) {
printf("a>b");
} else {
printf("a=b");
}
return 0;
}

result:
a<b

floating point:
ti_float,double,real,i* and ti_A* (ti_c* and ti_Ac* are good)

int compare(void *p1, void *p2)
{
return *(float *)p1 - *(float *)p2;
}

ex. compare(0.1,0.2)
-> (int)(0.1-0.2) = (int)(-0.1) = 0 
-> equal

32bit overflow/underflow:
ti_int,uint,dchar,ptr and ti_A*

ex. compare(int.max,int.min) 
-> (int)(0x7fffffff - 0x80000000) = (int)(0xffffffff) = -1 
-> int.max<int.min


fix:
if (*(float *)p1 < *(float *)p2) return -1;
else if (*(float *)p1 > *(float *)p2) return 1;
return 0;
or
float a = *(float *) p1;
float b = *(float *) p2;
return a < b ? -1 : a > b ? 1 : 0;
etc.

different between ti_* and ti_A*:

ti_long (good)
if (*(long *)p1 < *(long *)p2)
return -1;
else if (*(long *)p1 > *(long *)p2)
return 1;
return 0;

ti_Along (only check low word)
int result = s1[u] - s2[u];
if (result)
return result;


ti_C (Object) (bad?)
int equals(void *p1, void *p2)
{
Object o1 = *(Object*)p1;
Object o2 = *(Object*)p2;

return o1 == o2 || (o1 && o1.opCmp(o2) == 0);
}

ti_AC (Object[]) (good)
if (s1[u] === s2[u] ||
(s1[u] !== null && s2[u] !== null && s1[u].opEquals(s2[u])))

BERO
Jan 17 2004