I wrote the functions below in literally just a few min, so, even though they do seem to work for me, I wouldn't necessarily say they are production ready.

Also, I would only use these if you will know that the numbers past the mantissa are of small amounts.

Oh yeah, and please tell me if I screwed up somewhere, or if you think you can do it better :)

function floatProduct(... nums){
    var ml:int = 0; //max length past mantissa
    var ln:uint = nums.length;
    var rsl:int; //right side length
    var p:Number = 1;
    var s:String;
    for (var i:uint = 0; i< ln; ++i) {
        s = nums[i].toString();
        rsl = s.length - s.indexOf('.');
        ml += rsl;
        p *= nums[i];
    }
    return Number(p.toFixed(ml));
}
function floatSum(... nums){
    var ml:int = 0; //max length past mantissa
    var ln:uint = nums.length;
    var rsl:int; //right side length
    var p:Number = 0;
    var s:String;
    for (var i:int = 0; i< ln; ++i) {
        s = nums[i].toString();
        rsl = s.length - s.indexOf('.');
        if (rsl > ml) {
            ml = rsl;
        }
        p += nums[i];
    }
    return Number(p.toFixed(ml));
}


-




On 12/14/2010 12:05 PM, Kerry Thompson wrote:
Adrian Zając wrote:


    trace (0.27 + 0.03);   // output -->  0.30000000000000004

Can anyone tell me why I get this weird result in output window?

As people have said, it's a problem with decimals. It's not a problem with
Flash--it's a problem with binary numbers.

Integers are accurate because all integers are a multiple of 1, and binary
does just fine with combining 0's and 1's, the only possibile values of a
digit in the binary system.

If you think of decimals as fractions, maybe it will help you understand the
issue. In binary, a digit can only be a multiple of 2: 1/2, 1/4, 1/8/ 1/16,
1/32, and so on. You can create numbers that aren't multiples of two by
adding multiple digits. For example, 1/4 + 1/8 = 3/8, or .375.

This works pretty well, until you get numbers with a lot of decimal places.
At some point, you will find a decimal that is impossible to make using
powers of 2.

Cordially,

Kerry Thompson
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to