scaleform.minarto.com

소숫점 곱셈 버그 본문

ActionScript

소숫점 곱셈 버그

미나토 2007. 11. 8. 10:18
뭐 생각해보면 당연한거긴 하지만 덧셈에 있던 소숫점 계산 버그는 곱셈에서도 나타나기 시작했다

trace(0.075 * 3)
////////////////////////
0.22499999999999998


뭐 곱셈이란게 덧셈의 연속이니 말이다.

결국 요즘에 정말 쓸 데없는 두 클래스를 만들어서 사용하고 있다.


    public class CommaAdd
    {
        public static function add(num0:Number, num1:Number):Number
        {
            if(Math.round(num0) != num0 || Math.round(num1) != num1)
            {
                var commaCnt0:uint = (String(num0).split(".").length == 2) ? String(num0).split(".")[1].length : 0;
                var commaCnt1:uint = (String(num1).split(".").length == 2) ? String(num1).split(".")[1].length : 0;
                var commaCnt:uint = (commaCnt0 >= commaCnt1) ? commaCnt0 : commaCnt1;

                var sum:Number = (num0 * Math.pow(10, commaCnt) + num1 * Math.pow(10, commaCnt)) / Math.pow(10, commaCnt);
            }
            else
            {
                sum = num0 + num1;
            }
           
            return sum;
        }
    }

    public class CommaMultiplication
    {
        public static function cal(num0:Number, num1:Number):Number
        {
            if(Math.round(num0) != num0 || Math.round(num1) != num1)
            {
                var commaCnt0:uint = (String(num0).split(".").length == 2) ? String(num0).split(".")[1].length : 0;
                var commaCnt1:uint = (String(num1).split(".").length == 2) ? String(num1).split(".")[1].length : 0;
                var commaCnt:uint = (commaCnt0 >= commaCnt1) ? commaCnt0 : commaCnt1;

                var sum:Number = (num0 * Math.pow(10, commaCnt) * num1 * Math.pow(10, commaCnt)) / Math.pow(10, commaCnt) / Math.pow(10, commaCnt);
            }
            else
            {
                sum = num0 * num1;
            }
           
            return sum;
        }
    }


어도비...정말 나를 귀찮게 하고 있다