www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - double comparison bug (and bug-fix)

reply zwang <nehzgnaw gmail.com> writes:
<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>




</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
parent reply zwang <nehzgnaw gmail.com> writes:
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>




 </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
parent reply =?ISO-8859-1?Q?Thomas_K=FChne?= <thomas-dloop kuehne.THISISSPAM.cn> writes:
-----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>




|> </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
parent zwang <nehzgnaw gmail.com> writes:
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