Java rounding the results of a division when it sh

2019-03-04 15:48发布

问题:

So I have some code for scaling graphics to the size of a users screen by dividing the size of an 'Ideal' screen by the size of the users screen. Hers is a code snippet of what I'm doing:

public void setScaleFactor(GameContainer ui) {
    scaleFactorWidth = 2880 / ui.getWidth();
    scaleFactorHeight = 1800 / ui.getHeight();

    System.out.println("Screen measured as: "
            + Integer.toString(ui.getWidth()) + "x"
            + Integer.toString(ui.getHeight()));

    System.out.println("Scale factors are: "
            + Double.toString(scaleFactorWidth) + " "
            + Double.toString(scaleFactorHeight));

    textScale = (scaleFactorWidth + scaleFactorHeight) / 2;

    System.out.println("Text scale is: " + Double.toString(textScale));
}

Now if i run this on my computer (Mac Book pro with a screen resolution of 1440x900) the out come is that "scaleFactorWidth" is set to 2.0 and "scaleFactorHeight" is set to 2.0, this is as expected since my screen is exactly half the size of the target. But if run this code on a computer with a different resolution screen then the scaleFactors seem to get rounded up, I ran a test on a screen with 1024x600 and the "scaleFactorWidth" was set to 2.0 "scaleFactorHeight" was set to 3.0 when it should have been 2.8125 x 3.0.

IS this some sort of rounding error within java and if so how do I fix it?

Edit: Thanks for all the help, I've realised I was being very stupid as all I needed to do was add .0 to 2880 and 1800.

回答1:

In these lines

scaleFactorWidth = 2880 / ui.getWidth();
scaleFactorHeight = 1800 / ui.getHeight();

The calculation itself is Integer-based (according to the later calls of Integer.toString()). Just the result is then casted to double.

Use this code instead, in order to have the actual computation use double values:

scaleFactorWidth = 2880.0 / ui.getWidth();
scaleFactorHeight = 1800.0 / ui.getHeight();

or

scaleFactorWidth = 2880.0 / (double)ui.getWidth();
scaleFactorHeight = 1800.0 / (double)ui.getHeight();


回答2:

If you're talking about this GameContainer class, getWidth() and getHeight() return an int.

So you have to cast it as double

scaleFactorWidth = (double)2880 / ui.getWidth();
scaleFactorHeight = (double)1800 / ui.getHeight();


回答3:

Cast it to Double.

   scaleFactorWidth = (Double)2880 / ui.getWidth();
    scaleFactorHeight = (Double)1800 / ui.getHeight();


回答4:

ui.getWidth and ui.getHeight() returns you int and when you are performing operation on int it returns you int again. So convert your int value to Double.

Double    scaleFactorWidth = new Double(2880) / new Double(ui.getWidth());
Double    scaleFactorHeight = new Double(1800) / new Double(ui.getHeight());

Check below sample,

public static void main(String[] args) {
            Double scaleFactorWidth =  new Double(2880) /  new Double(1024);
            Double scaleFactorHeight = new Double(1024) / new Double(600);


            System.out.println("Scale factors are: "
                    + Double.toString(scaleFactorWidth) + " "
                    + Double.toString(scaleFactorHeight));

            Double textScale = (scaleFactorWidth + scaleFactorHeight) / 2;

            System.out.println("Text scale is: " + Double.toString(textScale));
        }

output:

Scale factors are: 2.8125 1.7066666666666668
Text scale is: 2.2595833333333335