Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 1859 times)
0 Members and 1 Guest are viewing this topic.
Offline Vladiedoo
« Posted 2012-08-15 20: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: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-08-15 21: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
« Reply #2 - Posted 2012-08-15 21: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!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-08-15 21: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
« Reply #4 - Posted 2012-08-15 21: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: 56
Projects: 11


Monkey for a head


« Reply #5 - Posted 2012-08-15 21: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
    « Reply #6 - Posted 2012-08-15 22:10:18 »

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

    JGO Kernel


    Medals: 345
    Projects: 3
    Exp: 5 years


    I'm the King!


    « Reply #7 - Posted 2012-08-15 22: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 03:10:15 »

    For user interaction purpose I hide it with formatter Smiley

    Offline Grunnt

    JGO Wizard


    Medals: 68
    Projects: 8
    Exp: 5 years


    Complex != complicated


    « Reply #9 - Posted 2012-08-16 07: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

    Games published by our own members! Check 'em out!
    Legends of Yore - The Casual Retro Roguelike
    Offline Riven
    « League of Dukes »

    JGO Overlord


    Medals: 793
    Projects: 4
    Exp: 16 years


    Hand over your head.


    « Reply #10 - Posted 2012-08-16 07: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
    Offline Grunnt

    JGO Wizard


    Medals: 68
    Projects: 8
    Exp: 5 years


    Complex != complicated


    « Reply #11 - Posted 2012-08-16 07: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.

    Pages: [1]
      ignore  |  Print  
     
     
    You cannot reply to this message, because it is very, very old.

     

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

    The first screenshot will be displayed as a thumbnail.

    Dwinin (21 views)
    2014-09-12 09:08:26

    Norakomi (55 views)
    2014-09-10 13:57:51

    TehJavaDev (66 views)
    2014-09-10 06:39:09

    Tekkerue (33 views)
    2014-09-09 02:24:56

    mitcheeb (54 views)
    2014-09-08 06:06:29

    BurntPizza (38 views)
    2014-09-07 01:13:42

    Longarmx (24 views)
    2014-09-07 01:12:14

    Longarmx (30 views)
    2014-09-07 01:11:22

    Longarmx (28 views)
    2014-09-07 01:10:19

    mitcheeb (37 views)
    2014-09-04 23:08:59
    List of Learning Resources
    by Longor1996
    2014-08-16 10:40:00

    List of Learning Resources
    by SilverTiger
    2014-08-05 19:33:27

    Resources for WIP games
    by CogWheelz
    2014-08-01 16:20:17

    Resources for WIP games
    by CogWheelz
    2014-08-01 16:19:50

    List of Learning Resources
    by SilverTiger
    2014-07-31 16:29:50

    List of Learning Resources
    by SilverTiger
    2014-07-31 16:26:06

    List of Learning Resources
    by SilverTiger
    2014-07-31 11:54:12

    HotSpot Options
    by dleskov
    2014-07-08 01:59:08
    java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
    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!