View Full Version : math.floor issue?
cristip
03-07-2005, 03:58 PM
for(var i =0, j=0; i < 15; i++)
{
j += .2;
trace("Math.floor("+j+") = " + Math.floor(j))
}
outputs:
...
Math.floor(1.6) = 1
Math.floor(1.8) = 1
Math.floor(2) = 1
Math.floor(2.2) = 2
Math.floor(2.4) = 2
...
can somebody tell me why Math.floor(2) = 1 ?
devonair
03-07-2005, 04:26 PM
hey, cristip,
it's because of the way Flash handles fractional numbers.. Drag your code out to 100 instead of 15 and you'll see that you wind up with numbers like 32.7999999999999 (rather than 32.8).. So, really your 2 is 1.9999999999999.. I forget why it does this, but there are other threads dealing with it, if you're in the mood for searching..
d.
cristip
03-10-2005, 10:09 AM
well if so why when i trace
trace("Math.floor("+j+") = " + Math.floor(j))
the first j is 2 and the second is 1.9999999... ?
I've run searches.. but no result.. does anyone knows a topic that discuss the same problem?
Thank you
devonair
03-10-2005, 11:48 AM
http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_13989
sneeuwitje
03-10-2005, 01:40 PM
As long as you work with integers there's no issue. Following adaption to your code is beating around the bush quite a bit, but will perform as expected ;):for(var i =0, j=0; i < 150; i += 10)
{
j += 2;
trace("Math.floor("+j/10+") = " + Math.floor(j/10))
}
output:...
Math.floor(1.8) = 1
Math.floor(2) = 2
Math.floor(2.2) = 2
...
sneeuwitje
03-10-2005, 02:01 PM
or easier:for (var i=0, j=0; i<15; i++, j=i*0.2)
{
trace("Math.floor("+j+") = " + Math.floor(j))
}
If you get it, I get it :confused:
cristip
03-10-2005, 02:45 PM
http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_13989
:) Thank you!
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.