There is a method for raising a number to a power.

public static double raisedToPower(double a, double b) { return Math.pow(a, b); } 

As a result of the calculation, the method can return NaN and Infinity values, which in turn can break the program at the call point.

In my understanding, I have to process it inside a method (If NaN or Infinity, then return a key that can be processed outside the method). That is the question, what can be returned as a key or is there another way to handle this situation?

 public static double raisedToPower(double a, double b) { double res = Math.pow(a, b); if (Double.isNaN(res) || Double.isInfinite(res)) { return key; } return res; } 

    2 answers 2

    In general, you understood the idea correctly, but interpreted it incorrectly.

    NaN and Infitity - this is already such a "key"

      I recommend to raise an exception and catch it outside:

       if(Double.isNaN(res)){ throw(new Exception("NAN")); } else if(Double.isInfinite(res)){ throw(new Exception("INFINITE")); } 

      Further, in the place of using the method, we simply write an exception handler using try, catch, finally.