Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (601)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
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 1181 times)
0 Members and 1 Guest are viewing this topic.
Offline Jacob_

Junior Devvie

Projects: 3

« Posted 2010-05-26 14: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:
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.45000000000000007 //added 25, subtracted 0.04

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 14: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:
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 Spiffy Duke »

Medals: 555
Projects: 3
Exp: 16 years

Eh? Who? What? ... Me?

« Reply #2 - Posted 2010-05-26 14: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 Devvie

Projects: 3

« Reply #3 - Posted 2010-05-26 16: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 16: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.

Jesse (10 views)
2015-07-29 04:35:27

Riven (33 views)
2015-07-27 16:38:00

Riven (16 views)
2015-07-27 15:35:20

Riven (20 views)
2015-07-27 12:26:13

Riven (10 views)
2015-07-27 12:23:39

BurntPizza (28 views)
2015-07-25 00:14:37

BurntPizza (39 views)
2015-07-24 22:06:39

BurntPizza (22 views)
2015-07-24 06:06:53

NoxInc (25 views)
2015-07-22 22:16:53

NoxInc (17 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!