1

I think I need to cast this code. I need to return an int but I have to multiply by percents and dont want them to be cut off.
Should I cast it like this brains = (double)brains - (this.getBrains() * 0.01);
Also should I cast the return statement at the end? I'm weak at casting and i've been trying to find examples to help but they're not helping.

          if(attacker >= attackee)
            {
            switch (weapon)
                {
                    case 't':
                        brains = brains + (other.getBrains() * 0.01);
                        attack = other.getBrains() * -0.01;
                        other.addBrains(attack); 
                        break;

                    case 's':
                        brains = brains + (other.getBrains() * 0.05);
                        attack = other.getBrains() * -0.05;
                        other.addBrains(attack); 
                        break;  

                    case 'c':
                        brains = brains + (other.getBrains() * 0.10);
                        attack = other.getBrains() * -0.10;
                        other.addBrains(attack); 
                        break;  

                    case 'k':
                        brains = brains + (other.getBrains() * 0.20);
                        attack = other.getBrains() * -0.20;
                        other.addBrains(attack); 
                        break;
                }
         }
        else 
            {
                switch (weapon)
                {
                    case 't':
                        brains = brains - (this.getBrains() * 0.01);
                        attack = this.getBrains() * 0.01;
                        other.addBrains(attack);
                        break;

                    case 's':
                        brains = brains - (this.getBrains() * 0.01);
                        attack = this.getBrains() * 0.01;
                        other.addBrains(attack);
                        break;  

                    case 'c':
                        brains = brains - (this.getBrains() * 0.01);
                        attack = this.getBrains() * 0.01;
                        other.addBrains(attack);
                        break;

                    case 'k':
                        brains = brains - (this.getBrains() * 0.01);
                        attack = this.getBrains() * 0.01;
                        other.addBrains(attack);
                        break;      
                }
            }                   
    return attack;
}
JessNicole27
  • 63
  • 1
  • 7
  • What type are `brains` and `attack`? Integers? – MasterOfBinary Nov 11 '13 at 23:43
  • brains is an instance variable and attack is a local variable – JessNicole27 Nov 11 '13 at 23:47
  • I just need it to return an integer but when i did it without any casting i got this error: possible loss of precision, so seeing this im assuming I need casting – JessNicole27 Nov 12 '13 at 00:00
  • 2
    Just out of curiosity, why are you switching on the weapon in your `else` statement, and then doing the exact same thing in every `case`? – StriplingWarrior Nov 12 '13 at 00:00
  • @StriplingWarrior the summarized description for the question is: If the attacking zombie is at a level equal to or higher than the zombie being attacked, then the attacking zombie gains brains and the zombie being attacked loses that amount. Ithe attacking zombie is at a lower level than the other zombie, then the transfer occurs in reverse (i.e. the other zombie gains the brains and the attacking zombie loses brains). The method should return the number of brains the attacking zombie gained (will be a positive number) or lost (as a negative number). – JessNicole27 Nov 12 '13 at 00:08
  • It doesn't address any casting issues, but may I suggest that rather than hardcoding all those numbers and repeating code, you determine an attack variable and a brains variable based on the two conditions (whether attacker ≤ attackee, and what the weapon is) and then just have one computation. E.g., http://pastebin.com/KQKckQNH . – Joshua Taylor Nov 12 '13 at 00:09
  • @JessNicole27: My point is that your program uses different multipliers for different weapons in the first `switch` statement, but not in the second one. I'm guessing that's a bug in your application, but if it's not, then the whole second switch statement can simply be replaced with the contents of one of your `case`s. – StriplingWarrior Nov 12 '13 at 16:19

3 Answers3

1

Casting from double to int will always truncate, so might not be exactly what you're after. That is, (int)3.812 will give you 3, when you probably want 4.

You don't need to cast to double either, as "int + int * double" will return a double.

The easiest way forward is to use Math.round, which will round the bigger values up. Be aware that doubles round to longs, and floats round to ints though.

Floats will almost certainly give you the precision you're after, so the simplest solution for you is:

brains = Math.round(brains - this.getBrains() * 0.01f);

Edit: if you do want the higher precision afforded by doubles, then Math.round will give you a long, which you'll need to cast to an int:

brains = (int)Math.round(brains - this.getBrains() * 0.01d); 

Note that the constant has changed from 0.01f to 0.01d changing the precision of the entire calculation.

Martin
  • 3,703
  • 2
  • 21
  • 43
  • I'm not sure using a float here is a good idea. Double can fit the entire range of an int so no precision is lost when casting `int -> double -> int`. Not so for floats, if the number is large enough casting to a float will actually lose precision. – Radiodef Nov 12 '13 at 00:05
  • Given the problem domain, assuming the questioner isn't a brain surgeon, I'm assuming the precision isn't that important. Will update my answer to include the higher-precision solution. – Martin Nov 12 '13 at 00:10
  • It probably doesn't really matter it's just true that precision can actually be lost. Simple test: `for (float i = 0f; i <= 65f; i += 1f) System.out.println((int)(Integer.MAX_VALUE - i));` – Radiodef Nov 12 '13 at 00:22
0

Suppose you want to subtract a 100th of the brains. You can do that in different ways:

  • Round up: brains = brains - (this.getBrains() / 100);
  • Round to the nearest brain: brains = brains - (int)((double)this.getBrains() * 0.01 + 0.5);

Of course, the first way can always be done with casting if you'd like, and I'm not sure whether casting twice is faster or slower than integer division.

Since attack is an integer and the return value is as well, you don't need to cast the return value.

MasterOfBinary
  • 245
  • 1
  • 12
0

Java automatically promotes lesser data types if there is a greater data type in the same expression. If you do this:

brains = brains + (other.getBrains() * 0.01);

You don't need to explicitly cast to a double at all because 0.01 is a double and that part of the expression is evaluated first (parenthesized/multiplication). If brains is an int you will need to cast the expression back to an int to make the assignment. Do it like this:

brains = (int)Math.rint(brains + (other.getBrains() * 0.01));

And of course make sure your integers are not small (< 100 for the operations you are doing).

Radiodef
  • 37,180
  • 14
  • 90
  • 125