static double log2ScaleExp(double[] xs) {
        if (xs.length == 0) return 0.0;
        double max = xs[0];
        for (int i = 1; i < xs.length; ++i)
            if (max < xs[i]) max = xs[i];
        if (max < 0.0 || max > 1.0) {
            String msg = "Max must be >= 0 and <= 1."
                + " Found max=" + max;
            throw new IllegalArgumentException(msg);
        }
        if (max == 0.0) return 0.0;
        // can't we just do this analytically?
        double exp = 0.0;
        double mult = 1.0;
        while (max < 0.5) {
            exp -= 1.0;
            mult *= 2.0;
            max *= 2.0;
        }
        for (int j = 0; j < xs.length; ++j)
            xs[j] = xs[j] * mult;
        if (exp > 0) {
            String msg = "Exponent must be <= 0."
                + " Found exp=" + exp;
            throw new IllegalArgumentException(msg);
        }
        return exp;
    }