0

This does not calculate properly in floats.

System.out.println(tileWidth);
System.out.println(Gdx.graphics.getWidth());
float drawPPU = 200 / Gdx.graphics.getWidth();
float drawWidth = drawPPU * tileWidth;
float drawHeight = drawPPU * tileHeight;
System.out.println(drawPPU);
System.out.println(drawWidth);

Gives this output:

125
360
0.0
0.0

But when i cast my INTS like FLOATS this it does calculate properly:

float drawPPU = 200 / (float)Gdx.graphics.getWidth();
float drawWidth = drawPPU * (float)tileWidth;
float drawHeight = drawPPU * (float)tileHeight;

I am pretty sure I never did these basic calculations like this and it took my a while until i found what was actually wrong with my viewport. I mean:

 (float = 200 / 360) == (float = 200.0 / 360.0)

So is there something wrong with my setup/compiling or am i mixing things up in some way?

Madmenyo
  • 8,389
  • 7
  • 52
  • 99
  • 2
    if everything on the right hand side is an integer, then it remains an integer until it gets assigned to a float. You need to coerce at least one of the values on the right hand side to a float to get floating point math. – Anya Shenanigans Dec 07 '14 at 14:56
  • The link to the duplicate has a title nobody with this problem will ever find. Not that mine is any better ;) – Madmenyo Dec 07 '14 at 15:03
  • @MennoGouw: It's a general problem description but there isn't anything better when you don't know what the underlying problem is. Googling "*java float division*" yields several several posts on the subject. – Jeroen Vannevel Dec 07 '14 at 15:06
  • @JeroenVannevel True, funny thing is i have been programming for a while now with java but never ran into this. Or i did without figuring out the actual bug :). – Madmenyo Dec 07 '14 at 15:14

0 Answers0