www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - signed / unsigned oddity

reply Henning Hasemann <hhasemann web.de> writes:
Is this a bug or did I misunderstood arithmetic or cast()?

cast(int)(-5 * 1 * 0.41));  // result: -2
cast(int)(-5 * 1u);         // result: -5
cast(int)(-5 * 1u * 0.41)); // result: 1760936589

The last result seems strange to me. I use dmd 1.005.

Henning
Feb 07 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Henning Hasemann wrote:
 Is this a bug or did I misunderstood arithmetic or cast()?
 
 cast(int)(-5 * 1 * 0.41));  // result: -2
 cast(int)(-5 * 1u);         // result: -5
 cast(int)(-5 * 1u * 0.41)); // result: 1760936589
 
 The last result seems strange to me. I use dmd 1.005.
This is caused by the type conversion rules: typeof(-5 * 1u) == uint, so -5 * 1u * 0.41 == cast(uint)-5 * 0.41 == 0xfffffffb * 0.41 == 1760936589.31 which rounds to 1760936589 when cast to an int.
Feb 07 2007