14 Replies Latest reply on Sep 10, 2012 8:24 AM by pauland

# Help with calculation

Hi,

I have a  calcualtion with result negative infinity (in.fin.ity.00).

I try to use isNaN(calculation) for validation but is not working.

tempCal = myCaculation

if (isNaN(tempCal)){

finalCal = 0;

}else{

finalCal = tempCal;

}

How can I validate this result?

Best,

• ###### 1. Re: Help with calculation

wrap the calculation in try.. catch.

• ###### 2. Re: Help with calculation

Thanks for you reply and help.

How? like this...

try

{

myCalculation

}

catch (error:*)

{

finalCal = 0;

}

finally

{

finalCal = myCalculation;

}

• ###### 3. Re: Help with calculation

Yes, give it a go. Strictly speaking, finally is not required.

• ###### 4. Re: Help with calculation

try

{

finalCal = myCalculation

}

catch (error:*)

{

finalCal = 0;

}

• ###### 5. Re: Help with calculation

The catch doesn't get any error, any other idea?

private function init():void{

var finalCal:Number;

var myCal:Number= 24/0;

try

{

myCal;

}

catch (error:*)

{

finalCal = 0;

}

finally

{

finalCal = myCal;

}

}

• ###### 6. Re: Help with calculation

Try without finally, the same issue, catch doesn't get any error:

private function init():void{

var finalCal:Number;

var myCal:Number= 24/0;

try

{

finalCal = myCal;

}

catch (error:*)

{

finalCal = 0;

}

}

• ###### 7. Re: Help with calculation

private function init():void{

var finalCal:Number;

try

{

finalCal = 24/0;

}

catch (error:*)

{

finalCal = 0;

}

}

• ###### 8. Re: Help with calculation

It's clever enough to spot the div by zero, but here's a clue.

trace(finalCal.toString()+" "+ (finalCal==NaN));

I get "Infinity false"

• ###### 9. Re: Help with calculation

Yes, that is my issue.

How can I validate that infinity?  (my original question)

Thanks for trying...

• ###### 10. Re: Help with calculation

I've already shown you, but here you go:

private function init():void{

var finalCal:Number;

try

{

finalCal = 24/0;

}

catch (error:*)

{

finalCal = 0;

}

if ( finalCal==NaN){

finalCal = 0;

}

}

• ###### 11. Re: Help with calculation

Refer to the APIs for function clarification:  http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/package.html#isNaN()

A non-zero number when divided by 0 is inputted into isNaN the return is false (even though you would expect it to be true since the result traces as 'infinity').

The original question is a little vague, but from the way I understand it, there's no need for try-catch.  This should work:

if(isFinite(YOUR_NUMBER) && !isNaN(YOUR_NUMBER))

{

//if YOUR_NUMBER is finite and it is NOT NaN... you're here.

}

else

{

//else, YOUR_NUMBER is either infinite or it is NaN...

}

• ###### 12. Re: Help with calculation

I got this

1098: Illogical comparison with NaN. This statement always evaluates to false.

• ###### 13. Re: Help with calculation

You got the solution NMed. First time i see isFinite.

Thanks !

• ###### 14. Re: Help with calculation

Good job NMed!