quick question.
when i run my program, why does java not add properly?
when I run my program I get answers like:
your heads total is: 0.15000000000000002
your tails total is: 0.35
why doesn't it compute to 0.15?
publicclass CoinDriver
{
publicstaticvoid main(String[] args)
{
double headsp = 0;
double tailsp = 0;
Coin[] p =new Coin[10];
for(int i = 0; i < p.length; i++)
{
p[i] =new Coin();
p[i].tossCoin();
String temp = p[i].getSide();
if(temp.equals("heads"))
headsp++;
else
tailsp++;
}
headsp = headsp * .01;
tailsp = tailsp * .01;
double headsn = 0;
double tailsn = 0;
Coin[] n =new Coin[10];
for(int i = 0; i < n.length; i++)
{
n[i] =new Coin();
n[i].tossCoin();
String temp2 = n[i].getSide();
if(temp2.equals("heads"))
headsn = headsn + .05;
else
tailsn = tailsn + .05;
}
System.out.println("your heads total is: " + headsn);
System.out.println("your tails total is: " + tailsn);
}
}
[2144 byte] By [
LuckY7a] at [2007-11-26 17:31:51]

"double" is imprecise.
In particular, you can not represent "0.05" precisely using "double".
So every time you use "0.05", you introduce a bit of rounding error.
You can represent "0.5" precisely, "0.25" precisely,
but you cannot represent "0.05" precisely
(this is due to the way IEEE floating point number is defined)
Try using an "int" or a "long" whenever possible.
As post above, doubles can only hold a finite set of values.
Read [url=http://docs.sun.com/source/806-3568/ncg_goldberg.html ]What Every Computer Scientist Should Know About Floating-Point Arithmetic[/url] for more info.
You have three options, depending on your needs. For most you can simple use Formatter to output the data to an acceptable number of digits ( DecimalFormat ).
For money math, you can look use int/long and do all the math in pence rather than pounds.
Or you can BigDecimal.
mlka at 2007-7-8 23:59:52 >

Why are you creating an array of Coin objects? You're not doing anything with the array. Also, you're repeating the same code, which is a questionable practice. Extract the similar portions of your code to their own methods.
Example:
public class CoinDriver {
enum Coin {
QUARTER(25), DIME(10), NICKEL(5), PENNY(1);
private final int value;
Coin(int value) { this.value = value; }
public int value() { return value; }
};
public static void main(String[] args) {
for (Coin c : Coin.values()) {
doFlips(c, 10);
}
}
static void doFlips(Coin c, int flipCount) {
java.util.Random rand = new java.util.Random();
int total = 0;
for (int i = 0; i < flipCount; i++) {
total += (rand.nextBoolean()) ? c.value() : 0;
}
System.out.printf("Value of heads for %s: $%5.2f\n", c, total / 100.0);
System.out.printf("Value of tails for %s: $%5.2f\n", c, (flipCount * c.value() - total) / 100.0);
}
}
~
what command would be easiest to round these #'s? I tried changing to an INT for just nickels and dividing by 100, but it always gives a 0.
> what command would be easiest to round these #'s? I
> tried changing to an INT for just nickels and
> dividing by 100, but it always gives a 0.
I assume you mean, let's say you have an int variable called cents.
And you print it like this:
System.out.println(cents/100);
Then of course you'll get 0 (when 0 <= cents <= 99),
and you'll get 1 (when 100 <= cents <= 199).
That's because you're performing an integer division,
which discards the remainder.
What you should do is to do both "/100" and "%100".
System.out.printf("%d dollars and %d cents.\n", cents/100, cents%100);