Results 21 to 24 of 24
 01172012, 11:58 AM #21Moderator
 Join Date
 Apr 2009
 Posts
 12,709
 Rep Power
 22
Re: Method for calculation Cauchy Inequality with fully implemented handler
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...
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.
 01172012, 03:40 PM #22Member
 Join Date
 Jan 2012
 Posts
 14
 Rep Power
 0
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.3469650205761316872418595679012e5, 7.1224535923609997684043268331463e7, 1.5661391322766984136380961777787e8, 2.609753939394591951900734969518e10, 3.4120417942992754810472370028262e12, 3.5916434929557749619266471215495e14, 3.1096524942448520364797770647969e16, 2.2533721785099826759429898267209e18, }; 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;
 01172012, 04:18 PM #23Moderator
 Join Date
 Apr 2009
 Posts
 12,709
 Rep Power
 22
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.
 01172012, 08:16 PM #24Member
 Join Date
 Jan 2012
 Posts
 14
 Rep Power
 0
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 runtime will throw unchecked exception) or input value is out of range.
Similar Threads

Can an Interface have implemented method???
By pppriyadarshi in forum New To JavaReplies: 1Last Post: 09102011, 06:44 PM 
Blocking prevents JFrame from being fully drawn.
By Bart in forum AWT / SwingReplies: 3Last Post: 04282011, 09:43 PM 
Simple calculation method problem.
By carman12 in forum New To JavaReplies: 11Last Post: 12292010, 02:56 AM 
Fully qualified file name is given as input. Look to see it exists or not
By renu in forum New To JavaReplies: 14Last Post: 09212010, 04:10 PM 
A Map implemented with ArrayLists
By Java Tip in forum java.langReplies: 0Last Post: 04162008, 10:29 PM
Bookmarks