FileDocCategorySizeDatePackage
PiscesMath.javaAPI DocphoneME MR2 API (J2ME)4623Wed May 02 18:00:36 BST 2007com.sun.pisces

PiscesMath

public class PiscesMath extends Object

Fields Summary
private static final int
SINTAB_LG_ENTRIES
private static final int
SINTAB_ENTRIES
private static int[]
sintab
public static final int
PI
public static final int
TWO_PI
public static final int
PI_OVER_TWO
public static final int
SQRT_TWO
Constructors Summary
private PiscesMath()

Methods Summary
public static intcos(int theta)

        return sin(PI_OVER_TWO - theta);
    
public static doublehypot(double x, double y)

        // new RuntimeException().printStackTrace();
        return Math.sqrt(x*x + y*y);
    
public static inthypot(int x, int y)

        return (int)((lsqrt((long)x*x + (long)y*y) + 128) >> 8);
    
public static longhypot(long x, long y)

        return (lsqrt(x*x + y*y) + 128) >> 8;
    
public static intisqrt(int x)

        int fracbits = 16;

        int root = 0;
        int remHi = 0;
        int remLo = x;
        int count = 15 + fracbits/2;

        do {
            remHi = (remHi << 2) | (remLo >>> 30); // N.B. - unsigned shift R
            remLo <<= 2;
            root <<= 1;
            int testdiv = (root << 1) + 1;
            if (remHi >= testdiv) {
                remHi -= testdiv;
                root++;
            }
        } while (count-- != 0);

        return root;
    
public static longlsqrt(long x)

        int fracbits = 16;

        long root = 0;
        long remHi = 0;
        long remLo = x;
        int count = 31 + fracbits/2;

        do {
            remHi = (remHi << 2) | (remLo >>> 62); // N.B. - unsigned shift R
            remLo <<= 2;
            root <<= 1;
            long testDiv = (root << 1) + 1;
            if (remHi >= testDiv) {
                remHi -= testDiv;
                root++;
            }
        } while (count-- != 0);

        return root;
    
public static intsin(int theta)


     
        sintab = new int[SINTAB_ENTRIES + 1];
        for (int i = 0; i < SINTAB_ENTRIES + 1; i++) {
            double theta = i*(Math.PI/2.0)/SINTAB_ENTRIES;
            sintab[i] = (int)(Math.sin(theta)*65536.0);
        }
    
        int sign = 1;
        if (theta < 0) {
            theta = -theta;
            sign = -1;
        }
        // 0 <= theta
        while (theta >= TWO_PI) {
            theta -= TWO_PI;
        }
        // 0 <= theta < 2*PI
        if (theta >= PI) {
            theta = TWO_PI - theta;
            sign = -sign;
        }
        // 0 <= theta < PI
        if (theta > PI_OVER_TWO) {
            theta = PI - theta;
        }
        // 0 <= theta <= PI/2
        int itheta = (int)((long)theta*SINTAB_ENTRIES/(PI_OVER_TWO));
        return sign*sintab[itheta];