1

This code gives me wrong output, im not sure why, and I am going crazy.

    float correctX=((getWidth()/599)*x);
    float correctY=((getHeight()/740)*y);

The result I get is: x = 520 y = 900.

And logcat output log: DETAILS----------Width: 1080. Heigth: 1533. X Value: 520. Y Value: 450

Why ? I have no clue and Im going crazy, i dont even know if its the correct syntax anymore lol.

I use android studio btw

1 Answer 1

2

This is because of integer division (go to Confusing Divisions) inaccuracies. Cast a few values to floats to make it better.

 int correctX = (int)((1080f/599)*520);

OR

Just change correctX to a float

 float correctX = (1080f/599)*520;
Sign up to request clarification or add additional context in comments.

6 Comments

Still incorrect. This is how I write it: float correctX=((getWidth()/599)*x); float correctY=((getHeight()/740)*y); This is the logcat output I get: DETAILS----------Width: 1080. Heigth: 1533. X Value: 520. Y Value: 450
That's because there are still no floats on the right hand of your assignment. It should be: ` float correctX=((getWidth()/599f)*x)` @JozefM
What does float s actually mean ?
What the hell, it works! Lol thanks alot!!! Unsure what it means tho, some googling tells me it tells the system it should be a 32bit variable, but not sure why that makes a diffrence
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.