hi i am defining a variable with the value of 1 and it increases by 0.25 i have used var Move = 1 if (Key.isDown(Key.UP)){Move += 0.25} when i run in debug mode i see numbers like 5.99999999994 and other numbers like that, it starts off alright going 1.25, 1.5,1.75 etc but then starts to have a lot of decimal places is there a way to fix this?
The mathematics amongst us can explain this better than I, but basically what you are seeing is a limitation of computer calculations. I could misuse such terms as floating point and binary conversion but I won't. If you want the answer to be accurate to two decimal places... then something like this... (others will have done better) roundingFunction = function(incoming){ adjNumber = Math.round(incoming*100); return adjNumber/100; } thisNumber = roundingFunction(5.7500000002) trace(thisNumber); hope that helps, cheers,
hi yeah that isn't my exact code i just wanted to know how to stop the number from going over two decimal places. in visual basic you juat define it to what you want like integer, single , double etc my real code is adding a number to a MC's rotation it adds 1 + AnotherVariable / 15 and that divided by 15 is what is producing the problem i was wondering how to limit it to 2 decimal places. im going to muck around with the Math.Round feature and see if it works, im at school now so i will upload the code tonight there are a number of things i am trying to do, i am trying to get the movment of a car to look real, instead for moving to the left it rotates a bit as well if you are going top speed you can't turn as sharp as you can when you are going slow also the screen zooms out as you accellerate and in when you let go. i am having trouble when i am at top speed and let go and press only left i want the road to slow down as well. anyway ill upload it the code is all over the place if you look at it could you tell me if im doing anything the hard way. Originally posted by: Newsgroup User I don't see what you see .. you must be doing something else other that what you are showing. I tried repeatedly adding 0.25 to a value and got exact results. That is expected because 0.25 is exactly represented in internal binary format. I think you may need to provide your exact code, so we can see where the rounding error is coming in.
I don't see what you see .. you must be doing something else other that what you are showing. I tried repeatedly adding 0.25 to a value and got exact results. That is expected because 0.25 is exactly represented in internal binary format. I think you may need to provide your exact code, so we can see where the rounding error is coming in.
It is simply the way things are with computers .. they store numbers in binary, and some numbers are not exactly represented in binary. So you will get errors. Use Math.round so that you can filter out the rounding errors.
Don't see what you're looking for? Try a search.
|