Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (529)
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  
  Double imprecision  (Read 1018 times)
0 Members and 1 Guest are viewing this topic.
Offline Jacob_

Junior Member


Projects: 3



« Posted 2010-05-26 16:21:34 »

I'm having trouble with some physics code. When the user presses an arrow key, it adds/subtracts 0.25 to the character's x velocity. Then the world steps, which should add/subtract 0.05 to get it closer to 0. This output of the velocity for each step shows what went wrong:
1  
2  
3  
4  
5  
6  
7  
8  
9  
0.0 //Not moving
0.25 //Pushed arrow key
0.45 //Held arrow key some more + game subtracted 0.05
0.4 //Subtracted 0.05
0.35000000000000003 //Subtracted 0.05
0.30000000000000004
0.25000000000000006
0.45000000000000007 //added 25, subtracted 0.04
0.4000000000000001

This gets things confused and the character will keep creeping along slowly in the same direction, while going through things it would normally collide with.
What might be wrong?

Rounding to 2 decimal places would work, but I want to see if there is a better way first.

When I jump, sometimes my feet get stuck in the ground, I'm assuming that's a related problem.
Offline JL235

JGO Coder


Medals: 10



« Reply #1 - Posted 2010-05-26 16:37:35 »

This is standard behaviour. Accurate floating point operations are really expensive, so the CPU only approximates them. This is why you get the added error. As long as you are never checking for an exact value (i.e. dblVal == 0.35) you can typically live with it.

You could use integers instead. Multiply all values (say by 100) so you can emulate the decimal values you need. When you use the value you convert to a double and divide back down by 100.

Second (and you might want to check if there is anything in the API that will do this already) you could build your own equals function where it takes an error value. I'm thinking something like:
1  
2  
3  
4  
5  
6  
public static boolean dblEquals(double a, double b, double error)
{
    return (a+error) > b && (a-error) < b;
}

dblEquals( a, b, 0.0001 );

Online princec

JGO Kernel


Medals: 339
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2010-05-26 16:59:33 »

I don't think 0.05 can actually be accurately represented as a float either. Dividing 1.0 by various powers-of-two and their combinations thereof should be accurately represented up to a point (no pun intended).

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jacob_

Junior Member


Projects: 3



« Reply #3 - Posted 2010-05-26 18:02:52 »

The main problem was that once the velocity got down to 0.049999999... it would get reduced to a number slightly under 0. The code would assume that it was a negative multiple of 0.05 and therefore add 0.05 to it to get it back to 0, thus going back to 0.49999999999999... again and creating an infinite loop.

Using an int instead sounds like a good idea.
Offline JL235

JGO Coder


Medals: 10



« Reply #4 - Posted 2010-05-26 18:36:51 »

Rather then adding 0.5 if the value is less then 0, why not add 0.5 if the value is less then -0.00001 ? You could also round values after making lots of add/removes to the nearest 0.05.

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.

ctomni231 (31 views)
2014-07-18 06:55:21

Zero Volt (27 views)
2014-07-17 23:47:54

danieldean (23 views)
2014-07-17 23:41:23

MustardPeter (24 views)
2014-07-16 23:30:00

Cero (39 views)
2014-07-16 00:42:17

Riven (41 views)
2014-07-14 18:02:53

OpenGLShaders (28 views)
2014-07-14 16:23:47

Riven (28 views)
2014-07-14 11:51:35

quew8 (25 views)
2014-07-13 13:57:52

SHC (61 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!