Learning pathsPricingA
← DSA problem library

DSA-149 · FOCUSED PRACTICE

Power x to n

Intermediate · Java 21 · Version 1 · editorial draft

Java reference and baseline verified on JDK 21: 2745 deterministic checks. Learner drafts are not executed.

Understand the problem

Compute a finite double base x raised to a signed int exponent n using exponentiation by squaring. Define the result through binary64 multiplication and, for a negative exponent, reciprocal normalization before the positive-power loop.

  • x must be finite; NaN and either input infinity throw IllegalArgumentException, including when n=0. Every int exponent is accepted, including MIN_VALUE and MAX_VALUE. Either signed zero with a negative exponent throws IllegalArgumentException.
  • For valid inputs, n=0 returns +1.0, including ±0.0 to zero. Positive exponents of negative zero preserve the usual parity sign: odd yields −0.0 and even yields +0.0. Negative exponents first form 1.0/x and then compute its positive power.
  • Return a double. Each division/multiplication rounds in binary64; representable overflow may yield signed infinity and underflow may yield a subnormal or signed zero. No finite-result guarantee, exact-real guarantee, or correctly-rounded Math.pow equivalence is promised. The baseline uses linear multiplication only on practical small exponents, and can round differently.
  • Walkthrough fixtures encode negative zero as decimal text because JSON numeric serialization loses its sign. The Java API accepts a double, and trace result/factor text also preserves signed zero, subnormals, and output infinities.

Examples

  1. Input: x=2.0, n=10
    Output: 1024.0

    Exponent ten uses powers corresponding to binary 1010.

  2. Input: x=2.0, n=-2
    Output: 0.25

    Normalize to base one-half and exponent two.

  3. Input: x=-2.0, n=3
    Output: -8.0

    An odd exponent retains the negative sign.

  4. Input: x=2.0, n=-1024
    Output: 5.562684646268003E-309

    Reciprocal-first squaring preserves this subnormal instead of overflowing 2^1024 before inversion.

Before you start

  • Binary decomposition of a nonnegative exponent
  • IEEE 754 double values, rounding, overflow, and signed zero

Recognize the structure

A nonnegative exponent can be decomposed into its binary digits. Squaring advances the base power from x to x² to x⁴ and so on; a set exponent bit selects that factor into the result. Halve the remaining exponent on each pass. This is algebraic divide-and-conquer with a loop invariant, not a linear multiplication loop and not Java’s ^ operator. Unlike memoized DP, its single shrinking dependency chain does not require a table.

Your Java attempt

Java 21 · Draft only. Your code is not compiled, executed, or graded here. Run it locally against the examples and edge cases.

Saving stores text in this browser, visible to other people using this browser profile. It does not sync to your account. Loading is always explicit.

No unsaved text changes · 119/20,000 characters

Self-review

Use the checklist to test your explanation. Checks apply to this visit.

Try before revealing

Hint 1

How can n be written as twice a smaller exponent plus one low bit?

Hint 2

Which factor represents each successive binary bit after repeated squaring?

Hint 3

Normalize a negative exponent in long, consume its low bit, shift, and square only for remaining work.

Derive the solution

Reveal the baseline, optimized reasoning, and Java code

Multiply the normalized factor |n| times

Validate the same domain, widen n to long, invert the base first when n is negative, and multiply a result initialized to one once per exponent unit. It uses O(|n|) multiplication steps and constant workspace. Tests restrict this baseline to small magnitudes; full-width exponents run only through the logarithmic reference. A high-precision BigDecimal oracle starts from the exact binary64 input value, computes small integer powers/reciprocals, and converts once for numerical comparison.

Improve the baseline

Validate the domain before special outcomes

Reject nonfinite x and zero with n<0 before doing arithmetic. n=0 returns one through the empty multiplication loop, but does not excuse an invalid NaN or infinity input. The ±0 checks intentionally use x==0.0, which recognizes both signs.

Widen before negating the exponent

Assign long exponent=n before checking negativity. MIN_VALUE’s magnitude is 2147483648, which is not a positive int but fits long. Negating n as an int first would overflow and could leave the loop negative or empty.

Normalize a negative exponent before squaring

For n<0 set factor=1.0/x and negate the widened exponent. In real arithmetic this transforms x^n into factor^|n|. In floating arithmetic it deliberately chooses reciprocal-first semantics: computing a positive power and inverting afterward can overflow away a representable tiny result.

Consume one binary digit per iteration

Start result=1. If exponent&1 is set, multiply result by factor. Shift exponent right once. If work remains, square factor to prepare the next bit. Saving the conditional final square avoids an irrelevant overflow after all exponent bits have been consumed.

Explain the algebraic invariant and its numeric limits

Ignoring rounding, result×factor^exponent equals the normalized target power. Taking an odd factor and then replacing exponent by floor(exponent/2) while squaring factor preserves this identity. Real Java doubles satisfy the stated sequence of rounded operations, not an exact identity. Repeated squaring can amplify earlier rounding, so logarithmic operation count alone does not imply logarithmic accumulated relative error.

Why it is correct

Normalization and initialization

A widened negative int exponent can be negated safely, including MIN_VALUE. For nonzero x, reciprocal normalization has the right algebraic meaning; its binary64 rounding is part of the contract. Initially result=1 and factor is the normalized base, establishing the ideal-arithmetic invariant.

Parity transition

Write remaining exponent e=2q+b. If b=1, consume one factor into result; otherwise leave result unchanged. Then square the factor and replace e by q. In exact arithmetic, r·f^(2q+b)=(r·f^b)·(f²)^q. When q=0 the factor need not be squared because the loop is done.

Termination and operational correctness

Each positive exponent is unsigned-shifted to a strictly smaller nonnegative long, reaching zero in at most 32 iterations for a normalized int magnitude. The result has consumed every set bit and supplies the ideal power when arithmetic is exact. With doubles, the specified rounded operations determine the output; signed zeros, infinities and subnormals are accepted results rather than hidden errors. No mutable input exists.

Complete Java reference and baseline

Save as Solution.java. Node definitions and imports are included where needed. The baseline is included for comparison; respect its input-size limits.

public final class Solution {
    private Solution() {}
    private static void validate(double x, int n) {
        if (!Double.isFinite(x)) throw new IllegalArgumentException("finite base required");
        if (x == 0.0 && n < 0) throw new IllegalArgumentException("zero to negative exponent");
    }
    public static double myPow(double x, int n) {
        validate(x, n);
        long exponent = n;
        double factor = x;
        if (exponent < 0) {
            factor = 1.0 / factor;
            exponent = -exponent;
        }
        double result = 1.0;
        while (exponent != 0) {
            if ((exponent & 1L) != 0) result *= factor;
            exponent >>>= 1;
            if (exponent != 0) factor *= factor;
        }
        return result;
    }
    // Linear multiplication baseline; only small |n| is practical.
    public static double baseline(double x, int n) {
        validate(x, n);
        long exponent = n;
        double factor = x;
        if (exponent < 0) {
            factor = 1.0 / factor;
            exponent = -exponent;
        }
        double result = 1.0;
        for (long used = 0; used < exponent; used++) result *= factor;
        return result;
    }
}

Read the meaningful blocks

Use a finite-base and zero-negative guard

The guard fixes domain behavior independently of floating division by zero or NaN propagation. Input infinity is rejected even though a finite valid input may later overflow to an output infinity.

Normalize using long and a double reciprocal

The cast precedes negation. Factor inversion precedes the multiplication loop, preventing positive-power overflow from discarding a representable inverse power. It may itself overflow for a tiny nonzero base, which is an allowed output path.

Update result, remaining exponent, then next factor

Read the low bit before shifting it away. Shift the nonnegative widened exponent, then square only if another iteration will use the next power. Result and factor are different roles: only set bits multiply the result.

Complexity

Let m=|n| evaluated in long. The reference uses O(log(m+1)) loop steps and O(1) space, with constant work for m=0. A full-width int magnitude needs at most 32 iterations and 31 useful squarings. Double arithmetic is fixed-width; this is an operation-count bound, not a numerical-error bound.

The baseline performs O(m+1) work with O(1) space and is impractical near the int limits. BigInteger/BigDecimal exact-power variants have growing operands whose bit/digit complexity must be analyzed separately. For floating results, early multiplication rounding may be amplified by subsequent squaring, especially for large exponents near base magnitude one.

Exact-state walkthroughs

Reveal normal and boundary traces

Consume only the set bits of exponent ten

Input: {"x":2,"n":10} · Rounded double / thrown outcome: 1024

Step 1 of 11: Widen the signed exponent and seed the empty product.

The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

Power x to n — consume binary digits of a widened exponent

Phase: initial.

Base x: 2; original signed exponent: 10. Decimal trace text preserves negative zero and output infinity; Java receives a double.

Live rounded numeric state

  • accumulator: 1
  • factor for next/current bit: 2
  • remaining exponent: 10
  • processed bits: 0

Remaining nonnegative exponent — low bit is on the right

  • 00000000bits 31..24
  • 00000000bits 23..16
  • 00000000bits 15..8
  • 00001010bits 7..0

In ideal arithmetic, accumulator × factorremaining exponent equals the normalized target. Actual double operations round, so the invariant explains algebra while the numeric contract defines execution.

At most 32 bits are consumed. Valid finite inputs may yield subnormals, signed zero, or signed infinity. The algorithm does not promise a correctly rounded real power or exact Math.pow equivalence.

State after this step
VariableValue
input{"x":2,"n":10}
phase"initial"
exponent10
factor"2"
accumulator"1"
iteration0
See the affected Java block
 1 public final class Solution {
 2     private Solution() {}
 3     private static void validate(double x, int n) {
 4         if (!Double.isFinite(x)) throw new IllegalArgumentException("finite base required");
 5         if (x == 0.0 && n < 0) throw new IllegalArgumentException("zero to negative exponent");
 6     }
 7     public static double myPow(double x, int n) {
 8         validate(x, n);
 9         long exponent = n;
10         double factor = x;
11         if (exponent < 0) {
12             factor = 1.0 / factor;
13             exponent = -exponent;
14         }
15         double result = 1.0;
16         while (exponent != 0) {
17             if ((exponent & 1L) != 0) result *= factor;
18             exponent >>>= 1;
19             if (exponent != 0) factor *= factor;
20         }
21         return result;
22     }
23     // Linear multiplication baseline; only small |n| is practical.
24     public static double baseline(double x, int n) {
25         validate(x, n);
26         long exponent = n;
27         double factor = x;
28         if (exponent < 0) {
29             factor = 1.0 / factor;
30             exponent = -exponent;
31         }
32         double result = 1.0;
33         for (long used = 0; used < exponent; used++) result *= factor;
34         return result;
35     }
36 }
37 

Highlight locates the relevant statement or block; one trace step may summarize several assignments.

Read the full static trace
  1. Widen the signed exponent and seed the empty product.

    The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

    input
    {"x":2,"n":10}
    phase
    "initial"
    exponent
    10
    factor
    "2"
    accumulator
    "1"
    iteration
    0
  2. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":10}
    phase
    "bit"
    exponent
    10
    factor
    "2"
    accumulator
    "1"
    iteration
    0
    bit
    0
    previousExponent
    10
    previousFactor
    "2"
    before
    "1"
  3. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":10}
    phase
    "advance"
    exponent
    5
    factor
    "4"
    accumulator
    "1"
    iteration
    1
    bit
    0
    previousExponent
    10
    previousFactor
    "2"
    before
    "1"
    squared
    true
  4. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":10}
    phase
    "bit"
    exponent
    5
    factor
    "4"
    accumulator
    "1"
    iteration
    1
    bit
    1
    previousExponent
    5
    previousFactor
    "4"
    before
    "1"
  5. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":10}
    phase
    "advance"
    exponent
    2
    factor
    "16"
    accumulator
    "4"
    iteration
    2
    bit
    1
    previousExponent
    5
    previousFactor
    "4"
    before
    "1"
    squared
    true
  6. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":10}
    phase
    "bit"
    exponent
    2
    factor
    "16"
    accumulator
    "4"
    iteration
    2
    bit
    0
    previousExponent
    2
    previousFactor
    "16"
    before
    "4"
  7. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":10}
    phase
    "advance"
    exponent
    1
    factor
    "256"
    accumulator
    "4"
    iteration
    3
    bit
    0
    previousExponent
    2
    previousFactor
    "16"
    before
    "4"
    squared
    true
  8. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":10}
    phase
    "bit"
    exponent
    1
    factor
    "256"
    accumulator
    "4"
    iteration
    3
    bit
    1
    previousExponent
    1
    previousFactor
    "256"
    before
    "4"
  9. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":10}
    phase
    "advance"
    exponent
    0
    factor
    "256"
    accumulator
    "1024"
    iteration
    4
    bit
    1
    previousExponent
    1
    previousFactor
    "256"
    before
    "4"
    squared
    false
  10. Return the specified rounded binary64 result.

    At most 32 exponent bits are processed. Algebraic correctness does not claim a correctly rounded real power or exact agreement with Math.pow.

    input
    {"x":2,"n":10}
    phase
    "complete"
    exponent
    0
    factor
    "256"
    accumulator
    "1024"
    iteration
    4
  11. Return the documented outcome.

    Input promises, output ownership, numeric domain, and rounding policy remain separate. Visualization histories and bit strings are excluded from reference workspace.

    result
    "1024"

Normalize before exponentiating a negative power

Input: {"x":2,"n":-3} · Rounded double / thrown outcome: 0.125

Step 1 of 8: Widen the signed exponent and seed the empty product.

The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

Power x to n — consume binary digits of a widened exponent

Phase: initial.

Base x: 2; original signed exponent: -3. Decimal trace text preserves negative zero and output infinity; Java receives a double.

Live rounded numeric state

  • accumulator: 1
  • factor for next/current bit: 2
  • remaining exponent: -3
  • processed bits: 0

Negative input has been widened to long; normalization to a nonnegative magnitude is next. Negating the original int would fail for MIN_VALUE.

In ideal arithmetic, accumulator × factorremaining exponent equals the normalized target. Actual double operations round, so the invariant explains algebra while the numeric contract defines execution.

At most 32 bits are consumed. Valid finite inputs may yield subnormals, signed zero, or signed infinity. The algorithm does not promise a correctly rounded real power or exact Math.pow equivalence.

State after this step
VariableValue
input{"x":2,"n":-3}
phase"initial"
exponent-3
factor"2"
accumulator"1"
iteration0
See the affected Java block
 1 public final class Solution {
 2     private Solution() {}
 3     private static void validate(double x, int n) {
 4         if (!Double.isFinite(x)) throw new IllegalArgumentException("finite base required");
 5         if (x == 0.0 && n < 0) throw new IllegalArgumentException("zero to negative exponent");
 6     }
 7     public static double myPow(double x, int n) {
 8         validate(x, n);
 9         long exponent = n;
10         double factor = x;
11         if (exponent < 0) {
12             factor = 1.0 / factor;
13             exponent = -exponent;
14         }
15         double result = 1.0;
16         while (exponent != 0) {
17             if ((exponent & 1L) != 0) result *= factor;
18             exponent >>>= 1;
19             if (exponent != 0) factor *= factor;
20         }
21         return result;
22     }
23     // Linear multiplication baseline; only small |n| is practical.
24     public static double baseline(double x, int n) {
25         validate(x, n);
26         long exponent = n;
27         double factor = x;
28         if (exponent < 0) {
29             factor = 1.0 / factor;
30             exponent = -exponent;
31         }
32         double result = 1.0;
33         for (long used = 0; used < exponent; used++) result *= factor;
34         return result;
35     }
36 }
37 

Highlight locates the relevant statement or block; one trace step may summarize several assignments.

Read the full static trace
  1. Widen the signed exponent and seed the empty product.

    The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

    input
    {"x":2,"n":-3}
    phase
    "initial"
    exponent
    -3
    factor
    "2"
    accumulator
    "1"
    iteration
    0
  2. Invert the base before powering and negate only the widened exponent.

    MIN_VALUE normalizes to positive 2147483648. Reciprocal-first semantics can retain a subnormal result lost by computing an overflowing positive power first.

    input
    {"x":2,"n":-3}
    phase
    "normalize"
    exponent
    3
    factor
    "0.5"
    accumulator
    "1"
    iteration
    0
  3. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-3}
    phase
    "bit"
    exponent
    3
    factor
    "0.5"
    accumulator
    "1"
    iteration
    0
    bit
    1
    previousExponent
    3
    previousFactor
    "0.5"
    before
    "1"
  4. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-3}
    phase
    "advance"
    exponent
    1
    factor
    "0.25"
    accumulator
    "0.5"
    iteration
    1
    bit
    1
    previousExponent
    3
    previousFactor
    "0.5"
    before
    "1"
    squared
    true
  5. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-3}
    phase
    "bit"
    exponent
    1
    factor
    "0.25"
    accumulator
    "0.5"
    iteration
    1
    bit
    1
    previousExponent
    1
    previousFactor
    "0.25"
    before
    "0.5"
  6. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-3}
    phase
    "advance"
    exponent
    0
    factor
    "0.25"
    accumulator
    "0.125"
    iteration
    2
    bit
    1
    previousExponent
    1
    previousFactor
    "0.25"
    before
    "0.5"
    squared
    false
  7. Return the specified rounded binary64 result.

    At most 32 exponent bits are processed. Algebraic correctness does not claim a correctly rounded real power or exact agreement with Math.pow.

    input
    {"x":2,"n":-3}
    phase
    "complete"
    exponent
    0
    factor
    "0.25"
    accumulator
    "0.125"
    iteration
    2
  8. Return the documented outcome.

    Input promises, output ownership, numeric domain, and rounding policy remain separate. Visualization histories and bit strings are excluded from reference workspace.

    result
    "0.125"

The minimum int exponent still terminates

Input: {"x":1,"n":-2147483648} · Rounded double / thrown outcome: 1

Step 1 of 68: Widen the signed exponent and seed the empty product.

The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

Power x to n — consume binary digits of a widened exponent

Phase: initial.

Base x: 1; original signed exponent: -2147483648. Decimal trace text preserves negative zero and output infinity; Java receives a double.

Live rounded numeric state

  • accumulator: 1
  • factor for next/current bit: 1
  • remaining exponent: -2147483648
  • processed bits: 0

Negative input has been widened to long; normalization to a nonnegative magnitude is next. Negating the original int would fail for MIN_VALUE.

In ideal arithmetic, accumulator × factorremaining exponent equals the normalized target. Actual double operations round, so the invariant explains algebra while the numeric contract defines execution.

At most 32 bits are consumed. Valid finite inputs may yield subnormals, signed zero, or signed infinity. The algorithm does not promise a correctly rounded real power or exact Math.pow equivalence.

State after this step
VariableValue
input{"x":1,"n":-2147483648}
phase"initial"
exponent-2147483648
factor"1"
accumulator"1"
iteration0
See the affected Java block
 1 public final class Solution {
 2     private Solution() {}
 3     private static void validate(double x, int n) {
 4         if (!Double.isFinite(x)) throw new IllegalArgumentException("finite base required");
 5         if (x == 0.0 && n < 0) throw new IllegalArgumentException("zero to negative exponent");
 6     }
 7     public static double myPow(double x, int n) {
 8         validate(x, n);
 9         long exponent = n;
10         double factor = x;
11         if (exponent < 0) {
12             factor = 1.0 / factor;
13             exponent = -exponent;
14         }
15         double result = 1.0;
16         while (exponent != 0) {
17             if ((exponent & 1L) != 0) result *= factor;
18             exponent >>>= 1;
19             if (exponent != 0) factor *= factor;
20         }
21         return result;
22     }
23     // Linear multiplication baseline; only small |n| is practical.
24     public static double baseline(double x, int n) {
25         validate(x, n);
26         long exponent = n;
27         double factor = x;
28         if (exponent < 0) {
29             factor = 1.0 / factor;
30             exponent = -exponent;
31         }
32         double result = 1.0;
33         for (long used = 0; used < exponent; used++) result *= factor;
34         return result;
35     }
36 }
37 

Highlight locates the relevant statement or block; one trace step may summarize several assignments.

Read the full static trace
  1. Widen the signed exponent and seed the empty product.

    The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

    input
    {"x":1,"n":-2147483648}
    phase
    "initial"
    exponent
    -2147483648
    factor
    "1"
    accumulator
    "1"
    iteration
    0
  2. Invert the base before powering and negate only the widened exponent.

    MIN_VALUE normalizes to positive 2147483648. Reciprocal-first semantics can retain a subnormal result lost by computing an overflowing positive power first.

    input
    {"x":1,"n":-2147483648}
    phase
    "normalize"
    exponent
    2147483648
    factor
    "1"
    accumulator
    "1"
    iteration
    0
  3. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    2147483648
    factor
    "1"
    accumulator
    "1"
    iteration
    0
    bit
    0
    previousExponent
    2147483648
    previousFactor
    "1"
    before
    "1"
  4. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    1073741824
    factor
    "1"
    accumulator
    "1"
    iteration
    1
    bit
    0
    previousExponent
    2147483648
    previousFactor
    "1"
    before
    "1"
    squared
    true
  5. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    1073741824
    factor
    "1"
    accumulator
    "1"
    iteration
    1
    bit
    0
    previousExponent
    1073741824
    previousFactor
    "1"
    before
    "1"
  6. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    536870912
    factor
    "1"
    accumulator
    "1"
    iteration
    2
    bit
    0
    previousExponent
    1073741824
    previousFactor
    "1"
    before
    "1"
    squared
    true
  7. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    536870912
    factor
    "1"
    accumulator
    "1"
    iteration
    2
    bit
    0
    previousExponent
    536870912
    previousFactor
    "1"
    before
    "1"
  8. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    268435456
    factor
    "1"
    accumulator
    "1"
    iteration
    3
    bit
    0
    previousExponent
    536870912
    previousFactor
    "1"
    before
    "1"
    squared
    true
  9. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    268435456
    factor
    "1"
    accumulator
    "1"
    iteration
    3
    bit
    0
    previousExponent
    268435456
    previousFactor
    "1"
    before
    "1"
  10. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    134217728
    factor
    "1"
    accumulator
    "1"
    iteration
    4
    bit
    0
    previousExponent
    268435456
    previousFactor
    "1"
    before
    "1"
    squared
    true
  11. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    134217728
    factor
    "1"
    accumulator
    "1"
    iteration
    4
    bit
    0
    previousExponent
    134217728
    previousFactor
    "1"
    before
    "1"
  12. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    67108864
    factor
    "1"
    accumulator
    "1"
    iteration
    5
    bit
    0
    previousExponent
    134217728
    previousFactor
    "1"
    before
    "1"
    squared
    true
  13. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    67108864
    factor
    "1"
    accumulator
    "1"
    iteration
    5
    bit
    0
    previousExponent
    67108864
    previousFactor
    "1"
    before
    "1"
  14. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    33554432
    factor
    "1"
    accumulator
    "1"
    iteration
    6
    bit
    0
    previousExponent
    67108864
    previousFactor
    "1"
    before
    "1"
    squared
    true
  15. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    33554432
    factor
    "1"
    accumulator
    "1"
    iteration
    6
    bit
    0
    previousExponent
    33554432
    previousFactor
    "1"
    before
    "1"
  16. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    16777216
    factor
    "1"
    accumulator
    "1"
    iteration
    7
    bit
    0
    previousExponent
    33554432
    previousFactor
    "1"
    before
    "1"
    squared
    true
  17. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    16777216
    factor
    "1"
    accumulator
    "1"
    iteration
    7
    bit
    0
    previousExponent
    16777216
    previousFactor
    "1"
    before
    "1"
  18. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    8388608
    factor
    "1"
    accumulator
    "1"
    iteration
    8
    bit
    0
    previousExponent
    16777216
    previousFactor
    "1"
    before
    "1"
    squared
    true
  19. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    8388608
    factor
    "1"
    accumulator
    "1"
    iteration
    8
    bit
    0
    previousExponent
    8388608
    previousFactor
    "1"
    before
    "1"
  20. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    4194304
    factor
    "1"
    accumulator
    "1"
    iteration
    9
    bit
    0
    previousExponent
    8388608
    previousFactor
    "1"
    before
    "1"
    squared
    true
  21. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    4194304
    factor
    "1"
    accumulator
    "1"
    iteration
    9
    bit
    0
    previousExponent
    4194304
    previousFactor
    "1"
    before
    "1"
  22. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    2097152
    factor
    "1"
    accumulator
    "1"
    iteration
    10
    bit
    0
    previousExponent
    4194304
    previousFactor
    "1"
    before
    "1"
    squared
    true
  23. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    2097152
    factor
    "1"
    accumulator
    "1"
    iteration
    10
    bit
    0
    previousExponent
    2097152
    previousFactor
    "1"
    before
    "1"
  24. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    1048576
    factor
    "1"
    accumulator
    "1"
    iteration
    11
    bit
    0
    previousExponent
    2097152
    previousFactor
    "1"
    before
    "1"
    squared
    true
  25. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    1048576
    factor
    "1"
    accumulator
    "1"
    iteration
    11
    bit
    0
    previousExponent
    1048576
    previousFactor
    "1"
    before
    "1"
  26. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    524288
    factor
    "1"
    accumulator
    "1"
    iteration
    12
    bit
    0
    previousExponent
    1048576
    previousFactor
    "1"
    before
    "1"
    squared
    true
  27. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    524288
    factor
    "1"
    accumulator
    "1"
    iteration
    12
    bit
    0
    previousExponent
    524288
    previousFactor
    "1"
    before
    "1"
  28. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    262144
    factor
    "1"
    accumulator
    "1"
    iteration
    13
    bit
    0
    previousExponent
    524288
    previousFactor
    "1"
    before
    "1"
    squared
    true
  29. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    262144
    factor
    "1"
    accumulator
    "1"
    iteration
    13
    bit
    0
    previousExponent
    262144
    previousFactor
    "1"
    before
    "1"
  30. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    131072
    factor
    "1"
    accumulator
    "1"
    iteration
    14
    bit
    0
    previousExponent
    262144
    previousFactor
    "1"
    before
    "1"
    squared
    true
  31. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    131072
    factor
    "1"
    accumulator
    "1"
    iteration
    14
    bit
    0
    previousExponent
    131072
    previousFactor
    "1"
    before
    "1"
  32. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    65536
    factor
    "1"
    accumulator
    "1"
    iteration
    15
    bit
    0
    previousExponent
    131072
    previousFactor
    "1"
    before
    "1"
    squared
    true
  33. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    65536
    factor
    "1"
    accumulator
    "1"
    iteration
    15
    bit
    0
    previousExponent
    65536
    previousFactor
    "1"
    before
    "1"
  34. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    32768
    factor
    "1"
    accumulator
    "1"
    iteration
    16
    bit
    0
    previousExponent
    65536
    previousFactor
    "1"
    before
    "1"
    squared
    true
  35. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    32768
    factor
    "1"
    accumulator
    "1"
    iteration
    16
    bit
    0
    previousExponent
    32768
    previousFactor
    "1"
    before
    "1"
  36. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    16384
    factor
    "1"
    accumulator
    "1"
    iteration
    17
    bit
    0
    previousExponent
    32768
    previousFactor
    "1"
    before
    "1"
    squared
    true
  37. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    16384
    factor
    "1"
    accumulator
    "1"
    iteration
    17
    bit
    0
    previousExponent
    16384
    previousFactor
    "1"
    before
    "1"
  38. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    8192
    factor
    "1"
    accumulator
    "1"
    iteration
    18
    bit
    0
    previousExponent
    16384
    previousFactor
    "1"
    before
    "1"
    squared
    true
  39. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    8192
    factor
    "1"
    accumulator
    "1"
    iteration
    18
    bit
    0
    previousExponent
    8192
    previousFactor
    "1"
    before
    "1"
  40. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    4096
    factor
    "1"
    accumulator
    "1"
    iteration
    19
    bit
    0
    previousExponent
    8192
    previousFactor
    "1"
    before
    "1"
    squared
    true
  41. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    4096
    factor
    "1"
    accumulator
    "1"
    iteration
    19
    bit
    0
    previousExponent
    4096
    previousFactor
    "1"
    before
    "1"
  42. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    2048
    factor
    "1"
    accumulator
    "1"
    iteration
    20
    bit
    0
    previousExponent
    4096
    previousFactor
    "1"
    before
    "1"
    squared
    true
  43. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    2048
    factor
    "1"
    accumulator
    "1"
    iteration
    20
    bit
    0
    previousExponent
    2048
    previousFactor
    "1"
    before
    "1"
  44. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    1024
    factor
    "1"
    accumulator
    "1"
    iteration
    21
    bit
    0
    previousExponent
    2048
    previousFactor
    "1"
    before
    "1"
    squared
    true
  45. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    1024
    factor
    "1"
    accumulator
    "1"
    iteration
    21
    bit
    0
    previousExponent
    1024
    previousFactor
    "1"
    before
    "1"
  46. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    512
    factor
    "1"
    accumulator
    "1"
    iteration
    22
    bit
    0
    previousExponent
    1024
    previousFactor
    "1"
    before
    "1"
    squared
    true
  47. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    512
    factor
    "1"
    accumulator
    "1"
    iteration
    22
    bit
    0
    previousExponent
    512
    previousFactor
    "1"
    before
    "1"
  48. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    256
    factor
    "1"
    accumulator
    "1"
    iteration
    23
    bit
    0
    previousExponent
    512
    previousFactor
    "1"
    before
    "1"
    squared
    true
  49. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    256
    factor
    "1"
    accumulator
    "1"
    iteration
    23
    bit
    0
    previousExponent
    256
    previousFactor
    "1"
    before
    "1"
  50. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    128
    factor
    "1"
    accumulator
    "1"
    iteration
    24
    bit
    0
    previousExponent
    256
    previousFactor
    "1"
    before
    "1"
    squared
    true
  51. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    128
    factor
    "1"
    accumulator
    "1"
    iteration
    24
    bit
    0
    previousExponent
    128
    previousFactor
    "1"
    before
    "1"
  52. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    64
    factor
    "1"
    accumulator
    "1"
    iteration
    25
    bit
    0
    previousExponent
    128
    previousFactor
    "1"
    before
    "1"
    squared
    true
  53. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    64
    factor
    "1"
    accumulator
    "1"
    iteration
    25
    bit
    0
    previousExponent
    64
    previousFactor
    "1"
    before
    "1"
  54. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    32
    factor
    "1"
    accumulator
    "1"
    iteration
    26
    bit
    0
    previousExponent
    64
    previousFactor
    "1"
    before
    "1"
    squared
    true
  55. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    32
    factor
    "1"
    accumulator
    "1"
    iteration
    26
    bit
    0
    previousExponent
    32
    previousFactor
    "1"
    before
    "1"
  56. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    16
    factor
    "1"
    accumulator
    "1"
    iteration
    27
    bit
    0
    previousExponent
    32
    previousFactor
    "1"
    before
    "1"
    squared
    true
  57. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    16
    factor
    "1"
    accumulator
    "1"
    iteration
    27
    bit
    0
    previousExponent
    16
    previousFactor
    "1"
    before
    "1"
  58. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    8
    factor
    "1"
    accumulator
    "1"
    iteration
    28
    bit
    0
    previousExponent
    16
    previousFactor
    "1"
    before
    "1"
    squared
    true
  59. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    8
    factor
    "1"
    accumulator
    "1"
    iteration
    28
    bit
    0
    previousExponent
    8
    previousFactor
    "1"
    before
    "1"
  60. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    4
    factor
    "1"
    accumulator
    "1"
    iteration
    29
    bit
    0
    previousExponent
    8
    previousFactor
    "1"
    before
    "1"
    squared
    true
  61. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    4
    factor
    "1"
    accumulator
    "1"
    iteration
    29
    bit
    0
    previousExponent
    4
    previousFactor
    "1"
    before
    "1"
  62. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    2
    factor
    "1"
    accumulator
    "1"
    iteration
    30
    bit
    0
    previousExponent
    4
    previousFactor
    "1"
    before
    "1"
    squared
    true
  63. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    2
    factor
    "1"
    accumulator
    "1"
    iteration
    30
    bit
    0
    previousExponent
    2
    previousFactor
    "1"
    before
    "1"
  64. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    1
    factor
    "1"
    accumulator
    "1"
    iteration
    31
    bit
    0
    previousExponent
    2
    previousFactor
    "1"
    before
    "1"
    squared
    true
  65. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":1,"n":-2147483648}
    phase
    "bit"
    exponent
    1
    factor
    "1"
    accumulator
    "1"
    iteration
    31
    bit
    1
    previousExponent
    1
    previousFactor
    "1"
    before
    "1"
  66. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":1,"n":-2147483648}
    phase
    "advance"
    exponent
    0
    factor
    "1"
    accumulator
    "1"
    iteration
    32
    bit
    1
    previousExponent
    1
    previousFactor
    "1"
    before
    "1"
    squared
    false
  67. Return the specified rounded binary64 result.

    At most 32 exponent bits are processed. Algebraic correctness does not claim a correctly rounded real power or exact agreement with Math.pow.

    input
    {"x":1,"n":-2147483648}
    phase
    "complete"
    exponent
    0
    factor
    "1"
    accumulator
    "1"
    iteration
    32
  68. Return the documented outcome.

    Input promises, output ownership, numeric domain, and rounding policy remain separate. Visualization histories and bit strings are excluded from reference workspace.

    result
    "1"

Odd negative-zero power preserves its sign

Input: {"x":"-0.0","n":3} · Rounded double / thrown outcome: -0.0

Step 1 of 7: Widen the signed exponent and seed the empty product.

The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

Power x to n — consume binary digits of a widened exponent

Phase: initial.

Base x: -0.0; original signed exponent: 3. Decimal trace text preserves negative zero and output infinity; Java receives a double.

Live rounded numeric state

  • accumulator: 1
  • factor for next/current bit: -0.0
  • remaining exponent: 3
  • processed bits: 0

Remaining nonnegative exponent — low bit is on the right

  • 00000000bits 31..24
  • 00000000bits 23..16
  • 00000000bits 15..8
  • 00000011bits 7..0

In ideal arithmetic, accumulator × factorremaining exponent equals the normalized target. Actual double operations round, so the invariant explains algebra while the numeric contract defines execution.

At most 32 bits are consumed. Valid finite inputs may yield subnormals, signed zero, or signed infinity. The algorithm does not promise a correctly rounded real power or exact Math.pow equivalence.

State after this step
VariableValue
input{"x":"-0.0","n":3}
phase"initial"
exponent3
factor"-0.0"
accumulator"1"
iteration0
See the affected Java block
 1 public final class Solution {
 2     private Solution() {}
 3     private static void validate(double x, int n) {
 4         if (!Double.isFinite(x)) throw new IllegalArgumentException("finite base required");
 5         if (x == 0.0 && n < 0) throw new IllegalArgumentException("zero to negative exponent");
 6     }
 7     public static double myPow(double x, int n) {
 8         validate(x, n);
 9         long exponent = n;
10         double factor = x;
11         if (exponent < 0) {
12             factor = 1.0 / factor;
13             exponent = -exponent;
14         }
15         double result = 1.0;
16         while (exponent != 0) {
17             if ((exponent & 1L) != 0) result *= factor;
18             exponent >>>= 1;
19             if (exponent != 0) factor *= factor;
20         }
21         return result;
22     }
23     // Linear multiplication baseline; only small |n| is practical.
24     public static double baseline(double x, int n) {
25         validate(x, n);
26         long exponent = n;
27         double factor = x;
28         if (exponent < 0) {
29             factor = 1.0 / factor;
30             exponent = -exponent;
31         }
32         double result = 1.0;
33         for (long used = 0; used < exponent; used++) result *= factor;
34         return result;
35     }
36 }
37 

Highlight locates the relevant statement or block; one trace step may summarize several assignments.

Read the full static trace
  1. Widen the signed exponent and seed the empty product.

    The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

    input
    {"x":"-0.0","n":3}
    phase
    "initial"
    exponent
    3
    factor
    "-0.0"
    accumulator
    "1"
    iteration
    0
  2. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":"-0.0","n":3}
    phase
    "bit"
    exponent
    3
    factor
    "-0.0"
    accumulator
    "1"
    iteration
    0
    bit
    1
    previousExponent
    3
    previousFactor
    "-0.0"
    before
    "1"
  3. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":"-0.0","n":3}
    phase
    "advance"
    exponent
    1
    factor
    "0"
    accumulator
    "-0.0"
    iteration
    1
    bit
    1
    previousExponent
    3
    previousFactor
    "-0.0"
    before
    "1"
    squared
    true
  4. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":"-0.0","n":3}
    phase
    "bit"
    exponent
    1
    factor
    "0"
    accumulator
    "-0.0"
    iteration
    1
    bit
    1
    previousExponent
    1
    previousFactor
    "0"
    before
    "-0.0"
  5. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":"-0.0","n":3}
    phase
    "advance"
    exponent
    0
    factor
    "0"
    accumulator
    "-0.0"
    iteration
    2
    bit
    1
    previousExponent
    1
    previousFactor
    "0"
    before
    "-0.0"
    squared
    false
  6. Return the specified rounded binary64 result.

    At most 32 exponent bits are processed. Algebraic correctness does not claim a correctly rounded real power or exact agreement with Math.pow.

    input
    {"x":"-0.0","n":3}
    phase
    "complete"
    exponent
    0
    factor
    "0"
    accumulator
    "-0.0"
    iteration
    2
  7. Return the documented outcome.

    Input promises, output ownership, numeric domain, and rounding policy remain separate. Visualization histories and bit strings are excluded from reference workspace.

    result
    "-0.0"

Output overflow is allowed

Input: {"x":2,"n":1024} · Rounded double / thrown outcome: Infinity

Step 1 of 25: Widen the signed exponent and seed the empty product.

The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

Power x to n — consume binary digits of a widened exponent

Phase: initial.

Base x: 2; original signed exponent: 1024. Decimal trace text preserves negative zero and output infinity; Java receives a double.

Live rounded numeric state

  • accumulator: 1
  • factor for next/current bit: 2
  • remaining exponent: 1024
  • processed bits: 0

Remaining nonnegative exponent — low bit is on the right

  • 00000000bits 31..24
  • 00000000bits 23..16
  • 00000100bits 15..8
  • 00000000bits 7..0

In ideal arithmetic, accumulator × factorremaining exponent equals the normalized target. Actual double operations round, so the invariant explains algebra while the numeric contract defines execution.

At most 32 bits are consumed. Valid finite inputs may yield subnormals, signed zero, or signed infinity. The algorithm does not promise a correctly rounded real power or exact Math.pow equivalence.

State after this step
VariableValue
input{"x":2,"n":1024}
phase"initial"
exponent1024
factor"2"
accumulator"1"
iteration0
See the affected Java block
 1 public final class Solution {
 2     private Solution() {}
 3     private static void validate(double x, int n) {
 4         if (!Double.isFinite(x)) throw new IllegalArgumentException("finite base required");
 5         if (x == 0.0 && n < 0) throw new IllegalArgumentException("zero to negative exponent");
 6     }
 7     public static double myPow(double x, int n) {
 8         validate(x, n);
 9         long exponent = n;
10         double factor = x;
11         if (exponent < 0) {
12             factor = 1.0 / factor;
13             exponent = -exponent;
14         }
15         double result = 1.0;
16         while (exponent != 0) {
17             if ((exponent & 1L) != 0) result *= factor;
18             exponent >>>= 1;
19             if (exponent != 0) factor *= factor;
20         }
21         return result;
22     }
23     // Linear multiplication baseline; only small |n| is practical.
24     public static double baseline(double x, int n) {
25         validate(x, n);
26         long exponent = n;
27         double factor = x;
28         if (exponent < 0) {
29             factor = 1.0 / factor;
30             exponent = -exponent;
31         }
32         double result = 1.0;
33         for (long used = 0; used < exponent; used++) result *= factor;
34         return result;
35     }
36 }
37 

Highlight locates the relevant statement or block; one trace step may summarize several assignments.

Read the full static trace
  1. Widen the signed exponent and seed the empty product.

    The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

    input
    {"x":2,"n":1024}
    phase
    "initial"
    exponent
    1024
    factor
    "2"
    accumulator
    "1"
    iteration
    0
  2. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":1024}
    phase
    "bit"
    exponent
    1024
    factor
    "2"
    accumulator
    "1"
    iteration
    0
    bit
    0
    previousExponent
    1024
    previousFactor
    "2"
    before
    "1"
  3. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":1024}
    phase
    "advance"
    exponent
    512
    factor
    "4"
    accumulator
    "1"
    iteration
    1
    bit
    0
    previousExponent
    1024
    previousFactor
    "2"
    before
    "1"
    squared
    true
  4. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":1024}
    phase
    "bit"
    exponent
    512
    factor
    "4"
    accumulator
    "1"
    iteration
    1
    bit
    0
    previousExponent
    512
    previousFactor
    "4"
    before
    "1"
  5. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":1024}
    phase
    "advance"
    exponent
    256
    factor
    "16"
    accumulator
    "1"
    iteration
    2
    bit
    0
    previousExponent
    512
    previousFactor
    "4"
    before
    "1"
    squared
    true
  6. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":1024}
    phase
    "bit"
    exponent
    256
    factor
    "16"
    accumulator
    "1"
    iteration
    2
    bit
    0
    previousExponent
    256
    previousFactor
    "16"
    before
    "1"
  7. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":1024}
    phase
    "advance"
    exponent
    128
    factor
    "256"
    accumulator
    "1"
    iteration
    3
    bit
    0
    previousExponent
    256
    previousFactor
    "16"
    before
    "1"
    squared
    true
  8. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":1024}
    phase
    "bit"
    exponent
    128
    factor
    "256"
    accumulator
    "1"
    iteration
    3
    bit
    0
    previousExponent
    128
    previousFactor
    "256"
    before
    "1"
  9. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":1024}
    phase
    "advance"
    exponent
    64
    factor
    "65536"
    accumulator
    "1"
    iteration
    4
    bit
    0
    previousExponent
    128
    previousFactor
    "256"
    before
    "1"
    squared
    true
  10. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":1024}
    phase
    "bit"
    exponent
    64
    factor
    "65536"
    accumulator
    "1"
    iteration
    4
    bit
    0
    previousExponent
    64
    previousFactor
    "65536"
    before
    "1"
  11. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":1024}
    phase
    "advance"
    exponent
    32
    factor
    "4294967296"
    accumulator
    "1"
    iteration
    5
    bit
    0
    previousExponent
    64
    previousFactor
    "65536"
    before
    "1"
    squared
    true
  12. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":1024}
    phase
    "bit"
    exponent
    32
    factor
    "4294967296"
    accumulator
    "1"
    iteration
    5
    bit
    0
    previousExponent
    32
    previousFactor
    "4294967296"
    before
    "1"
  13. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":1024}
    phase
    "advance"
    exponent
    16
    factor
    "18446744073709552000"
    accumulator
    "1"
    iteration
    6
    bit
    0
    previousExponent
    32
    previousFactor
    "4294967296"
    before
    "1"
    squared
    true
  14. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":1024}
    phase
    "bit"
    exponent
    16
    factor
    "18446744073709552000"
    accumulator
    "1"
    iteration
    6
    bit
    0
    previousExponent
    16
    previousFactor
    "18446744073709552000"
    before
    "1"
  15. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":1024}
    phase
    "advance"
    exponent
    8
    factor
    "3.402823669209385e+38"
    accumulator
    "1"
    iteration
    7
    bit
    0
    previousExponent
    16
    previousFactor
    "18446744073709552000"
    before
    "1"
    squared
    true
  16. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":1024}
    phase
    "bit"
    exponent
    8
    factor
    "3.402823669209385e+38"
    accumulator
    "1"
    iteration
    7
    bit
    0
    previousExponent
    8
    previousFactor
    "3.402823669209385e+38"
    before
    "1"
  17. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":1024}
    phase
    "advance"
    exponent
    4
    factor
    "1.157920892373162e+77"
    accumulator
    "1"
    iteration
    8
    bit
    0
    previousExponent
    8
    previousFactor
    "3.402823669209385e+38"
    before
    "1"
    squared
    true
  18. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":1024}
    phase
    "bit"
    exponent
    4
    factor
    "1.157920892373162e+77"
    accumulator
    "1"
    iteration
    8
    bit
    0
    previousExponent
    4
    previousFactor
    "1.157920892373162e+77"
    before
    "1"
  19. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":1024}
    phase
    "advance"
    exponent
    2
    factor
    "1.3407807929942597e+154"
    accumulator
    "1"
    iteration
    9
    bit
    0
    previousExponent
    4
    previousFactor
    "1.157920892373162e+77"
    before
    "1"
    squared
    true
  20. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":1024}
    phase
    "bit"
    exponent
    2
    factor
    "1.3407807929942597e+154"
    accumulator
    "1"
    iteration
    9
    bit
    0
    previousExponent
    2
    previousFactor
    "1.3407807929942597e+154"
    before
    "1"
  21. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":1024}
    phase
    "advance"
    exponent
    1
    factor
    "Infinity"
    accumulator
    "1"
    iteration
    10
    bit
    0
    previousExponent
    2
    previousFactor
    "1.3407807929942597e+154"
    before
    "1"
    squared
    true
  22. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":1024}
    phase
    "bit"
    exponent
    1
    factor
    "Infinity"
    accumulator
    "1"
    iteration
    10
    bit
    1
    previousExponent
    1
    previousFactor
    "Infinity"
    before
    "1"
  23. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":1024}
    phase
    "advance"
    exponent
    0
    factor
    "Infinity"
    accumulator
    "Infinity"
    iteration
    11
    bit
    1
    previousExponent
    1
    previousFactor
    "Infinity"
    before
    "1"
    squared
    false
  24. Return the specified rounded binary64 result.

    At most 32 exponent bits are processed. Algebraic correctness does not claim a correctly rounded real power or exact agreement with Math.pow.

    input
    {"x":2,"n":1024}
    phase
    "complete"
    exponent
    0
    factor
    "Infinity"
    accumulator
    "Infinity"
    iteration
    11
  25. Return the documented outcome.

    Input promises, output ownership, numeric domain, and rounding policy remain separate. Visualization histories and bit strings are excluded from reference workspace.

    result
    "Infinity"

Reciprocal-first subnormal survives

Input: {"x":2,"n":-1024} · Rounded double / thrown outcome: 5.562684646268003e-309

Step 1 of 26: Widen the signed exponent and seed the empty product.

The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

Power x to n — consume binary digits of a widened exponent

Phase: initial.

Base x: 2; original signed exponent: -1024. Decimal trace text preserves negative zero and output infinity; Java receives a double.

Live rounded numeric state

  • accumulator: 1
  • factor for next/current bit: 2
  • remaining exponent: -1024
  • processed bits: 0

Negative input has been widened to long; normalization to a nonnegative magnitude is next. Negating the original int would fail for MIN_VALUE.

In ideal arithmetic, accumulator × factorremaining exponent equals the normalized target. Actual double operations round, so the invariant explains algebra while the numeric contract defines execution.

At most 32 bits are consumed. Valid finite inputs may yield subnormals, signed zero, or signed infinity. The algorithm does not promise a correctly rounded real power or exact Math.pow equivalence.

State after this step
VariableValue
input{"x":2,"n":-1024}
phase"initial"
exponent-1024
factor"2"
accumulator"1"
iteration0
See the affected Java block
 1 public final class Solution {
 2     private Solution() {}
 3     private static void validate(double x, int n) {
 4         if (!Double.isFinite(x)) throw new IllegalArgumentException("finite base required");
 5         if (x == 0.0 && n < 0) throw new IllegalArgumentException("zero to negative exponent");
 6     }
 7     public static double myPow(double x, int n) {
 8         validate(x, n);
 9         long exponent = n;
10         double factor = x;
11         if (exponent < 0) {
12             factor = 1.0 / factor;
13             exponent = -exponent;
14         }
15         double result = 1.0;
16         while (exponent != 0) {
17             if ((exponent & 1L) != 0) result *= factor;
18             exponent >>>= 1;
19             if (exponent != 0) factor *= factor;
20         }
21         return result;
22     }
23     // Linear multiplication baseline; only small |n| is practical.
24     public static double baseline(double x, int n) {
25         validate(x, n);
26         long exponent = n;
27         double factor = x;
28         if (exponent < 0) {
29             factor = 1.0 / factor;
30             exponent = -exponent;
31         }
32         double result = 1.0;
33         for (long used = 0; used < exponent; used++) result *= factor;
34         return result;
35     }
36 }
37 

Highlight locates the relevant statement or block; one trace step may summarize several assignments.

Read the full static trace
  1. Widen the signed exponent and seed the empty product.

    The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

    input
    {"x":2,"n":-1024}
    phase
    "initial"
    exponent
    -1024
    factor
    "2"
    accumulator
    "1"
    iteration
    0
  2. Invert the base before powering and negate only the widened exponent.

    MIN_VALUE normalizes to positive 2147483648. Reciprocal-first semantics can retain a subnormal result lost by computing an overflowing positive power first.

    input
    {"x":2,"n":-1024}
    phase
    "normalize"
    exponent
    1024
    factor
    "0.5"
    accumulator
    "1"
    iteration
    0
  3. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-1024}
    phase
    "bit"
    exponent
    1024
    factor
    "0.5"
    accumulator
    "1"
    iteration
    0
    bit
    0
    previousExponent
    1024
    previousFactor
    "0.5"
    before
    "1"
  4. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-1024}
    phase
    "advance"
    exponent
    512
    factor
    "0.25"
    accumulator
    "1"
    iteration
    1
    bit
    0
    previousExponent
    1024
    previousFactor
    "0.5"
    before
    "1"
    squared
    true
  5. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-1024}
    phase
    "bit"
    exponent
    512
    factor
    "0.25"
    accumulator
    "1"
    iteration
    1
    bit
    0
    previousExponent
    512
    previousFactor
    "0.25"
    before
    "1"
  6. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-1024}
    phase
    "advance"
    exponent
    256
    factor
    "0.0625"
    accumulator
    "1"
    iteration
    2
    bit
    0
    previousExponent
    512
    previousFactor
    "0.25"
    before
    "1"
    squared
    true
  7. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-1024}
    phase
    "bit"
    exponent
    256
    factor
    "0.0625"
    accumulator
    "1"
    iteration
    2
    bit
    0
    previousExponent
    256
    previousFactor
    "0.0625"
    before
    "1"
  8. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-1024}
    phase
    "advance"
    exponent
    128
    factor
    "0.00390625"
    accumulator
    "1"
    iteration
    3
    bit
    0
    previousExponent
    256
    previousFactor
    "0.0625"
    before
    "1"
    squared
    true
  9. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-1024}
    phase
    "bit"
    exponent
    128
    factor
    "0.00390625"
    accumulator
    "1"
    iteration
    3
    bit
    0
    previousExponent
    128
    previousFactor
    "0.00390625"
    before
    "1"
  10. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-1024}
    phase
    "advance"
    exponent
    64
    factor
    "0.0000152587890625"
    accumulator
    "1"
    iteration
    4
    bit
    0
    previousExponent
    128
    previousFactor
    "0.00390625"
    before
    "1"
    squared
    true
  11. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-1024}
    phase
    "bit"
    exponent
    64
    factor
    "0.0000152587890625"
    accumulator
    "1"
    iteration
    4
    bit
    0
    previousExponent
    64
    previousFactor
    "0.0000152587890625"
    before
    "1"
  12. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-1024}
    phase
    "advance"
    exponent
    32
    factor
    "2.3283064365386963e-10"
    accumulator
    "1"
    iteration
    5
    bit
    0
    previousExponent
    64
    previousFactor
    "0.0000152587890625"
    before
    "1"
    squared
    true
  13. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-1024}
    phase
    "bit"
    exponent
    32
    factor
    "2.3283064365386963e-10"
    accumulator
    "1"
    iteration
    5
    bit
    0
    previousExponent
    32
    previousFactor
    "2.3283064365386963e-10"
    before
    "1"
  14. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-1024}
    phase
    "advance"
    exponent
    16
    factor
    "5.421010862427522e-20"
    accumulator
    "1"
    iteration
    6
    bit
    0
    previousExponent
    32
    previousFactor
    "2.3283064365386963e-10"
    before
    "1"
    squared
    true
  15. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-1024}
    phase
    "bit"
    exponent
    16
    factor
    "5.421010862427522e-20"
    accumulator
    "1"
    iteration
    6
    bit
    0
    previousExponent
    16
    previousFactor
    "5.421010862427522e-20"
    before
    "1"
  16. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-1024}
    phase
    "advance"
    exponent
    8
    factor
    "2.938735877055719e-39"
    accumulator
    "1"
    iteration
    7
    bit
    0
    previousExponent
    16
    previousFactor
    "5.421010862427522e-20"
    before
    "1"
    squared
    true
  17. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-1024}
    phase
    "bit"
    exponent
    8
    factor
    "2.938735877055719e-39"
    accumulator
    "1"
    iteration
    7
    bit
    0
    previousExponent
    8
    previousFactor
    "2.938735877055719e-39"
    before
    "1"
  18. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-1024}
    phase
    "advance"
    exponent
    4
    factor
    "8.636168555094445e-78"
    accumulator
    "1"
    iteration
    8
    bit
    0
    previousExponent
    8
    previousFactor
    "2.938735877055719e-39"
    before
    "1"
    squared
    true
  19. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-1024}
    phase
    "bit"
    exponent
    4
    factor
    "8.636168555094445e-78"
    accumulator
    "1"
    iteration
    8
    bit
    0
    previousExponent
    4
    previousFactor
    "8.636168555094445e-78"
    before
    "1"
  20. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-1024}
    phase
    "advance"
    exponent
    2
    factor
    "7.458340731200207e-155"
    accumulator
    "1"
    iteration
    9
    bit
    0
    previousExponent
    4
    previousFactor
    "8.636168555094445e-78"
    before
    "1"
    squared
    true
  21. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-1024}
    phase
    "bit"
    exponent
    2
    factor
    "7.458340731200207e-155"
    accumulator
    "1"
    iteration
    9
    bit
    0
    previousExponent
    2
    previousFactor
    "7.458340731200207e-155"
    before
    "1"
  22. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-1024}
    phase
    "advance"
    exponent
    1
    factor
    "5.562684646268003e-309"
    accumulator
    "1"
    iteration
    10
    bit
    0
    previousExponent
    2
    previousFactor
    "7.458340731200207e-155"
    before
    "1"
    squared
    true
  23. Inspect the low bit before changing the remaining exponent.

    A set bit consumes this factor; a zero bit skips it. In ideal arithmetic accumulator×factor^exponent stays equal to the normalized target. Double operations round.

    input
    {"x":2,"n":-1024}
    phase
    "bit"
    exponent
    1
    factor
    "5.562684646268003e-309"
    accumulator
    "1"
    iteration
    10
    bit
    1
    previousExponent
    1
    previousFactor
    "5.562684646268003e-309"
    before
    "1"
  24. Consume the bit, halve the remaining exponent, and prepare a useful next factor.

    The new exponent is smaller and nonnegative. No square is computed after the last bit. Exact decimal text preserves zero signs, subnormal values, and infinities instead of turning them into JSON null.

    input
    {"x":2,"n":-1024}
    phase
    "advance"
    exponent
    0
    factor
    "5.562684646268003e-309"
    accumulator
    "5.562684646268003e-309"
    iteration
    11
    bit
    1
    previousExponent
    1
    previousFactor
    "5.562684646268003e-309"
    before
    "1"
    squared
    false
  25. Return the specified rounded binary64 result.

    At most 32 exponent bits are processed. Algebraic correctness does not claim a correctly rounded real power or exact agreement with Math.pow.

    input
    {"x":2,"n":-1024}
    phase
    "complete"
    exponent
    0
    factor
    "5.562684646268003e-309"
    accumulator
    "5.562684646268003e-309"
    iteration
    11
  26. Return the documented outcome.

    Input promises, output ownership, numeric domain, and rounding policy remain separate. Visualization histories and bit strings are excluded from reference workspace.

    result
    "5.562684646268003e-309"

Zero exponent uses the empty product

Input: {"x":0,"n":0} · Rounded double / thrown outcome: 1

Step 1 of 3: Widen the signed exponent and seed the empty product.

The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

Power x to n — consume binary digits of a widened exponent

Phase: initial.

Base x: 0; original signed exponent: 0. Decimal trace text preserves negative zero and output infinity; Java receives a double.

Live rounded numeric state

  • accumulator: 1
  • factor for next/current bit: 0
  • remaining exponent: 0
  • processed bits: 0

Remaining nonnegative exponent — low bit is on the right

  • 00000000bits 31..24
  • 00000000bits 23..16
  • 00000000bits 15..8
  • 00000000bits 7..0

In ideal arithmetic, accumulator × factorremaining exponent equals the normalized target. Actual double operations round, so the invariant explains algebra while the numeric contract defines execution.

At most 32 bits are consumed. Valid finite inputs may yield subnormals, signed zero, or signed infinity. The algorithm does not promise a correctly rounded real power or exact Math.pow equivalence.

State after this step
VariableValue
input{"x":0,"n":0}
phase"initial"
exponent0
factor"0"
accumulator"1"
iteration0
See the affected Java block
 1 public final class Solution {
 2     private Solution() {}
 3     private static void validate(double x, int n) {
 4         if (!Double.isFinite(x)) throw new IllegalArgumentException("finite base required");
 5         if (x == 0.0 && n < 0) throw new IllegalArgumentException("zero to negative exponent");
 6     }
 7     public static double myPow(double x, int n) {
 8         validate(x, n);
 9         long exponent = n;
10         double factor = x;
11         if (exponent < 0) {
12             factor = 1.0 / factor;
13             exponent = -exponent;
14         }
15         double result = 1.0;
16         while (exponent != 0) {
17             if ((exponent & 1L) != 0) result *= factor;
18             exponent >>>= 1;
19             if (exponent != 0) factor *= factor;
20         }
21         return result;
22     }
23     // Linear multiplication baseline; only small |n| is practical.
24     public static double baseline(double x, int n) {
25         validate(x, n);
26         long exponent = n;
27         double factor = x;
28         if (exponent < 0) {
29             factor = 1.0 / factor;
30             exponent = -exponent;
31         }
32         double result = 1.0;
33         for (long used = 0; used < exponent; used++) result *= factor;
34         return result;
35     }
36 }
37 

Highlight locates the relevant statement or block; one trace step may summarize several assignments.

Read the full static trace
  1. Widen the signed exponent and seed the empty product.

    The Java variable is long before any negation. Negative-zero fixture text preserves its sign through JSON; the Java API still accepts a double.

    input
    {"x":0,"n":0}
    phase
    "initial"
    exponent
    0
    factor
    "0"
    accumulator
    "1"
    iteration
    0
  2. Return the specified rounded binary64 result.

    At most 32 exponent bits are processed. Algebraic correctness does not claim a correctly rounded real power or exact agreement with Math.pow.

    input
    {"x":0,"n":0}
    phase
    "complete"
    exponent
    0
    factor
    "0"
    accumulator
    "1"
    iteration
    0
  3. Return the documented outcome.

    Input promises, output ownership, numeric domain, and rounding policy remain separate. Visualization histories and bit strings are excluded from reference workspace.

    result
    "1"

Invalid reciprocal is rejected

Input: {"x":0,"n":-1} · Rounded double / thrown outcome: IllegalArgumentException

Step 1 of 2: Reject an input outside the stated numeric domain.

NaN/infinite inputs and either zero sign with a negative exponent are domain errors. Output infinities from a valid finite base are a different case.

Power x to n — consume binary digits of a widened exponent

Phase: reject.

Base x: 0; original signed exponent: -1. Decimal trace text preserves negative zero and output infinity; Java receives a double.

This input violates the finite-base or zero-negative-exponent rule. Throw IllegalArgumentException before the power loop.

State after this step
VariableValue
input{"x":0,"n":-1}
phase"reject"
exponent-1
factor"0"
accumulator"1"
iteration0
See the affected Java block
 1 public final class Solution {
 2     private Solution() {}
 3     private static void validate(double x, int n) {
 4         if (!Double.isFinite(x)) throw new IllegalArgumentException("finite base required");
 5         if (x == 0.0 && n < 0) throw new IllegalArgumentException("zero to negative exponent");
 6     }
 7     public static double myPow(double x, int n) {
 8         validate(x, n);
 9         long exponent = n;
10         double factor = x;
11         if (exponent < 0) {
12             factor = 1.0 / factor;
13             exponent = -exponent;
14         }
15         double result = 1.0;
16         while (exponent != 0) {
17             if ((exponent & 1L) != 0) result *= factor;
18             exponent >>>= 1;
19             if (exponent != 0) factor *= factor;
20         }
21         return result;
22     }
23     // Linear multiplication baseline; only small |n| is practical.
24     public static double baseline(double x, int n) {
25         validate(x, n);
26         long exponent = n;
27         double factor = x;
28         if (exponent < 0) {
29             factor = 1.0 / factor;
30             exponent = -exponent;
31         }
32         double result = 1.0;
33         for (long used = 0; used < exponent; used++) result *= factor;
34         return result;
35     }
36 }
37 

Highlight locates the relevant statement or block; one trace step may summarize several assignments.

Read the full static trace
  1. Reject an input outside the stated numeric domain.

    NaN/infinite inputs and either zero sign with a negative exponent are domain errors. Output infinities from a valid finite base are a different case.

    input
    {"x":0,"n":-1}
    phase
    "reject"
    exponent
    -1
    factor
    "0"
    accumulator
    "1"
    iteration
    0
  2. Return the documented outcome.

    Input promises, output ownership, numeric domain, and rounding policy remain separate. Visualization histories and bit strings are excluded from reference workspace.

    result
    "IllegalArgumentException"

Test the contract

  1. Input: x=0.0, n=0
    Output: 1.0

    Empty product convention.

  2. Input: x=-0.0, n=3
    Output: -0.0

    Odd parity preserves the negative zero sign.

  3. Input: x=1.0, n=-2147483648
    Output: 1.0

    Widened negation handles the full int magnitude.

  4. Input: x=2.0, n=1024
    Output: Infinity

    Overflow is an allowed result.

  5. Input: x=2.0, n=-1075
    Output: 0.0

    The exact tiny positive value rounds to zero.

  6. Input: x=0.0, n=-1
    Output: IllegalArgumentException

    Zero has no reciprocal in the stated domain.

Common mistakes and counterexamples

Negate MIN_VALUE in int

Counterexample: -(-2147483648) wraps back to a negative int.

Widen to long before negating.

Invert only after computing x^|n|

Counterexample: 2^1024 overflows to infinity; its reciprocal becomes zero, losing the nonzero 2^-1024 subnormal.

Normalize to 1/x first under this contract.

Use ^ or claim exact real powers

Counterexample: Java’s ^ is integer XOR; binary64 multiplication rounds.

Use multiply-and-square and state its floating-point contract.

Reject all overflow outputs or silently accept invalid inputs

Counterexample: A finite base 2 with n=1024 legitimately yields infinity, while NaN input is explicitly rejected even at exponent zero.

Separate input-domain validation from permitted output overflow/underflow.

Follow-ups

Return exact integer or modular powers.

Use BigInteger for nonnegative integer powers, or define a modulus and multiplication overflow policy. Negative modular powers require an inverse and coprimality conditions; floating reciprocal normalization is not a modular inverse.

Require correctly rounded Math.pow-compatible output.

A logarithmic multiplication algorithm alone does not supply that accuracy contract. Use an implementation with a specified numerical guarantee and treat comparison tolerances and exceptional values explicitly. This lesson’s tests use relative/absolute tolerances plus exact sign/class checks where appropriate.

Use a recursive implementation.

Return one for zero, compute one half-power recursively, square it once, and multiply by the normalized base when odd. Recomputing the half twice loses logarithmic work. Recursion adds O(log(m+1)) stack depth; widened normalization and floating limits remain relevant.

Related foundations

Keep practicing

AceYourInterviews · 2026-10-18 · Original teaching content and implementations authored for AceYourInterviews. Familiar interview problem names describe standard algorithmic tasks; no company-frequency or platform-difficulty claims are made.