For a requested absolute tolerance , stop when the retained root bracket has width at most , and report its midpoint. The bisection error bound then guarantees in exact arithmetic. Using the ceiling function and the base-two logarithm, the required number of halvings is
The zero allows stopping before any update when the initial root bracket is already sufficiently narrow. For the polynomial on with , . After nine halvings,
Thus the reported midpoint meets the tolerance; its actual error from the square root is approximately . After eight halvings the bound is , so eight do not give this worst-case guarantee.
Cache the endpoint values. Each update of the bisection method needs just one new function evaluation, at the old midpoint. With no early zero of a function, updates therefore cost function evaluations, including the two initial endpoints. Returning the new midpoint requires no additional function evaluation; requesting its value would add one. The nine-update example needs eleven evaluations for the guarantee.
A small function value alone does not guarantee a small position error: rescaling changes those values without changing its zeros of a function. In a practical implementation, use an explicit width test and an iteration limit. With floating-point arithmetic, also stop if the computed midpoint equals either endpoint: the root bracket can no longer shrink, so repeating the update would loop forever. Report this precision limit separately if the requested tolerance remains unmet. Reliable sign evaluations are needed to preserve the root bracket; compare signs directly rather than multiplying endpoint values, which can suffer overflow or underflow in floating-point arithmetic.
For a rounded midpoint still inside a valid root bracket, the geometric error bound is ; it need not equal because of roundoff error. A rigorous computed enclosure also needs conservative arithmetic for that bound and reliable endpoint signs. The exact-arithmetic bisection error bound should not be claimed unchanged after precision prevents further halving.
For practical tolerance conventions, SciPy's bisect combines absolute and relative tolerances through , and provides a maximum iteration count and convergence status. These differ from the purely absolute width test above.[ref]
For the additive infinite cyclic group , the generating set of a group produces the infinite line: each integer is adjacent to and . Adding the moves gives and additional edges skipping one vertex. Each skip still costs one step in the word metric.
For integers , these word metrics are
The absolute value counts unit moves, and the ceiling function counts pairs plus a possible leftover unit. Every -move changes the integer by at most two, while moves of size two followed by at most one move of size one attain this bound. For example, the graph path has five steps for , while has three for . This calculation gives the concrete comparison
Figure 1.
Two Cayley graphs of the integers: unit moves need five steps from zero to five; adding moves of size two needs three
. Details.
The picture shows only a finite window of each infinite Cayley graph. By contrast, alone fails to be a generating set of a group for : it generates the subgroup . Its move graph has two connected components of a graph, the even integers and the odd integers. Within either component the graph distance is ; between different components no finite graph path exists. Adding a shortcut is different from deleting the moves needed for connectivity.
Suppose is a nonnegative integer count of items and each batch holds at most items, where is a positive integer. The smallest sufficient number of batches is
Indeed, exactly when the integer is at least . If , no batches are needed. If is a multiple of , no partly filled batch is needed.
For items with capacity per batch, . Five batches can be full and the sixth holds two items, leaving unused places. This is a different question from the five complete groups counted by .
Figure 1.
Endpoint-aware integer counts and six twenty-place batches needed for 102 items
. Details.
Writing the Euclidean division , , gives the equivalent rule when , and when . This explains the ceiling function's jump: one extra item beyond a complete multiple demands a whole additional batch. For exact integer arithmetic, the identity avoids a fractional intermediate value; an implementation still needs an integer representation large enough to hold .
Ceiling function 2026-10-14
The ceiling function gives the least integer large enough to meet a real lower bound. For a real number ,
Thus , , and . The operation rounds upward in the total order on the real numbers, which can move a negative input towards zero. Its difference from the input obeys . Python's math.ceil implements this least-integer definition. [ref].
The ceiling count for fixed-size batches is a direct application. Suppose each box holds at most items, with positive integer , and there are items. The smallest number of boxes with enough capacity is : a candidate count must satisfy . For , , six boxes are needed, leaving unused places. Exactly 100 items need only five boxes, and zero items need zero boxes. Adding one to the floor function result unconditionally would therefore overcount at exact multiples of .
Figure 1.
The ceiling function retains the endpoint on the left and counts enough whole containers
. Left: open and filled endpoints distinguish the half-open intervals where , including negative inputs. Right: the original capacity example uses 20 items per box; 101 items need six boxes, while an exact multiple of 20 needs no extra box. The right-hand inputs are integer item counts. Details.
For nonnegative integer and positive integer , Euclidean division gives the exact identity
Write , . The count is when the remainder is zero and otherwise, and the right-hand side encodes these two cases. This argument specifies the domain of the identity; arbitrary negative denominators require their own sign conventions.
The ceiling function is a nondecreasing function, constant on each of the half-open intervals , and has a discontinuity of a function at every integer. It is tied to the floor function by . Adding an integer shifts its output by the same amount: . At an integer the floor and ceiling agree; at every other input they differ by one. These statements refer to exact real numbers; an input already perturbed by floating-point arithmetic can land on the other side of a threshold.
Any deterministic comparison sort correct for all orders of distinct sort keys needs at least comparisons on some input. Here is a factorial, a logarithm, and the brackets denote the ceiling function. This is a worst-case requirement, not a requirement on every input.[ref]
For three distinct sort keys, the diagram has one leaf for each possible ordering of the original entries . Some paths need two comparisons, others three; two comparisons cannot distinguish all six orders.
Figure 1.
A comparison decision tree distinguishes the six orders of three distinct sort keys
. Each internal node tests the sort keys of two fixed original entries. A leaf gives the order in which their complete sorting records must be emitted. There are leaves and maximum depth three. The diagram assumes distinct sort keys, so no equality branch is needed. Details.
For the proof outline, represent adaptive comparisons by a binary tree: distinct sort keys give only two outcomes per comparison. A leaf fixes an output permutation; one leaf cannot correctly handle two different strict orders. Hence there must be at least leaves. Maximum depth permits at most leaves, so gives the bound. Even a three-way comparator has only two reachable outcomes on this distinct-key family.
The growth follows without a precise approximation to the factorial. At least of its factors are at least , so for ,
The brackets denote the floor function. This gives a constant times lower bound for large , matching the worst-case comparison growth of merge sort.
Distinct sort keys are essential to the counting argument. Identical sort keys do not create different strict orders. If all sort keys coincide, stable sorting returns the arrival order. This does not remove the worst-case bound for a comparison sort required to handle arbitrary distinct-key inputs.
The floor function and ceiling function turn a real number into directed integer bounds. They answer two different questions: which integer is the largest one that does not exceed , and which is the smallest one that is not less than ?
Both outputs are integers. These are exact mathematical operations; they need not choose the closest integer, and they do not refer to roundoff error in floating-point arithmetic.
For example,
The fractional part is . The negative example matters: discarding the digits after the decimal point would give , which is truncation toward zero, rather than the floor function's value . At an integer, the floor function and ceiling function agree and the fractional part vanishes.
Figure 1.
Floor and ceiling give enclosing integers; fractional part repeats on each unit interval
. Details.
The usefulness is a bridge from continuous quantities to discrete decisions. An integer count in a closed interval uses both bounds. A ceiling count for fixed-size batches counts how many containers or pages are needed; a floor function counts how many complete groups fit. In Euclidean division, the same bounds specify the quotient, while the fractional part records the scaled remainder. A small amount of notation removes many cases that would otherwise have to be stated separately.
The reflection identity
follows by reflecting the ordered integers across zero: the largest one below becomes the smallest one above . For every integer , translation gives and . These formulas turn upper bounds into lower bounds and move examples between neighboring intervals.
For a historical example of the notation in use, Kenneth E. Iverson's 1962 conference paper A Programming Language lists the floor function, ceiling function and residue operation alongside the ordinary arithmetic operations. It states the two defining inequalities above, then extends operations component by component to vectors and matrices. This shows their role as tools for describing algorithms, rather than merely conventions for writing decimal numbers. [ref]
For objects assigned to boxes, let be the number in box . The generalized pigeonhole principle states
The ceiling function rounds up because box counts are integers. An equivalent threshold formulation says: for a positive integer , if , some box contains at least objects. For , putting objects in each box shows why the strict inequality matters. [ref]
Computed examples. For and , . Counts attain this bound. For and , , and the balanced counts sum to and have largest count exactly . The threshold version agrees: . In contrast, seven boxes each containing three hold only objects.
Figure 1.
Two balanced arrangements attain the generalized pigeonhole principle bounds: thirteen objects in twelve boxes force two, and twenty-three in seven force four
. Details.
The bound is sharp for every and . Use Euclidean division to write with integers and . Place objects in each of boxes and objects in each remaining box. The largest count is if , and if , exactly . This balanced arrangement makes the guarantee best possible even though other arrangements can be much more crowded.
Proof outline. If every count were at most , the total would be at most . This contradicts . Taking proves the displayed bound for ; for every count is zero.
Integer arithmetic 2026-10-13
Integer arithmetic computes with integers. Addition, subtraction and multiplication preserve integer values; division can instead return a quotient and remainder through Euclidean division. For example, gives five complete groups and a remainder of two. The ceiling count for fixed-size batches requires six groups to hold all 102 items.
Mathematical integers have no fixed size bound. A computer representation may have one, so an exact arithmetic formula still requires sufficient space for its inputs and intermediate values. The floor function and ceiling function define mathematical bounds independently of a chosen representation.
For real numbers , the integers in the closed interval begin at and end at , if there are any. Their number is therefore
The extra one includes both ends of the resulting integer interval. The maximum with zero handles an empty intersection: contains no integer, even though it has positive length.
For , the ceiling function selects and the floor function selects , giving . The four points are . For , the count is five, not four: the real length and the number of integer points are different quantities.
The endpoint convention matters. For the half-open interval with , the last allowed integer is , so the count becomes . This handles an integer endpoint without including it accidentally. For example, has four integer points.
Fix an integer base . Let have at most digits in positional notation, treating zero as one digit. For , set using the ceiling function, and . Euclidean division gives
where and . The floor function selects the high block; the remainder selects the low block. Both operands must use the same , including when their actual digit lengths differ.
For odd , the low block has digit positions and the high block has . Leading zero padding aligns a shorter operand without changing its integer value. Padding to a power-of-two length is optional: the split itself works for every . If power-of-two padding is used, the padded length is less than twice the original length, so it changes only a constant factor in the asymptotic analysis.
Merge sort 2026-10-13
Merge sort is a divide-and-conquer algorithm that sorts a finite array by splitting it into contiguous halves, sorting each half using recursion, and combining the two sorted arrays with the merge algorithm. An empty or one-entry array is returned immediately. For length , put the first entries on the left and the remaining on the right; the brackets are the floor function and ceiling function. Always choose the left head on equal keys to obtain stable sorting. This is the split, sort, combine structure taught in the MIT lecture notes.[ref]
Take the concrete input array
The tags identify the entries originally at indices and , counting from zero. Both keys are ; the tags are never compared. Reading downward in the figure follows the splits, then the returned sorted arrays. Arrows show dependencies; the level layout groups independent calls rather than giving their chronological execution order.
Figure 1.
Merge sort splits into base cases and returns sorted arrays
. The tagged keys 2a and 2b retain their original order through stable sorting. The merge algorithm makes 4, 6 and 7 head comparisons on the three returning levels, for 17 in total. Details.
The first two halves are and . Their pairs split into one-entry base cases. Applying the merge algorithm gives the following fully worked return levels; each number in the last column counts only comparisons between two available heads.
Returning levelResulting sorted arraysHead comparisons
Pairs, , ,
Halves,
Whole array
In the left-half merge algorithm, the head pairs are , and ; it takes , then appends . In the final merge algorithm, the head pairs are , , , , , and . It takes , then appends . Thus this precise version makes head comparisons and, ignoring identity tags, returns .
The elegance of merge sort is that it creates exactly the structure its next step needs: two smaller sorted arrays make the larger ordering inexpensive. Once the whole array is sorted, equal keys are adjacent and binary search can serve repeated queries. Stable sorting also retains information that the keys alone do not express. If the left-half merge algorithm instead takes the right head on the tie, it returns ; the keys are sorted but stable sorting has failed.
For time complexity, assume each key comparison, entry access and entry copy has constant cost. The merge algorithm processes entries in time. In Big O notation, the resulting recurrence relation is
For , the balanced levels contain entries each and there are returning levels. Each costs , so the worst-case time complexity is . For arbitrary , there are at most returning levels, each processing at most entries, giving the same bound. The worst-case number of head comparisons is also . Here is the base-two logarithm. The MIT notes give this level-by-level analysis for merge sort.[ref]
The conventional array implementation uses a reusable buffer of entries for the merge algorithm, together with pending calls for recursion. Its auxiliary space complexity is therefore , beyond the original array. This bound describes that implementation. A linked list permits a merge algorithm that reconnects existing nodes; its storage accounting differs from copying array entries into a buffer.
For a short algorithm correctness outline, the empty and one-entry base cases are sorted. Assuming recursion correctly sorts both shorter halves, the merge algorithm returns all their entries in order. Equal keys within a half keep their relative order by the same argument applied to the smaller call; across halves, every left entry originally preceded every right entry, so taking left first preserves stable sorting. Both half-lengths decrease for , establishing termination.
Suppose a binary prefix code has integer codeword lengths . Under a source with probability mass function , its average codeword length is exactly cross-entropy, . The Kraft inequality is satisfied because . This exact interpretation requires suitable dyadic rational probabilities; a general is not an integer codeword length. For positive , a prefix code with lengths exists by the Kraft inequality and has average length between and . The ceiling function causes the one-symbol rounding overhead. The cross-entropy term describes mismatch overhead; those are distinct costs.
Natural density Created 2026-10-05 Updated 2026-10-13
For a set of nonnegative integers, its natural density is the limiting proportion of the first nonnegative integers that belong to :
provided this limit exists. Write for the counting function. The numerator is a finite cardinality; the denominator is exactly , including zero. Every existing natural density lies between zero and one.
For the even nonnegative integers , there are elements below and below . Thus using the ceiling function, and . For the square numbers , the exact count for is
The floor function counts the allowed indices , and the extra one counts zero. At , the square numbers below ten are , so the proportion is ; at , it is . The count grows like the square root of , while the denominator grows like .
The prime numbers also have natural density zero. If is their set, the Prime number theorem gives , hence . The Prime number theorem is a substantial result about the count, rather than a consequence of the fact that there are infinitely many prime numbers. [ref]
Why use natural density when cardinality already measures size? The even nonnegative integers, the square numbers, and the prime numbers all have the same countably infinite cardinality, yet their proportions among large initial integer intervals differ. Natural density distinguishes frequent membership from sparse membership. Equivalently, under a uniform distribution on a finite set of possible nonnegative integers, is the probability of landing in . A set without natural density shows why this limiting proportion need not exist.
Figure 1.
Exact proportions below N: even integers, squares, and an alternating-block set
. Every point uses the count in divided by , with zero included among the even integers and square numbers. The right panel marks exact block-boundary counts for a set without natural density. Its low and high proportions approach and along different subsequences. Details.
Polled input-output 2026-10-13
In polled input-output, the CPU explicitly checks a device's status to discover whether work is ready or an operation has completed. Repeatedly checking in a tight loop is busy polling; periodic checks can instead leave time for other work. Interrupt-driven input-output changes how readiness is discovered, while DMA changes how payload data moves. [ref]
Use the model drawn under interrupt-driven input-output: device events occur at times , while polls occur at . Assume each event remains pending until detected and an event simultaneous with a poll is visible to that poll. The detection times are , giving delays . Four of the seven checks find no new event. These are event-detection delays, not completion times for servicing the events.
More generally, for polls at with , an event at time is first detected at
The ceiling function chooses the first poll at or after the event. To verify the bound, that poll is less than one interval beyond ; if is itself a poll time, the delay is zero by the stated visibility convention. This argument assumes instantaneous checks, persistent pending status and an uninterrupted poll schedule. It does not describe missed transient events or a CPU too busy to maintain that schedule.
The attraction of polled input-output is simple control and the possibility of processing several ready items in one visit. Busy polling spends CPU time even when nothing is ready, but short waits or frequent events can make avoiding repeated interrupt entry worthwhile. The right choice depends on actual checking, notification and service costs. See interrupt-driven input-output for a documented combination of both strategies. [ref]
// Destination: computer-science.bigb
For , let contain the first terms, with . The remainder after those terms is
This is the exact absolute error. A convenient bound replaces the denominator by , giving through the triangle inequality. The exact denominator is preferable when the sign or complex phase of is known.
Figure 1.
The omitted fraction and the number of terms needed to capture 99 percent of a geometric sum
. Details.
For and , the omitted fraction of the entire sum is . Capturing at least percent therefore means . Taking logarithms and applying the ceiling function gives the smallest integer :
This gives and terms for and , respectively. The difference is a concrete reason to care about the common ratio, even when all four series converge.
An absolute target has a different scale. For , , and required absolute error at most , the total sum is and
The first acceptable term count is : the remainder is approximately , while at it is approximately . Counting terms and checking the tail avoids an ambiguous instruction to stop when a term merely looks small.
Let the numerical support of a probability distribution be . Its cumulative distribution function counts how many values have been passed:
It is zero below , equals on for , and is one at and above . Every jump is , even when the horizontal gaps differ.
For inverse transform sampling, take an ideal continuous uniform distribution for on . Partition that interval into intervals of width and output
Each interval has probability , proving the desired discrete uniform distribution. Using the floor function, the index is ; for consecutive integer values the output is . With the usual quantile function convention , one instead writes for , using the ceiling function. The endpoint conventions differ only at a finite set of points with probability zero under the ideal continuous uniform distribution.
For , the four intervals , , , and select , respectively. Thus selects , while selects . The upper half of the interval selects values at least four, recovering probability . Equal widths refer to the sampling probability coordinate, not to distances between output values. This is an exact mathematical construction assuming an ideal continuous uniform distribution; a finite computer random-number source needs its own justification of equal output chances.
Truncation toward zero 2026-10-14
Truncation toward zero removes the fractional magnitude of a real number, giving the integer obtained by applying the floor function to its absolute value and restoring the original sign; a magnitude below one gives zero. More precisely,
The result is zero for . For example, and , whereas . Python's math.trunc uses this towards-zero convention, so replacing it by math.floor changes calculations with negative noninteger inputs. [ref].
Figure 1.
Truncation toward zero is symmetric in sign, while the fractional part defined by the floor function is always nonnegative
. Left: exact integer endpoint values distinguish the two sign-dependent truncation rules; a whole neighborhood of zero maps to zero. Right: takes the sign of a noninteger input, while lies in on both sides of zero. Open endpoints show excluded limiting values. Details.
For every real number ,
The discarded difference has the sign of when is noninteger. In contrast, the fractional part is always in . At , these two differences are and . Thus specifying a signed fractional remainder requires specifying which integer-valued operation is used.
Unlike the floor function and ceiling function, truncation toward zero need not commute with adding an integer. At and , , whereas . This matters if negative coordinates are assigned to equal-width bins by converting a quotient to an integer: belongs to the bin under the floor function, but truncation gives zero. The appropriate operation follows from the declared bin intervals.
Truncation toward zero is a nondecreasing function with a discontinuity of a function at each nonzero integer. It has no discontinuity of a function at zero, because it is identically zero on . Its agreement with the floor function on the positive side and the ceiling function on the negative side explains the different closed endpoints in the diagram.