算法最佳实践积累

一、计算点到原点的距离,或者三角形斜边长

public static double hypot(double x, double y) {
        if (x < 0.0)
            x = -x;
        else if (x == 0.0)
            return y < 0.0 ? -y : y;
        if (y < 0.0)
            y = -y;
        else if (y == 0.0)
            return x;
        if (x < y) {
            x /= y;
            return y * Math.sqrt(1.0 + x * x);
        } else {
            y /= x;
            return x * Math.sqrt(1.0 + y * y);
        }
    }

 二、返回double值的hashCode

/**
     * Computes a hash code for a double value, using the algorithm from Joshua
     * Bloch's book <i>Effective Java"</i>
     * 
     * @return a hashcode for the double value
     */
    private static int hashCode(double x) {
        long f = Double.doubleToLongBits(x);
        return (int) (f ^ (f >>> 32));
    }

 三、计算多项式的和:f(x)=a0+a1*x+a2*x²+...+an*X^n

霍纳规则:

  sum=0;

  for int i=n to 0

  sum = ai+x*sum;

原文地址:https://www.cnblogs.com/winson/p/3025700.html