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

    Help with calculation

    jfb00 Level 3

      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?

      Thanks in advance.

      Best,

        • 1. Re: Help with calculation
          pauland Level 4

          wrap the calculation in try.. catch.

          • 2. Re: Help with calculation
            jfb00 Level 3

            Thanks for you reply and help.

            How? like this...

            try

            {

                 myCalculation

            }

            catch (error:*)

            {

                 finalCal = 0;

            }

            finally

            {

                 finalCal = myCalculation;

            }

            • 3. Re: Help with calculation
              pauland Level 4

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

              • 4. Re: Help with calculation
                pauland Level 4

                Oops, your code is wrong.

                 

                try

                {

                     finalCal = myCalculation

                }

                catch (error:*)

                {

                     finalCal = 0;

                }

                • 5. Re: Help with calculation
                  jfb00 Level 3

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

                   

                  import mx.controls.Alert;

                             

                              private function init():void{

                                  var finalCal:Number;

                                  var myCal:Number= 24/0;

                                 

                                  try

                                  {

                                      myCal;

                                  }

                                  catch (error:*)

                                  {

                                      finalCal = 0;

                                  }

                                  finally

                                  {

                                      finalCal = myCal;

                                  }

                                 

                                  Alert.show(finalCal.toString());

                              }

                  • 6. Re: Help with calculation
                    jfb00 Level 3

                    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;

                                    }

                                   

                                    Alert.show(finalCal.toString());

                                }

                    • 7. Re: Help with calculation
                      pauland Level 4

                      How about ..

                       

                      private function init():void{

                                      var finalCal:Number;

                                     

                                      try

                                      {

                                          finalCal = 24/0;

                                      }

                                      catch (error:*)

                                      {

                                          finalCal = 0;

                                      }

                                     

                                      Alert.show(finalCal.toString());

                                  }

                      • 8. Re: Help with calculation
                        pauland Level 4

                        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
                          jfb00 Level 3

                          Yes, that is my issue.

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

                          Thanks for trying...

                          • 10. Re: Help with calculation
                            pauland Level 4

                            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;

                                            }

                                            Alert.show(finalCal.toString());

                                        }

                            • 11. Re: Help with calculation
                              NMed

                              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
                                jfb00 Level 3

                                I got this

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

                                • 13. Re: Help with calculation
                                  jfb00 Level 3

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

                                  Thanks !

                                  • 14. Re: Help with calculation
                                    pauland Level 4

                                    Good job NMed!