double toDouble()

in commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/Int128.java [205:236]


    double toDouble() {
        long h = hi;
        long l = lo;
        // Special cases
        if (h == 0) {
            return l;
        }
        if (l == 0) {
            return h * 0x1.0p64;
        }
        // Both h and l are non-zero and can be negated to a positive magnitude.
        // Use the same logic as toBigInteger to create magnitude (h, l) and a sign.
        int sign = 1;
        if ((h ^ l) < 0) {
            // Here we rearrange to [2^64 * (hi64-1)] + [2^64 - lo64].
            if (h >= 0) {
                h = h - 1;
            } else {
                // As above with negation
                h = ~h; // -h - 1
                l = -l;
                sign = -1;
            }
        } else if (h < 0) {
            // Invert negative values to create the equivalent positive magnitude.
            h = -h;
            l = -l;
            sign = -1;
        }
        final double x = IntMath.uint128ToDouble(h, l);
        return sign < 0 ? -x : x;
    }