digitalmars.D.bugs - double comparison bug (and bug-fix)
- zwang <nehzgnaw gmail.com> Feb 19 2005
- zwang <nehzgnaw gmail.com> Feb 19 2005
- =?ISO-8859-1?Q?Thomas_K=FChne?= <thomas-dloop kuehne.THISISSPAM.cn> Feb 19 2005
- zwang <nehzgnaw gmail.com> Feb 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>
Feb 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);
Feb 19 2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (MingW32)
iD8DBQFCF1xG3w+/yD4P9tIRAluiAKCqV0DzCqL4qH7AOeNxWQWSNfUgfQCeKz5t
c5JJQZcUZ5ov5CfeiZ63zeI=
=+SXP
-----END PGP SIGNATURE-----
Feb 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.
Feb 19 2005








zwang <nehzgnaw gmail.com>