Thread overview
double comparison bug (and bug-fix)
Feb 19, 2005
zwang
Feb 19, 2005
zwang
Feb 19, 2005
Thomas Kühne
Feb 19, 2005
zwang
February 19, 2005
<test-code>
void main(){
	TypeInfo ti = typeid(double);
	double a = 0, b = .1;
	assert(ti.compare(&a, &b)<0);	
}
</test-code>


This is due to a bug in phobos/std/typeinfo/ti_double.d.
<code>
#18    int compare(void *p1, void *p2)
#19    {
#20    return cast(int)(*cast(double *)p1 - *cast(double *)p2);
#21    }
</code>

A quick fix:
<code>
    int compare(void *p1, void *p2)
    {
    double d = (*cast(double *)p1 - *cast(double *)p2);
    return d>0?1:d<0?-1:0;
    }
</code>
February 19, 2005
zwang wrote:
> <test-code>
> void main(){
>     TypeInfo ti = typeid(double);
>     double a = 0, b = .1;
>     assert(ti.compare(&a, &b)<0);   }
> </test-code>
> 
> 
> This is due to a bug in phobos/std/typeinfo/ti_double.d.
> <code>
> #18    int compare(void *p1, void *p2)
> #19    {
> #20    return cast(int)(*cast(double *)p1 - *cast(double *)p2);
> #21    }
> </code>
> 
> A quick fix:
> <code>
>     int compare(void *p1, void *p2)
>     {
>     double d = (*cast(double *)p1 - *cast(double *)p2);
>     return d>0?1:d<0?-1:0;
>     }
> </code>


The following files should also be patched similarly:
File ti_double.d:
20              return cast(int)(*cast(double *)p1 - *cast(double *)p2);
File ti_float.d:
20              return cast(int)(*cast(float *)p1 - *cast(float *)p2);
File ti_idouble.d:
20              return cast(int)(*cast(double *)p1 - *cast(double *)p2);
File ti_ifloat.d:
20              return cast(int)(*cast(float *)p1 - *cast(float *)p2);
File ti_ireal.d:
20              return cast(int)(*cast(real *)p1 - *cast(real *)p2);
File ti_real.d:
20              return cast(int)(*cast(real *)p1 - *cast(real *)p2);
February 19, 2005
zwang wrote:

| zwang wrote:
|
|> <test-code>
|> void main(){
|>     TypeInfo ti = typeid(double);
|>     double a = 0, b = .1;
|>     assert(ti.compare(&a, &b)<0);   }
|> </test-code>
|>
|>
|> This is due to a bug in phobos/std/typeinfo/ti_double.d.
|> <code>
|> #18    int compare(void *p1, void *p2)
|> #19    {
|> #20    return cast(int)(*cast(double *)p1 - *cast(double *)p2);
|> #21    }
|> </code>
|>
|> A quick fix:
|> <code>
|>     int compare(void *p1, void *p2)
|>     {
|>     double d = (*cast(double *)p1 - *cast(double *)p2);
|>     return d>0?1:d<0?-1:0;
|>     }
|> </code>

float, double and real are known bugs.
Wasn't there some patch floating around half a year ago?!?

| The following files should also be patched similarly:
| File ti_idouble.d:
| 20              return cast(int)(*cast(double *)p1 - *cast(double *)p2);
| File ti_ifloat.d:
| 20              return cast(int)(*cast(float *)p1 - *cast(float *)p2);
| File ti_ireal.d:
| 20              return cast(int)(*cast(real *)p1 - *cast(real *)p2);

Added to DStress as
http://dstess.kuehne.cn/run/sort_13.d
http://dstess.kuehne.cn/run/sort_14.d
http://dstess.kuehne.cn/run/sort_15.d

Thomas
February 19, 2005
Thomas Kühne wrote:
> float, double and real are known bugs.
> Wasn't there some patch floating around half a year ago?!?

Sorry for my unawareness of these early bug reports.
I still wonder why Phobos isn't patched yet.