Because 'amount' is a floating point type and multiplying by powers of 10 (or even adding them) doesn't work quite well for numbers that are represented as powers of 2 (or something like that).
Instead of 0.5 any small number would do, 0.000001, for instance. Casting to int discards everything after the decimal point (and it might be something like .999999999)
You can read about it in misof's article:
http://www.topcoder.com/tc?module=Stati ... egersReals
Either avoid using floating points (if possible) or be paranoid while using them.
There's Goldberg's paper on the topic that I never bothered reading completely (maybe I should):
http://docs.sun.com/source/806-3568/ncg_goldberg.html
You can test it yourself with something like this (it's Java, but you get the idea):
Code: Select all
public class Test {
public static void main(String[] args) {
double a = 0.0;
for (int i = 0; i < 50; i++) {
System.out.println(i + " " + (20 * a));
a += 0.1;
}
}
}