// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class FixedPoint {
   private int scalval;
   private byte ndec;
   private final static int[] scal = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
   public FixedPoint(int val, int ndec) {
      scalval = val * scal[ndec];
      this.ndec = (byte)ndec;
   }
   public FixedPoint(String val, int ndec) {
      this(Integer.parseInt(val), ndec);
   }
   private FixedPoint(int scalval, byte ndec) {
      this.ndec = ndec;
      this.scalval = scalval;
   }
   public FixedPoint add(FixedPoint fp) {
      if(this.ndec != fp.ndec) {
         throw new RuntimeException("Different scale");
      }
      return new FixedPoint(this.scalval + fp.scalval, this.ndec);
   }
   public FixedPoint sub(FixedPoint fp) {
      if(this.ndec != fp.ndec) {
         throw new RuntimeException("Different scale");
      }
      return new FixedPoint(this.scalval - fp.scalval, this.ndec);
   }
   public FixedPoint mul(FixedPoint fp) {
      if(this.ndec != fp.ndec) {
         throw new RuntimeException("Different scale");
      }
      return new FixedPoint(this.scalval * fp.scalval / scal[this.ndec], this.ndec);
   }
   public FixedPoint div(FixedPoint fp) {
      if(this.ndec != fp.ndec) {
         throw new RuntimeException("Different scale");
      }
      return new FixedPoint(this.scalval * scal[this.ndec]/ fp.scalval, this.ndec);
   }
   public String toString() {
      return Integer.toString(scalval / scal[ndec]) + "." + Integer.toString(scalval % scal[ndec]);
   }
   public static void main(String[] args) {
      System.out.println((1.0+2.0)*3.0/4.0);
      FixedPoint one = new FixedPoint(1, 2);
      FixedPoint two = new FixedPoint(2, 2);
      FixedPoint three = new FixedPoint("3", 2);
      FixedPoint four = new FixedPoint("4", 2);
      System.out.println(one.add(two).mul(three).div(four));
   }
}
