Computer Science Canada

Possible Loss of Precision

Author:  mapleleaf31 [ Mon Mar 28, 2005 12:51 pm ]
Post subject:  Possible Loss of Precision

So basically I want a vary basic could that when you click a button, the figure on screen will get smaller, it works fine until I try and use decimals to try and get the figure to shrink a bit more slowly so I know it has to do with that, can anyone help me?

code:
   
public void EnChange()
    {
        if (faceX == 8)
        {
                faceHeight = (faceHeight*0.8);
                faceWidth = (faceWidth*0.8);
                eyeHeight = (eyeHeight*0.8);
                eyeWidth = (eyeWidth*0.8);
                eyeX = (eyeX*0.8);
                eyeY = (eyeY*0.8);
        }
        repaint();              // repaint the screen
    }


Like I said, it would run fine if I took the 0. out of it and had all the values as just 8, is there not a way I can't put decimals there, or maybe a different way to get it to shrink a bit more slowly?

Author:  wtd [ Mon Mar 28, 2005 1:48 pm ]
Post subject: 

If your variables are integers then multiplying them by floating point numbers might have different results that you think.

For instance, multiply 9 by 0.8. You get 7.2. But your variables are integers, so you end up storing 7.

Also, a hint:

code:
myVariable = (myVariable * 0.8);


Equals:

code:
myVariable = myVariable * 0.8;


Or:

code:
myVariable *= 0.8;


: