//This is not really a class,
// just a bunch of class methods and variables
// see also PctInt.java

class FracInt {
    static final int FRACBITS=10;
    static final int ONE=(1<<FRACBITS);
    static final int HALF=(1<<(FRACBITS-1));
    static final int FRACPART=ONE-1;
    static final int INTPART=~FRACPART;
    
    //return a FracInt quantity close to the given fp value.
    public final static int from_float(float f) {
	return (int)(f * ONE + 0.5);
    }

    public final static int div(int a, int b) {
	if (b < 0) {
	    b = -b;
	    a = -a;
	}
	if (b < HALF) return 1; //no div by zero.
	return (a / ((b+HALF) >> FRACBITS));
    }
    public final static int round(int a) {
      if (a > 0)
	return ((a + HALF) & INTPART);
      else 
	return ((a - HALF) & INTPART);
    } 
    public final static int mult(int a, int b) {
	if ((b | a) > 0) 
	    return (a >> (FRACBITS/2)) * (b >> (FRACBITS-FRACBITS/2));
	else 
	    if ((b & a) < 0) {
		return (-a >> (FRACBITS/2)) * (-b >> (FRACBITS-FRACBITS/2));
	    } else {
		if (b < 0) 
		    return - ((a >> (FRACBITS/2)) * (-b >> (FRACBITS-FRACBITS/2)));
		else 
		    return ((-a >> (FRACBITS/2)) * (b >> (FRACBITS-FRACBITS/2)));
	    }
    }
    public final static int to_int(int a) {
	return (round(a) >> FRACBITS);
    }
}

