This modified version of your function will give the results you are
looking for. I'm not sure what this means in terms of your general
conclusion, or in terms of best practice.
private function add(v1:Number, v2:Number, v0:Number):Number {
var v3:Number = v1+v2;
var status:String = (v0.toPrecision(3)==v3.toPrecision(3))?"OK"
:"failure" ;
trace("("+v1+ ") + ("+v2+") = "+v3.toPrecision(3)+", "+status);
return v3;
}
--- In [email protected], "Vadim Melnik" <[EMAIL PROTECTED]> wrote:
>
> Hello All,
>
> Its should be very simple, I saw posts related to this issue before
in this list, but somehow was unable to find it right now. Below is
listed simple test case for problem:
>
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="test()">
> <mx:Script><![CDATA[
> private function add(v1:*, v2:*, v0:*):*
> {
> var v3:* = v1+v2;
> var status:String = (v0==v3)?"OK":"failure";
> trace("("+v1+") + ("+v2+") = "+v3+", "+status);
> return v3;
> }
>
> private function test():void
> {
> add(1.2, 1, 2.2);
> add(1.2, -1, 0.2);
> add(82003.9, -16923, 65080.9);
> add(3.2, -1, 2.2);
> }
> ]]></mx:Script>
> </mx:Application>
>
> And result output:
>
> (1.2) + (1) = 2.2, OK
> (1.2) + (-1) = 0.19999999999999996, failure
> (82003.9) + (-16923) = 65080.899999999994, failure
> (3.2) + (-1) = 2.2, OK
>
> Is it bug or special behavior in AS3 by design, that second and
third "add" calls produces strange results instead of 0.2 and 65080.9.
In other words it's impossible to compare two calculated financial
values on Flex side .
>
>
> --
> Thanks,
> Vadim.
>