Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (307)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Slick2D MouseWheel and Decimal Rounding  (Read 1126 times)
0 Members and 1 Guest are viewing this topic.
Offline Vladiedoo

Senior Member


Medals: 11



« Posted 2012-08-15 22:29:13 »

Hi, for the following Slick2D code I have two questions.

(zoom is a float and zoom2 is a double)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
   public void mouseWheelMoved(int change) {
      if (change >= 120) {
         Info.zoom += .1;
         Info.zoom2 += .1;
      } else {
         Info.zoom -= .1;
         Info.zoom2 -= .1;
      }
      System.out.println(Info.zoom + " " + Info.zoom2);
   }


  • I've seen this happen before when I used Java2D but why is "int change" measured by 120s?
  • In the Console my numbers eventually look like .7999995 instead of .8 or .2000000000000015 instead of 2, why does this happen, is there a way to fix this?

Thanks for any help you guys can provide.
Offline ra4king

JGO Kernel


Medals: 292
Projects: 2


I'm the King!


« Reply #1 - Posted 2012-08-15 23:09:05 »

1. Don't know, this might be mouse-specific and there might be a method that returns a resolution somewhere
2. That's basic floating point errors, you will learn to love them Smiley

Offline Vladiedoo

Senior Member


Medals: 11



« Reply #2 - Posted 2012-08-15 23:18:58 »

1. Don't know, this might be mouse-specific and there might be a method that returns a resolution somewhere
2. That's basic floating point errors, you will learn to love them Smiley

1. I remember testing two different mouses but got the same numbers.
2. Does that mean I'd want to use whole numbers and then subtract if I wanted decimal accurate numbers?
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline ra4king

JGO Kernel


Medals: 292
Projects: 2


I'm the King!


« Reply #3 - Posted 2012-08-15 23:35:08 »

There's no way you can get exactly accurate numbers. If you see how floating point values are represented on the byte-level, you'll understand Smiley

Offline Vladiedoo

Senior Member


Medals: 11



« Reply #4 - Posted 2012-08-15 23:45:01 »

Thanks for replying but this sparks some questions. I'll be sure to do some research on byte level representation but if you don't mind me asking..

  • I'm sure, for example, NASA doesn't use Java but how would they calculate decimal accurate numbers? Wouldn't they need to do that for simulations and calculations?
  • Is this a Java specific issue or a issue that computers as a whole share? Also my TI-84 Calculator does decimal precision just fine(or so I think), how does it work around it?
Offline Orangy Tang

JGO Kernel


Medals: 48
Projects: 11


Monkey for a head


