-3

I keep getting 0 with the following equation, and I'm sure it is something I am missing, but this has been bugging me for the past few days.

int BASE_SIZE = 8;
Point screenSize = new Point(1440,2000);

mMaxSize = mScreenSize.x/BASE_SIZE;

// This line is the line causing issue.
int surfaceViewSize = mMaxSize * ((BASE_SIZE-1)/BASE_SIZE);

This is regardless of if I make the variable an integer, if I use Math.round, I make it a double, anything. I can not for the life of me figure this out.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
miversen33
  • 573
  • 5
  • 19
  • There's a method that can already get you the screen size, by the way – OneCricketeer Aug 01 '17 at 13:57
  • 3
    `(BASE_SIZE-1)/BASE_SIZE` is zero because Java uses integer division. Maybe you mean `mMaxSize * (BASE_SIZE-1) / BASE_SIZE;` – khelwood Aug 01 '17 at 13:57
  • yes I use that method to get the screen size, I just didn't feel like inputting that in as this is not in an activity. And the goal here is to get mMaxScreenSize .x * 7/8. Hence the mMaxScreenSize.x * ((8-1)/8) – miversen33 Aug 01 '17 at 14:00
  • Please change your title according to the [ask]. – Ivar Aug 01 '17 at 14:01
  • whoever tagged this, thank you! I knew it was an issue with integer division but couldn't figure how to resolve it. Casting to a double corrected the issue. – miversen33 Aug 01 '17 at 14:07

2 Answers2

3

this integer division here:

(BASE_SIZE-1)/BASE_SIZE

result to be

int surfaceViewSize = mMaxSize * 0;

you need to cast one of the operands into a double or float

replace your operations with:

mMaxSize = 1.0*mScreenSize.x/BASE_SIZE;
int surfaceViewSize = mMaxSize * ((1.0*BASE_SIZE-1)/BASE_SIZE);
xenteros
  • 15,586
  • 12
  • 56
  • 91
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • *"you need to **cast** one of the operands into a double or float"* and then you proceed without casting *anything*, instead multiplying by the double number 1, and lack of cast making the code not even compile. – Andreas Aug 01 '17 at 14:01
2
int surfaceViewSize = (mMaxSize * (BASE_SIZE-1))/BASE_SIZE;

Try this its just a braces issue

xenteros
  • 15,586
  • 12
  • 56
  • 91
PRADEEP Kumar
  • 232
  • 1
  • 7
  • 3
    No, it's not. It's all about integer division – xenteros Aug 01 '17 at 13:59
  • To be fair, changing the braces will change the way the integer division is done but in the end xenteros is right: the basic problem is integer division. – Thomas Aug 01 '17 at 14:00
  • You don't actually need the outer parentheses at all. But yes, this is the best answer. There's no point in converting everything to `float` or `double` when you're going to assign the result back to an `int` anyway. You just have to be smart about the order you do things. – Dawood ibn Kareem Aug 01 '17 at 14:03