import java.applet.Applet; import java.awt.event.*; import java.awt.*; public class Interval { double l,r; /**constructors **/ public Interval() { } public Interval(Interval X) { this.l=X.l; this.r=X.r; } public Interval(double x) { this.l=x; this.r=x; } /**x<=y in this case**/ public Interval(double x,double y) { this.l=x; this.r=y; } /**printout**/ public void print() { System.out.println("["+this.l+" , "+this.r+"]"); } /**rounds a double upwards **/ public static double roundUp(double x) { if(x==Double.POSITIVE_INFINITY) return(Double.POSITIVE_INFINITY); if(x==Double.NEGATIVE_INFINITY) return(Double.NEGATIVE_INFINITY); if(x==0) return Double.longBitsToDouble(1); long xx = Double.doubleToLongBits(x); if(x>0) return(Double.longBitsToDouble(xx+1)); return(Double.longBitsToDouble(xx-1)); } /**rounds downward**/ public static double roundDn(double x) { if (x==0) return(-roundUp(0.0)); return(-roundUp(-x)); } /**round out**/ public static Interval fat(double x) { Interval I=new Interval(); I.l=roundDn(x); I.r=roundUp(x); return(I); } /**basic arithmetic routines**/ public static Interval max(Interval I,Interval J) { double t=Math.max(I.r,J.r); t=roundUp(t); return(new Interval(t)); } public static Interval min(Interval I,Interval J) { double t=Math.min(I.l,J.l); t=roundDn(t); return(new Interval(t)); } /**absolute value**/ public static Interval abs(Interval I) { Interval J=new Interval(); if(I.l >=0) { J=new Interval(I.l,I.r); return(J); } if(I.r<=0) { J=new Interval(roundDn(-I.r),roundUp(-I.l)); return(J); } double t=Math.max(Math.abs(I.l),Math.abs(I.r)); t=roundUp(t); J=new Interval(0.0,t); return(J); } public boolean equalsZero() { if(l!=0.0) return(false); if(r!=0.0) return(false); return(true); } public static Interval ZERO() { return(new Interval(0.0)); } /**Checks that the absolute value is less than 2^{27}. This is to avoid overflow errors**/ public void checkRange() { double d=134217728.0; if(r>d) {throw new ProofException("number out of range 1");} if(l<-d) {throw new ProofException("number out of range 2");} } /**Checks that the absolute value lies between 2^{-11} and 2^7. This is to avoid overflow and underflow error.**/ public void checkRangeStrong() { if(r>128.0) {throw new ProofException("number out of range");} if(l<-128.0) {throw new ProofException("number out of range");} if(Math.abs(l)<1.0/2048.0) {throw new ProofException("number out of range");} if(Math.abs(r)<1.0/2048.0) {throw new ProofException("number out of range");} } public static Interval plus(Interval I,Interval J) { I.checkRange(); J.checkRange(); Interval K=new Interval(); K.l=roundDn(I.l+J.l); K.r=roundUp(I.r+J.r); return(K); } public static Interval minus(Interval I,Interval J) { I.checkRange(); J.checkRange(); Interval K=new Interval(); K.l=roundDn(I.l-J.r); K.r=roundDn(I.r-J.l); return(K); } public static Interval times(Interval I,Interval J) throws ProofException { I.checkRange(); J.checkRange(); Interval K=new Interval(); double ll,lr,rl,rr,l,r; ll=I.l*J.l; lr=I.l*J.r; rl=I.r*J.l; rr=I.r*J.r; K.l=ll; if(K.l>lr) K.l=lr; if(K.l>rl) K.l=rl; if(K.l>rr) K.l=rr; K.r=ll; if(K.rlr) K.l=lr; if(K.l>rl) K.l=rl; if(K.l>rr) K.l=rr; K.r=ll; if(K.r128) { throw new ProofException("Interval: sqrt applied to number greater than 128");} J.l=roundDn(Math.sqrt(I.l)); J.r=roundUp(Math.sqrt(I.r)); return(J); } /**look up table for the powers of 2**/ public static Interval powerLookUp(int k) throws ProofException { Interval ONE=new Interval(1.0); Interval d=new Interval(); if(k<-3) {throw new ProofException("BoxInterval powerLookUp: k too low");} if(k>25) {throw new ProofException("BoxInterval powerLookUp: k too hi");} if(k==-3) return(fat(8.0)); if(k==-2) return(fat(4.0)); if(k==-1) return(fat(2.0)); if(k==0) return(fat(1.0/1.0)); if(k==1) return(fat(1.0/2.0)); if(k==2) return(fat(1.0/4.0)); if(k==3) return(fat(1.0/8.0)); if(k==4) return(fat(1.0/16.0)); if(k==5) return(fat(1.0/32.0)); if(k==6) return(fat(1.0/64.0)); if(k==7) return(fat(1.0/128.0)); if(k==8) return(fat(1.0/256.0)); if(k==9) return(fat(1.0/512.0)); if(k==10) return(fat(1.0/1024.0)); if(k==11) return(fat(1.0/2048.0)); if(k==12) return(fat(1.0/4096.0)); if(k==13) return(fat(1.0/8192.0)); if(k==14) return(fat(1.0/16384.0)); if(k==15) return(fat(1.0/32768.0)); if(k==16) return(fat(1.0/65536.0)); if(k==17) return(fat(1.0/131072.0)); if(k==18) return(fat(1.0/262144.0)); if(k==19) return(fat(1.0/524288.0)); if(k==20) return(fat(1.0/1048576.0)); if(k==21) return(fat(1.0/2097152.0)); if(k==22) return(fat(1.0/4194304.0)); if(k==23) return(fat(1.0/8288608.0)); if(k==24) return(fat(1.0/16777216.0)); return(fat(1.0/33554432.0)); } }