« Reply #5 - Posted 2012-08-15 23:53:53 »

    • I'm sure, for example, NASA doesn't use Java but how would they calculate decimal accurate numbers? Wouldn't they need to do that for simulations and calculations?
    • Is this a Java specific issue or a issue that computers as a whole share? Also my TI-84 Calculator does decimal precision just fine(or so I think), how does it work around it?

    For arbitrary precision numbers you'd use something like BCD ( http://en.wikipedia.org/wiki/Binary-coded_decimal ). If you're using java then there's BigDecimal ( http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html ) which handles all the icky details for you.

    However, unless you're sending probes to mars or handling large quantities of money these are probably overkil. Really you need to read up on how floating point numbers are represented because it's common to all languages, not just java. This is a pretty comprehensive doc: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

    For most game-related stuff you can usually get away with using floats but rounding when displaying, or using integers with a fixed decimal place (ie. you assume everything is multiplied by, say, 100).

    [ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
    Offline Vladiedoo

    Senior Member


    Medals: 11



    « Reply #6 - Posted 2012-08-16 00:10:18 »

    Awesome, thank you both, this was extremely helpful  Grin.
    Offline ra4king

    JGO Kernel


    Medals: 292
    Projects: 2


    I'm the King!


    « Reply #7 - Posted 2012-08-16 00:31:59 »

    Also, your TI-84 calculator most likely is doing some tricks to hide the errors from you. Same with the Windows calculator Wink

    Offline ReBirth
    « Reply #8 - Posted 2012-08-16 05:10:15 »

    For user interaction purpose I hide it with formatter Smiley

    Offline Grunnt

    JGO Knight


    Medals: 23
    Projects: 6


    Complex != complicated


    « Reply #9 - Posted 2012-08-16 09:25:03 »

    @Orangy Tang: thanks! That was very insightful.

    Edit:funny, I do know about bits and bytes, but I never really stopped to consider that even doing something as simple as adding two 0.1f floats actually results in a small rounding error:

    1  
    2  
    3  
    4  
    5  
    6  
    7  
    public static void main(String[] args) {
          float a = 0;
          for (int i = 0; i < 10; i++) {
             a = a + 0.1f;
          }
          System.out.println(a);
       }


    This gives as output '1.0000001', instead of the - naively - expected '1'.

    This makes me happy. Am I weird? Grin

    Grunnt Games  Play my 4K games or follow development of Constellation Control, a back-to-basics 4X RTS.
    Games published by our own members! Check 'em out!
    Play the free demo of Revenge of the Titans!
    Offline Riven
    « League of Dukes »

    JGO Overlord


    Medals: 463
    Projects: 4


    Hand over your head.


    « Reply #10 - Posted 2012-08-16 09:29:22 »

    I'm going to reply to this from a whole other point of view.

    You most likely don't want zooming done by adding an subtracting a zoom-factor.

    Instead, you want zoom levels, which raise the zoom factor to a certain power:
    1  
    2  
    3  
    4  
    5  
    6  
    7  
    8  
    9  
    10  
    11  
    12  
    13  
       Info.zoomLevel = 0;

       public void mouseWheelMoved(int change) {
          if (change >= 120) {
             Info.zoomLevel++
          } else {
             Info.zoomLevel--;
          }

          // each zoom level decreases/increases the zooming with 5%
         Info.zoomFactor = Math.pow(1.00 + 0.05, Info.zoomLevel);
          System.out.println(Info.zoomFactor);
       }


    Which instantly gets rid of the accumulation of floatingpoint errors. (!)

    Hi, appreciate more people! Σ ♥ = ¾
    Learn how to award medals... and work your way up the social rankings
    Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
    Offline Grunnt

    JGO Knight


    Medals: 23
    Projects: 6


    Complex != complicated


    « Reply #11 - Posted 2012-08-16 09:41:42 »

    @Riven, thanks for that advice, I'm also going to take it. This discussion just made me realize the current zoom function in the game I'm working on is flawed (some quick hack I never got around to do properly), even though it works on my machine. This looks like a good way to fix it.

    Grunnt Games  Play my 4K games or follow development of Constellation Control, a back-to-basics 4X RTS.
    Pages: [1]
      ignore  |  Print  
     
     
    You cannot reply to this message, because it is very, very old.

    Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
     
    Get high quality music tracks for your game!

    Add your game by posting it in the WIP section,
    or publish it in Showcase.

    The first screenshot will be displayed as a thumbnail.

    The invasion has landed! On Mars! And you're there to beat 'em!
    mrbenebob (16 views)
    2013-06-19 14:55:23

    BrassApparatus (24 views)
    2013-06-19 08:52:37

    NegativeZero (27 views)
    2013-06-19 03:31:52

    NegativeZero (30 views)
    2013-06-19 03:24:09

    Jesse_Attard (34 views)
    2013-06-18 22:03:02

    HeroesGraveDev (70 views)
    2013-06-15 23:35:23

    Vermeer (69 views)
    2013-06-14 20:08:06

    davedes (71 views)
    2013-06-14 16:03:55

    alaslipknot (63 views)
    2013-06-13 07:56:31

    Roquen (88 views)
    2013-06-12 04:12:32
    Smoothing Algorithm Question
    by UprightPath
    2013-05-28 02:58:26

    Smoothing Algorithm Question
    by UprightPath
    2013-05-28 02:57:33

    Complex number cookbook
    by Roquen
    2013-04-24 12:47:31

    2D Dynamic Lighting
    by Oskuro
    2013-04-17 16:46:12

    2D Dynamic Lighting
    by Oskuro
    2013-04-17 16:45:57

    2D Dynamic Lighting
    by Oskuro
    2013-04-17 16:23:20

    Noise (bandpassed white)
    by Roquen
    2013-04-05 17:36:01

    Noise (bandpassed white)
    by Roquen
    2013-04-03 16:17:38
    Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!