Sample

int-5848ab3191b3-I13

Lower every eligible classic and enhanced for loop to a while loop

Matched original source

Lines119

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Scalabrino0.7820–1; higher is more readable
Dorn0.3550–1; higher is more readable
Mi0.60–1; higher is more readable
LLOC41Logical lines; fewer is more readable
DeepSeek V4 Pro16.670–20; mean of three runs
GPT-6.1 Sol170–20; mean of three runs
BTTF0.5790–1; higher is more readable

Views

Source code

package com.google.common.math; import static com.google.common.base.Preconditions.checkArgument; import static java.lang.Double.MAX_EXPONENT; import static java.lang.Double.MIN_EXPONENT; import static java.lang.Double.POSITIVE_INFINITY; import static java.lang.Double.doubleToRawLongBits; import static java.lang.Double.isNaN; import static java.lang.Double.longBitsToDouble; import static java.lang.Math.getExponent; import static java.lang.Math.max; import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.VisibleForTesting; import java.math.BigInteger; /** * Utilities for {@code double} primitives. * * @author Louis Wasserman */ @GwtIncompatible final class DoubleUtils { private DoubleUtils() {} // The mask for the significand, according to the {@link // Double#doubleToRawLongBits(double)} spec. static final long SIGNIFICAND_MASK = 0x000fffffffffffffL; // The mask for the exponent, according to the {@link // Double#doubleToRawLongBits(double)} spec. static final long EXPONENT_MASK = 0x7ff0000000000000L; // The mask for the sign, according to the {@link // Double#doubleToRawLongBits(double)} spec. static final long SIGN_MASK = 0x8000000000000000L; static final int SIGNIFICAND_BITS = 52; static final int EXPONENT_BIAS = 1023; /** The implicit 1 bit that is omitted in significands of normal doubles. */ static final long IMPLICIT_BIT = SIGNIFICAND_MASK + 1; static long getSignificand(double d) { checkArgument(isFinite(d), "not a normal value"); int exponent = getExponent(d); long bits = doubleToRawLongBits(d); bits &= SIGNIFICAND_MASK; return (exponent == MIN_EXPONENT - 1) ? bits << 1 : bits | IMPLICIT_BIT; } static boolean isFinite(double d) { return getExponent(d) <= MAX_EXPONENT; } static boolean isNormal(double d) { return getExponent(d) >= MIN_EXPONENT; } /* * Returns x scaled by a power of 2 such that it is in the range [1, 2). Assumes x is positive, * normal, and finite. */ static double scaleNormalize(double x) { long significand = doubleToRawLongBits(x) & SIGNIFICAND_MASK; return longBitsToDouble(significand | ONE_BITS); } static double bigToDouble(BigInteger x) { // This is an extremely fast implementation of BigInteger.doubleValue(). JDK patch pending. BigInteger absX = x.abs(); int exponent = absX.bitLength() - 1; // exponent == floor(log2(abs(x))) if (exponent < Long.SIZE - 1) { return x.longValue(); } else if (exponent > MAX_EXPONENT) { return x.signum() * POSITIVE_INFINITY; } /* * We need the top SIGNIFICAND_BITS + 1 bits, including the "implicit" one bit. To make rounding * easier, we pick out the top SIGNIFICAND_BITS + 2 bits, so we have one to help us round up or * down. twiceSignifFloor will contain the top SIGNIFICAND_BITS + 2 bits, and signifFloor the * top SIGNIFICAND_BITS + 1. * * It helps to consider the real number signif = absX * 2^(SIGNIFICAND_BITS - exponent). */ int shift = exponent - SIGNIFICAND_BITS - 1; long twiceSignifFloor = absX.shiftRight(shift).longValue(); long signifFloor = twiceSignifFloor >> 1; signifFloor &= SIGNIFICAND_MASK; // remove the implied bit /* * We round up if either the fractional part of signif is strictly greater than 0.5 (which is * true if the 0.5 bit is set and any lower bit is set), or if the fractional part of signif is * >= 0.5 and signifFloor is odd (which is true if both the 0.5 bit and the 1 bit are set). */ boolean increment = (twiceSignifFloor & 1) != 0 && ((signifFloor & 1) != 0 || absX.getLowestSetBit() < shift); long signifRounded = increment ? signifFloor + 1 : signifFloor; long bits = (long) (exponent + EXPONENT_BIAS) << SIGNIFICAND_BITS; bits += signifRounded; /* * If signifRounded == 2^53, we'd need to set all of the significand bits to zero and add 1 to * the exponent. This is exactly the behavior we get from just adding signifRounded to bits * directly. If the exponent is MAX_DOUBLE_EXPONENT, we round up (correctly) to * Double.POSITIVE_INFINITY. */ bits |= x.signum() & SIGN_MASK; return longBitsToDouble(bits); } /** Returns its argument if it is non-negative, zero if it is negative. */ static double ensureNonNegative(double value) { checkArgument(!isNaN(value)); return max(value, 0.0); } @VisibleForTesting static final long ONE_BITS = 0x3ff0000000000000L; }

Loading feature diagnosis…

Source regions

Feature contributions

Feature contributions

FeatureValueContribution
(log-odds)
Halstead volumeHow much symbolic information the code contains. It combines the total number of operators and values with the number of distinct ones.3526.302−116.368
Source linesThe number of non-empty source lines.119+47.6
Byte entropyHow varied the bytes in the source text are. Repeated bytes give a lower value; a more even mixture gives a higher value.5.122−7.683

Intercept 8.87 + feature contributions = -67.581 (log-odds).
Score = sigmoid(-67.581) = 0.

Score0.782

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.119−3.177
Indentation frequencyHow rapidly indentation changes from line to line.87+2.272
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.86−1.553
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.118.881+1.252
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.95.042+0.729
Identifier layout (horizontal)How rapidly the visual pattern of identifier characters changes from left to right. It measures spatial frequency, not the meaning of the names.99.983−0.57
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.058+0.378

Intercept 0.07 + feature contributions = -0.598 (log-odds).
Score = sigmoid(-0.598) = 0.355.

Score0.6
Score41
Run 1

The code is well-structured, with clear separation of constants, helper methods, and a complex algorithm. Nesting is minimal, and each method has a single, focused purpose. Comments effectively explain non-obvious logic, especially in `bigToDouble`. Action granularity is mostly one action per line, though a few lines combine multiple operations (e.g., `bits |= x.signum() & SIGN_MASK;`). Reading flow is natural, with methods ordered logically from simple utilities to the complex conversion method. The only minor issues are the length of `bigToDouble` (which is necessary for its correctness) and the use of bitwise operations that require domain knowledge. Overall, readability is high for mathematically oriented code.

Run 2

The code is well-structured, with clear sections for constants, helper methods, and the main conversion routine. Nesting is minimal, with guard clauses and early returns contributing to a flat, readable flow. Intent is generally clear, especially in methods like isFinite, isNormal, and ensureNonNegative, while well-commented complex logic (bigToDouble, scaleNormalize) explains non-obvious bit manipulation. Code length is concise for its purpose, though bigToDouble is inherently long. Action granularity is mostly one action per line, with only minor multi-action lines (e.g., combined assignment and bitwise operations). Reading flow is logical: constants first, then simple helpers, then the complex core. Some domain-specific constants (e.g., ONE_BITS) could benefit from a brief comment, and a few lines bundle operations that could be split for clarity.

Run 3

The code is well-structured with clear sections for constants, helper methods, and core logic. It uses descriptive naming and reasonable comments explaining non-obvious algorithms. The nesting is mostly flat, with only a few simple conditionals. Intent is generally clear, especially in methods like `isFinite` and `ensureNonNegative`. The `bigToDouble` method is somewhat complex and lengthy but includes good explanatory comments. Most lines perform a single action, though some bit manipulation operations are dense. Reading flow is natural, with helper methods before their use. Minor deductions for the complexity of `bigToDouble` and some dense one-line bit manipulations that could benefit from being split into smaller steps for clarity.

Run 1

Code Structure: Well-organized utility class with focused methods and named constants; placing ONE_BITS with the other constants would improve consistency. Nesting: Minimal nesting, with early returns keeping the main conversion logic flat. Clarity of intent: Descriptive method names and detailed comments explain complex floating-point operations, though abbreviated variables and dense bitwise rounding expressions require specialist knowledge. Code length: Appropriately concise implementation; lengthy comments are mostly justified by the numerical subtleties. Action granularity: Statements generally perform one action per line, with only a few expressions combining several operations. Reading flow: Consistent formatting and sequential calculation stages make the code easy to follow, although the densely grouped imports and occasional imprecise terminology, such as the finite-value check's 'not a normal value' message, slightly reduce clarity.

Run 2

Code Structure: Well-organized utility class with named constants and focused methods, though ONE_BITS is separated from related constants. Nesting: Shallow conditionals and early returns keep control flow simple. Clarity of intent: Descriptive method names and detailed comments explain complex floating-point operations; abbreviated variables and dense rounding expressions require domain knowledge. Code length: Implementation is concise, with lengthy comments largely justified by the mathematical complexity. Action granularity: Most lines perform one operation, although some bitwise and boolean expressions combine several conceptual steps. Reading flow: Methods generally progress naturally from validation through calculation to return. Minor distractions include tightly packed imports, the potentially confusing 'not a normal value' validation message, and comments whose terminology does not always align precisely with the code.

Run 3

Code Structure: Well-organized utility class with named constants and focused methods; ONE_BITS would fit more naturally alongside the other constants. Nesting: Minimal nesting, with early returns keeping the main conversion method relatively flat. Clarity of intent: Descriptive method names and detailed comments explain the IEEE-754 operations and rounding, although abbreviated variables and dense bitwise expressions require specialist knowledge. Code length: Mostly concise; the longer bigToDouble method and its extensive comments are justified by the algorithm's complexity. Action granularity: Generally one operation per line, with only a few compound expressions combining several logical checks. Reading flow: Natural progression from constants to helpers and conversion logic, supported by explanatory comments. Minor formatting inconsistencies and a potentially confusing validation message ('not a normal value' for a finiteness check) slightly reduce readability.