sbinder
02-07-2009, 05:10 PM
I've noticed a problem with the Math.floor function in OL 4.2. I know this is an issue in swf8 - haven't tried it in other platforms yet.
You can replicate the issue by setting a variable to (i.e. v) to "7.12". Then look at Math.floor(100 * v). You'll get 711 instead of 712. You can also see the problem if you type Math.floor( 100 * "7.12" ) into the debugger.
Obviously, the problem has to do with the floating point representation of the string "7.12". If you take Math.floor( 100 * 7.12 ) you get the correct answer (712). If you set a varible to 100 * "7.12" and Debug.write the variable, it prints as 7.12. But then if you take Math.floor() of the variable - you get 711. Does anybody have any suggestions as to a workaround? the only thing I can think of is something like (horrifying):
Math.floor( ( 100 * "7.12") + 0.000001 ).
It should work correctly if the input number (i.e. 7.12) is guaranteed to be only a few decimal places long. But it's really, really ugly.
You can replicate the issue by setting a variable to (i.e. v) to "7.12". Then look at Math.floor(100 * v). You'll get 711 instead of 712. You can also see the problem if you type Math.floor( 100 * "7.12" ) into the debugger.
Obviously, the problem has to do with the floating point representation of the string "7.12". If you take Math.floor( 100 * 7.12 ) you get the correct answer (712). If you set a varible to 100 * "7.12" and Debug.write the variable, it prints as 7.12. But then if you take Math.floor() of the variable - you get 711. Does anybody have any suggestions as to a workaround? the only thing I can think of is something like (horrifying):
Math.floor( ( 100 * "7.12") + 0.000001 ).
It should work correctly if the input number (i.e. 7.12) is guaranteed to be only a few decimal places long. But it's really, really ugly.