Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24
  1. #21
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Method for calculation Cauchy Inequality with fully implemented handler

    Quote Originally Posted by AndreB View Post
    What makes you think so? Why should execeptions be used in this particular example?

    To my best knowledge defensive programming (Defensive programming - Wikipedia, the free encyclopedia) is a good programming practice! I don't see any optimism here ;-)
    Defensive programming is based around you writing code to protect you from methods you call.
    In this case you are writing the method itself, in which an illegal argument should throw an IllegalArgument exception...it is, after all, illegal.
    Then, should the exception occur in testing, you should try and figure out why you let an IllegalArgument get into this method...


    Quote Originally Posted by AndreB View Post
    If you look closely into the code, i do not return a "0" value. In the examples above I use Double.NaN as return value for calculations which is actually also a standard behaviour for bad calculations in java.

    It would be nice, if you would provide an example of how you would use exceptions in that particular example.
    NaN is just as bad. The caller still has to check.
    It is the equivalent of returning null on an error.
    That is poor practice.
    If a method expects arguments of a certain range (and the example given implied that was the case) then arguments outside that range should be classed as illegal. Testing should (and the aforementioned defensive programming) ensure these exceptions do not occur. If they do then fix the source of the problem.

  2. #22
    iliyapolak is offline Member
    Join Date
    Jan 2012
    Posts
    14
    Rep Power
    0

    Default Re: Method for calculation Cauchy Inequality with fully implemented handler

    Another method for cosecant calculation here I did not use catch blocks.
    Input checking logics throws exceptions.

    Java Code:
        public static double cosecantApprox(double value) throws IllegalArgumentException , ArithmeticException{
        	
        	double x = value;
        	int i,j,k;
        	int l = 0;
        	 int exp = 0;
        	 double term = 1/x; 
        	 double cosecant = 0f;
        	 int factor = 0;
        	 double sum = 0f;
        	 double[] pow = new double[12];
        	 double[] csc = new double[12];
        	 double[] coeff = {
        			           0.16666666666666666666666666666667,
        			           0.01944444444444444444444444444444,
        			           0.00205026455026455026455026455026,
        			           0.00104993386243382043650793650794,
        			           2.3469650205761316872418595679012e-5,
        			           7.1224535923609997684043268331463e-7,
        			           1.5661391322766984136380961777787e-8,
        			           2.609753939394591951900734969518e-10,
        			           3.4120417942992754810472370028262e-12,
        			           3.5916434929557749619266471215495e-14,
        			           3.1096524942448520364797770647969e-16,
        			           2.2533721785099826759429898267209e-18,
        	 };
        	 
        	 
        	                      assert (pow == csc);
        	                      assert (coeff == csc);
        	                      assert (coeff == pow);
        	                      
        	                      if (value >= Double.MAX_VALUE || value <=Double.MIN_NORMAL){
        	                    	  return 0;
        	                    	  
        	                      }else if(value == 0)  {
        	                    	  throw new ArithmeticException("division by 0");
        	                    	  
        	                      }else if(value < 0 ){
        	                    	 throw new IllegalArgumentException("argument must be between 0 and Pi");
        	                    	  
        	                      }else if(value >Math.PI){
        	                    	  throw new IllegalArgumentException("argument must be between 0 and Pi");
        	                    	  
        	                      } else{
        	                    	  
        	                    	  for(i = 0;i<pow.length;i++){
        	                    		  
        	                    		  if(exp<=-1){
        	                    			  exp=1;
        	                    			  ++exp;
        	                    			  factor = (2*exp)+1;
        	                    			  
        	                    		  }else{
        	                    		  
        	                    		  
        	                    		  exp = ++l;
        	                    		  factor = (2*exp)+1;
        	                    		  
        	                    		  pow[i] = Math.pow(x, factor);
        	                    		  
        	                    		  System.out.println(Arrays.toString(pow));//for testing only
        	                    	  }
        	                    	  }
        	                    	  for (j = 0;j<csc.length;j++){
        	                    		  
        	                    		  csc[j] = (pow[j] * coeff[j]);
        	                    		  System.out.println(Arrays.toString(csc));//used for testing only
        	                    	  }
        	                    	  
        	                    	  for(k = 0;k<csc.length;k++){
        	                    		  
        	                    		  sum = sum + csc[k];
        	                    		  System.out.println(Arrays.toString(csc));
        	                    	  }
        	                    	  
        	                    	  cosecant = term+sum;
        	                    	  System.out.printf("%20s \n",  "cosecant("+x+")");
        	                    	  System.out.printf("%.16f \n" ,  cosecant);
        	                      }
        	                      
        		 return cosecant;

  3. #23
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Method for calculation Cauchy Inequality with fully implemented handler

    Note my argument above is based on the idea that limitSin was a method specifically designed to handle values between a certain range.
    Now, if the above cosecant has no such limitation on it then AndreB's use of NaN would be more the standard.

  4. #24
    iliyapolak is offline Member
    Join Date
    Jan 2012
    Posts
    14
    Rep Power
    0

    Default Re: Method for calculation Cauchy Inequality with fully implemented handler

    Yes cosecant has similar range constraints , although negative range could be implemented because it is defined mathematically I did not do this and only implemented positive range (0 to Pi).NaN can be returned when division by 0 is encountered(here i think run-time will throw unchecked exception) or input value is out of range.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Can an Interface have implemented method???
    By pppriyadarshi in forum New To Java
    Replies: 1
    Last Post: 09-10-2011, 07:44 PM
  2. Replies: 3
    Last Post: 04-28-2011, 10:43 PM
  3. Simple calculation method problem.
    By carman12 in forum New To Java
    Replies: 11
    Last Post: 12-29-2010, 02:56 AM
  4. Replies: 14
    Last Post: 09-21-2010, 05:10 PM
  5. A Map implemented with ArrayLists
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •