Finding the Exponent: Binary Construction in Mercury’s Square Roots and Logarithms

How knowing where to begin—and knowing how to refine—turns continuous mathematics into computational algorithms.

Mathematics gives us familiar relationships:

x2 = a   and   bx = a

But a computer must answer a more concrete question: How can we construct the unknown value from a finite sequence of operations?

Mercury, an arbitrary-precision floating-point arithmetic library, offers an interesting perspective. Its square-root and logarithm algorithms approach their answers through successive binary contributions. Each candidate is tested against the value being sought, and the construction becomes progressively finer.

The algorithms have something important in common. But their differences reveal why some inverse operations are easier to begin than others.

The Advantage of Knowing the First Bit

Consider the square root:

x = √a

If we know the leading binary position of a, we can determine the leading binary position of its positive square root.

For example:

26 ≤ 100 < 27

Since 26 = 64 and 28 = 256, the square root falls between 23 and 24:

23 ≤ √100 < 24

The answer begins at binary position 3.

This gives an algorithm a useful starting point. It can begin with the contribution 23, then examine successively smaller powers of two.

Given a current approximation m, propose:

q = m + 2k

If q2 ≤ a, the contribution fits and can be retained. Otherwise, it is rejected.

The result is constructed one binary place at a time.

Notice what made the beginning convenient: squaring doubles the exponent of a power of two. The magnitude of the input therefore tells us where to begin constructing the root, even though the remaining bits still have to be tested.

But What If the Unknown Is the Exponent?

Now consider:

bx = a

The logarithm asks for the missing exponent:

x = logb(a)

For real logarithms, the target a must be positive and the base b must be positive and different from 1.

Unlike square-root extraction, we cannot generally read the leading position of x directly from the leading position of a. We must establish an appropriate exponent scale relative to the base.

Mercury’s logarithm implementation approaches this constructively. Repeated squaring or square rooting helps locate an initial power contribution. Once that scale is established, the algorithm can begin refining the unknown exponent.

For a positive exponent, think of candidate contributions in descending binary positions:

x ≈ bk2k + bk-12k-1 + bk-22k-2 + …

Here each binary choice bj is either 0 or 1. Not every term is retained. Each candidate contribution is tested according to whether its corresponding power moves toward or beyond the target. For bases below 1, the comparisons reverse direction, but the construction principle remains.

When refinement reaches fractional binary positions, the mathematical relationship becomes particularly elegant.

Why Square Roots Return to the Story

Consider a nonnegative exponent with fractional binary contributions:

x = n + b1/2 + b2/4 + b3/8 + …

where each bi is either zero or one.

Exponentiation gives:

ax = an × (a1/2)b1 × (a1/4)b2 × (a1/8)b3 × …

But:

a1/2 = √a

and:

a1/4 = √(√a)

Each additional square root corresponds to a finer fractional binary contribution.

This is the connection that makes Mercury’s constructive logarithm algorithm so interesting: square roots are not merely related to logarithms mathematically; they can supply the successive power contributions used to recover the exponent.

Mercury’s optimized logarithm implementation also maintains an accumulated power, avoiding the need to recompute the entire exponentiation for each candidate contribution. Its slower counterpart provides a useful contrast by recomputing candidate powers more directly.

Why Binary?

Binary is not required by the continuity of logarithms. A real logarithm is continuous on its positive domain, but continuity alone does not dictate an algorithm.

Binary refinement is attractive because it transforms a search into successive two-way decisions.

Each step resolves another binary position. Each fractional exponent step can be generated through square rooting. And each candidate can be evaluated against the target.

There is a useful sense in which two is the natural minimal integer subdivision factor: it creates the smallest possible nontrivial choice.

It is not, however, a universal optimum for numerical computation. Other algorithms can compute logarithms through different approximations and convergence methods.

Mercury’s value as an educational implementation lies partly in how clearly its binary approach exposes the construction.

Where Continuous Mathematics Meets Finite Precision

Mathematically, successive refinement can continue indefinitely. A computer cannot.

Mercury represents a value using a configurable number of 32-bit mantissa words. Each additional word supplies 32 more bits of stored precision.

Eventually, a proposed correction becomes too small to change the representable result. The continuous mathematical process has encountered the finite resolution of its numerical representation.

This distinction matters. A converged finite-precision calculation is not the same thing as an exact mathematical limit. But a well-designed numerical algorithm can approach that limit systematically, with precision chosen by the caller.

The Larger Lesson

Square roots and logarithms are inverse operations of different relationships, yet both demonstrate a powerful approach to numerical computation:

Find the scale, propose a contribution, test it, and refine.

For square roots, the initial scale follows naturally from the leading binary position of the input. For logarithms, the exponent scale must first be established relative to the base and target.

From there, binary refinement turns the search into a sequence of increasingly fine decisions.

That is one of Mercury’s most interesting educational qualities. The implementation invites us to examine not only the answer, but how the answer is constructed.

The mathematics is continuous. The algorithm proceeds through discrete decisions. Precision determines how far the construction can go.

And the source code lets us follow those decisions all the way down.

Continue Exploring Mercury

Compare the algorithms directly in the Mercury C implementation, follow the canonical Mercury reference, or explore the Mercury learning hub.

When Two Percentages Cancel: Hidden Direction in Same and Heads

A binary stream can be viewed in at least two natural ways. We can count its values—Heads and Tails—or we can count the relation between consecutive values—Same and Different. These views appear to describe different aspects of the stream.

But a simple subtraction reveals that they overlap in a surprisingly precise way:

SamePercentage - HeadsPercentage

This expression does not merely compare sameness with a preference for Heads. One entire class of transitions cancels out, leaving a directional measurement of what happened after Tails.

The Four Transitions

Every counted transition belongs to one of four classes:

HH  Head followed by Head
HT  Head followed by Tail
TH  Tail followed by Head
TT  Tail followed by Tail

Same transitions are:

Same = HH + TT

Transitions ending in Heads are:

Heads = HH + TH

Subtracting the second from the first gives:

Same - Heads
= (HH + TT) - (HH + TH)
= TT - TH

The HH contribution disappears completely.

SamePercentage - HeadsPercentage measures whether a preceding Tail was followed more often by another Tail or by a Head.

Its interpretation is direct:

  • A positive value means TT exceeded TH: Tails showed persistence.
  • A negative value means TH exceeded TT: Tails showed reversal.
  • A value near zero means neither direction dominated.

A subtraction between two broad percentages has secretly isolated one directional half of the transition table.

The Mirror Measurement

The corresponding Heads-side expression is:

SamePercentage - TailsPercentage

Here:

Same - Tails
= (HH + TT) - (HT + TT)
= HH - HT

This time TT cancels. What remains measures whether a preceding Head was followed more often by another Head or by a Tail.

We can provisionally name the pair:

  • Tail Continuation Balance: Same - Heads = TT - TH
  • Head Continuation Balance: Same - Tails = HH - HT

These are raw directional balances. They preserve both the direction of the effect and the amount of the stream that contributed to it.

The Pair Reconstructs the Familiar Geometry

Let:

D_T = Same - Heads
D_H = Same - Tails

Adding them gives:

D_T + D_H
= 2 × SamePercentage - 100
= SamePercentage - DiffPercentage

Their sum is the overall balance between persistence and alternation.

Subtracting them gives:

D_T - D_H
= TailsPercentage - HeadsPercentage

Their difference is the ordinary value imbalance, viewed from the opposite direction.

The two directional measurements therefore form a small coordinate system:

  • Their sum describes Same versus Different.
  • Their difference describes Tails versus Heads.

This is more than a numerical coincidence. It shows that the value-space and relation-space descriptions of a binary stream are algebraically intertwined. The directional transition balances sit between them.

Why ClassicMetaGuess Is a Natural Probe

TruthInTheFlip does not only record Heads and Tails. Its central experiments anticipate the relation between flips.

ClassicMetaGuess observes whether the most recent transition was Same or Different and anticipates the opposite relation next:

Same      → anticipate Different
Different → anticipate Same

Its opposite, AntiMetaGuess, anticipates continuation in the relation stream:

Same      → anticipate Same
Different → anticipate Different

These two strategies are nearly ideal instruments for this subject because neither gives Heads or Tails a privileged role. If every Head in a stream were renamed Tail and every Tail renamed Head, its Same/Different relations would remain unchanged, and both strategies would make the same relational choices.

ClassicMetaGuess probes alternation in the relation stream. AntiMetaGuess probes persistence in that stream. They are complementary views of whether Same tends to follow Different and whether a relation tends to repeat itself.

The new directional metrics ask a different but adjacent question:

How is the observed Same/Different balance distributed across transitions beginning with Heads and transitions beginning with Tails?

The strategy and the metric should therefore not be collapsed into one idea. The anticipation strategy is an active rule that selects a relation for the next flip. The directional balance is a descriptive property reconstructed from the outcomes. One acts; the other measures.

Their relationship is valuable precisely because they remain distinct. A relation-symmetric strategy can be compared with source measurements that reveal whether its apparent behavior was carried equally by both sides of the binary alphabet.

Exploring Quant.tkr with TruthInTheFlip Farm

The archived Quant.tkr record gives us a concrete source for exploring these identities. A rolling window keeps the measurement local while absTotal preserves the position in the full experiment.

The Tail Continuation Balance can be emitted with:

offset#SamePercentage,negate#HeadsPercentage

The Head Continuation Balance is:

offset#SamePercentage,negate#TailsPercentage

A Farm command for both is:

csv tracker window by_total 100B file "Quant.tkr" absTotal SamePercentage HeadsPercentage TailsPercentage offset#SamePercentage,negate#HeadsPercentage offset#SamePercentage,negate#TailsPercentage

The overall Same/Different balance reconstructed from the pair is:

scale#offset50#SamePercentage,2

That produces:

2 × (SamePercentage - 50)
= SamePercentage - DiffPercentage

A broader export can place the reconstructed geometry beside the anticipation record:

csv tracker window by_total 100B file "Quant.tkr" absTotal AnticipatedPercentage ZScore SamePercentage HeadsPercentage offset#SamePercentage,negate#HeadsPercentage offset#SamePercentage,negate#TailsPercentage scale#offset50#SamePercentage,2

This does not claim that a contemporaneous source balance caused the anticipation result. It puts the two trajectories in a common record so their shape, settlement and possible relationships can be examined.

A Standardized Directional Score

The same cancellation can be expressed through the existing Z-scores. Because ZScoreSame and ZScoreHeads use the same sample total and the same 50 percent reference:

ZScoreSame - ZScoreHeads

is proportional to TT - TH. However, simply subtracting two Z-scores does not automatically produce another correctly scaled Z-score. They are derived from the same transitions and are therefore correlated.

Under an independent fair-source model, a naturally scaled Tail-directional score is:

(ZScoreSame - ZScoreHeads) / sqrt(2)

The corresponding Farm expression is:

scale#offset#ZScoreSame,negate#ZScoreHeads,0.7071067811865476

The Heads-side mirror is:

scale#offset#ZScoreSame,negate#ZScoreTails,0.7071067811865476

These scores make directional behavior more comparable across sample sizes. They should still be introduced with care: their interpretation depends on the fair independent null and on the tracker’s counting boundaries.

The Boundary Quirk

The raw identities are exact because SamePercentage, HeadsPercentage and TailsPercentage are calculated over the same counted observations.

A conditional question requires an additional step:

P(T after T) - P(H after T)

The raw Tail Continuation Balance can be written as:

P(previous T) × [P(T after T) - P(H after T)]

Dividing by the proportion of preceding Tails would remove the weighting. However, the tracker directly exposes the distribution of current Heads and Tails, not a separate count of the prior values participating in the recorded transitions.

Those distributions can differ at the two endpoints of a finite interval. At billions of flips the numerical difference is vanishingly small, but it is still a conceptual distinction. The raw balance is exact; a conditional version using TailsPercentage as its denominator is an extremely close finite-window approximation.

Relating the Source to Strategy Selection

TruthInTheFlip also records how often a strategy selected Same and how often that choice succeeded. This permits a second family of comparisons.

The strategy’s Same-selection posture is:

BetSamePercentage - SamePercentage

In Farm form:

offset#BetSamePercentage,negate#SamePercentage

This asks whether the strategy selected Same more often than Same actually occurred. It describes the strategy’s posture, not its accuracy.

A more performance-oriented quantity is Same Selection Lift:

BetSameWinRate - SamePercentage

In Farm form:

offset#BetSameWinRate,negate#SamePercentage

A positive value says that Same occurred more frequently among the conditions where the strategy selected Same than it did across the window as a whole. The Different-side mirror is:

offset#BetDiffWinRate,negate#DiffPercentage

These measurements become especially relevant to strategies such as BetSamePersistence2. That experiment uses an independent inner anticipation record to decide whether the next outer window should favor Same or Different. Selection Lift can help distinguish merely choosing the locally common side from identifying conditions where that choice performed better than the window’s general baseline.

What the Subtraction Is Saying

The original expression now has several compatible readings:

SamePercentage - HeadsPercentage
= TT - TH
= weighted Tail-conditioned persistence
= one directional component of Same versus Different

Its greatest value may be conceptual. Heads/Tails and Same/Different are often treated as separate summaries. Yet when their percentages are placed in the same algebra, they reveal the directional transitions hidden underneath.

ClassicMetaGuess and AntiMetaGuess approach the stream from relation-space without preferring either value. The new balances approach it from the transition table and recover the contribution of each starting value. Together they provide a layered view:

  1. The source produces Heads and Tails.
  2. Consecutive values produce Same and Different.
  3. An anticipation strategy acts on that relation stream.
  4. Directional balances reveal where the observed relational structure was carried.

Nothing new has been inserted into the record. The information was already present. The subtraction simply gives it a name and a place from which it can be seen.

github.com/johnwaynecornell/TruthInTheFlip

The Complex Geometry of Time: From Neolithic Benches to Imaginary Futures

If you map human progress on a standard, linear timeline, it is visually useless. 99.9% of the graph is an empty line of hunter-gatherers, and absolutely everything we consider “modern technology” is crammed into a microscopic sliver on the far right edge.

But, as geeks know, when you have data spanning massively different magnitudes, you switch to a logarithmic scale. If we plot the timeline where the x-axis represents log10(years ago), the picture changes entirely:

  • 10^6 (1,000,000 years ago): Control of fire and early stone tools.
  • 10^5 (100,000 years ago): Emergence of modern human language.
  • 10^4 (10,000 years ago): The Agricultural Revolution and the invention of the bench.
  • 10^3 (1,000 years ago): The printing press and the Renaissance.
  • 10^2 (100 years ago): Harnessing electricity and flight.
  • 10^1 (10 years ago): Cloud computing and the smartphone era.
  • 10^0 (Today): Widespread generative AI and rapid code deployment.

The Discovery: The Future is Orthogonal

While the past is a solid, continuous line of real numbers, looking into the future on this scale requires us to take the logarithm of a negative number (negative “years ago”). This is algebraically impossible on a 1D number line, forcing us into complex numbers.

In Base-10, every future milestone exists exactly 1.36i units above the past on the complex plane. This means:

  1. 100 years in the future = 2 + 1.36i
  2. 1,000 years in the future = 3 + 1.36i

Importantly for the natural logarithm base e the constant is exactly πi

Here is the mathematical proof of this “complex number” theory using Euler’s formula ($e^{i\pi} = -1$):We can rewrite $-100$ as $100 \times -1$.Using logarithm rules, we split it: $\log_{10}(100) + \log_{10}(-1)$.We know $\log_{10}(100)$ is exactly $2$.To find $\log_{10}(-1)$, we use the natural log equivalence derived from Euler’s formula: $\ln(-1) = i\pi$.Using the change-of-base formula, $\log_{10}(-1) = \frac{i\pi}{\ln(10)}$.So, mathematically, 100 years in the future on this logarithmic timeline is exactly:$$2 + \frac{i\pi}{\ln(10)}$$

Mathematically, the future isn’t just “in front” of us; it is a parallel dimension floating 1.36 units away, running alongside the past but never touching it. Through a logarithmic lens, the milestones of human invention form a steady, evenly spaced progression, proving that our rate of innovation isn’t just fast—it is shifting our very geometry.

$$\log_{10}(-t) = \log_{10}(t) + \log_{10}(-1)$$

  • The $\log_{10}(t)$ part gives you the real number (the $2$ for 100 years, the $3$ for 1,000 years, the $4$ for 10,000 years).

  • The $\log_{10}(-1)$ part is the imaginary piece ($\frac{i\pi}{\ln(10)}$), which always evaluates to $1.364376… i$.