functions of a real returning respectively the largest smaller and the smallest larger integer
{\displaystyle \lfloor x\rfloor =x-\{x\}}
{\displaystyle \lfloor x\rfloor =m}
{\displaystyle \lfloor x\rfloor }
{\displaystyle \lfloor x\rfloor \leq \lceil x\rceil ,}
In mathematics, the floor function is the function that takes as input a real number x, and gives as output the greatest integer less than or equal to x, denoted ⌊xβŒ‹ or … Wikipedia
🌐
Wikipedia
en.wikipedia.org β€Ί wiki β€Ί Floor_and_ceiling_functions
Floor and ceiling functions - Wikipedia
February 5, 2026 - In mathematics, the floor function is the function that takes as input a real number x, and gives as output the greatest integer less than or equal to x, denoted ⌊xβŒ‹ or floor(x). Similarly, the ceiling function maps x to the least integer greater than or equal to x, denoted ⌈xβŒ‰ or ceil(x). ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί dsa β€Ί find-ceil-ab-without-using-ceil-function
Find ceil of a/b without using ceil() function - GeeksforGeeks
September 20, 2022 - // C++ program to find ceil(a/b) // without using ceil() function #include <cmath> #include <iostream> using namespace std; // Driver function int main() { // taking input 1 int a = 4; int b = 3; int val = (a / b) + ((a % b) != 0); cout << "The ...
🌐
Microsoft Support
support.microsoft.com β€Ί en-us β€Ί office β€Ί ceiling-function-0a5cd7c8-0720-4f0a-bd2c-c943e510899f
CEILING function - Microsoft Support
Returns number rounded up, away from zero, to the nearest multiple of significance. For example, if you want to avoid using pennies in your prices and your product is priced at $4.42, use the formula =CEILING(4.42,0.05) to round prices up to the nearest nickel.
🌐
DEV Community
dev.to β€Ί kiani0x01 β€Ί python-ceiling-division-4087
Python Ceiling Division - DEV Community
July 27, 2025 - Mastering ceiling division in Python unlocks cleaner code and fewer edge-case bugs. Whether you choose math.ceil(a / b) for clarity or -(-a // b) for speed, understanding both methods gives you flexibility in any project.
🌐
Mathspp
mathspp.com β€Ί blog β€Ί til β€Ί 001
TIL #001 – ceiling division in Python | mathspp
While there is no direct built-in operator for that, someone replied saying that we can use a couple of minus signs and floor division to do that. Ceiling division with a and b would be equivalent to ceil(a / b).
Find elsewhere
🌐
Enterprise DNA
blog.enterprisedna.co β€Ί python-ceiling-division
Python Ceiling Division: Quick User Guide – Master Data Skills + AI
In programming, it’s common for developers to perform various arithmetic operations. One of these operations includes ceiling division, which divides a number by zero, and the result is rounded to the nearest whole number.
🌐
PKH Me
blog.pkh.me β€Ί p β€Ί 36-figuring-out-round,-floor-and-ceil-with-integer-division.html
Figuring out round, floor and ceil with integer division
During the process I realized I had many wrong assumptions about integer divisions, and also discovered some remarkably useful mathematical properties. This story is about a journey into figuring out equivalent functions to round(a/b), floor(a/b) and ceil(a/b) with a and b integers, while staying in the integer domain (no intermediate float transformation allowed).
🌐
Brilliant
brilliant.org β€Ί wiki β€Ί ceiling-function
Ceiling Function | Brilliant Math & Science Wiki
The ceiling is related to the floor function by the formula \[ \lceil x \rceil = -\lfloor -x \rfloor.
Top answer
1 of 2
4

This isn't intended as a complete and definitive answer, more a list of places I've seen this concept crop up and writers have seen fit to give it a name. Aside from elementary number theory, careful treatment of remainders and their definition is important in pedagogy and in the study of computation β€” certainly in specifying algorithms for use in computers, but also systems of mental arithmetic and traditional hand calculation. These are the areas I've had a look around but feel free to suggest more in the comments.

Number theory

I liked @mathlove's suggestion that the "greatest negative remainder when we divide $168$ by $17$ is $-2$". Sadly Google suggests this isn't in widespread use, but it certainly has been used by professional mathematicians before. Here is Leonard Eugene Dickson in Volume II (Diophantine analysis) of his magisterial History of the Theory of Numbers, quoting from p. 68:

Let $\rho_1$ be the least positive remainder and -$\rho_2$ the greatest negative remainder when s is divided by k

Dickson's "greatest negative remainder" is like Wikipedia's definition of remainder $a=q \times d+r$ but relaxes the requirement $r \ge 0$. Interestingly he puts $r = βˆ’\rho_2$ so $\rho_2 \ge 0$. He clearly regards the positive quantity as more natural, preferring to write the equivalent of $a=q \times d βˆ’ \rho_2$, but never gives $\rho_2$ a name. Since we want the remainder to be zero if we can divide exactly, we would be better to say greatest non-positive remainder.

I thought "least positive remainder" pretty unambiguous until I checked whether anyone had inverted the phrase... Peter Barlow, English mathematician and physicist, writing in the early nineteenth century, uses "least negative remainder" in this proof of the four-square theorem:

If $A$ be any prime number, and all the consecutive squares $1^2, 2^2, 3^2, 4^2, \text{&c} ... \left(\frac{A-1}{2}\right)^2$ be divided by $A$, they will each leave a different positive remainder ... In the same manner it is proved, that the negative remainders are all different from each other ... let $p$ be the least negative remainder, then $p+1$ must be found either amongst the positive or negative remainders...

Barlow's doesn't just mean "least in magnitude" here, he clearly views "negative remainders" as positive quantities that we have to subtract, so $a=q' \times d - r_{neg}$ rather than the usual $a = q \times d + r_{pos}$ for positive remainders. (In context, Barlow is picking the "least negative remainder" from a set of remainders for different divisions, rather than the plausible remainders you could assign to an individual division. He also knew that the remainder in this case could not be zero, but in general it might be, so for our purposes the better phrase would be least non-positive remainder. But the important thing is that he views what he calls "negative remainders" as positive, and the one nearest zero that we'd choose for a specific division is indeed the least non-negative quantity we could subtract from a suitable product to obtain the dividend.) I find it rather confusing to have a "negative remainder" that's defined to be non-negative so I understand why more modern authors don't seem to use the term this way.

I'm not sure how this terminology would extend to the corresponding quotient: a "negative quotient" would be horribly confusing, while "the quotient corresponding to the greatest [or following Barlow, least... perhaps best to say "nearest zero" to be really unambiguous!] negative remainder" is a mouthful.

Pedagogy

There's a huge mathematics education literature on the teaching of division with remainders, and I can't claim to have done any kind of systematic review of it. There are some surprisingly subtle concepts in play (e.g. Cooper and Karsenty, 2018 consider if there's a difference between "no remainder" and "remainder = zero", or whether "3 remainder 2" from $17 \div 5$ is the same "3 remainder 2" as you get from $11 \div 3$) but the whole body of literature I looked at stuck rigorously to the definition of "the remainder" as the least non-negative remainder. It might be more fruitful to look at the pedagogical literature on mental arithmetic strategies, where I suspect more flexibility may be shown.

There's a highly cited paper by Liljedahl, Chernoff, and Zazkis (2007) about designing investigatory tasks for schoolchildren in which "negative remainders" arise as a side-issue. Their task involved placing pentominoes on a 10x10 grid with squares labelled 1-100, and asking if the sum of the numbers covered by the pentomino is divisible by 5.

Finally, some students combined the previous renumbering of the grid with their understanding of remainder as possibly being deficit (i.e. a remainder of 3 is the same as a deficit of 2). ... The groups who came to the solution of renumbering the chart as in figure 3c had no difficulties explaining their observations pertaining to symmetry, balance, and reflective and rotational complements. This is nicely demonstrated in Angela’s comments: We noticed in the remainder zero group that there are always the same number of squares on the left as on the right. We now see that these cancel each other out because the ones on the right have a positive remainder and the ones on the left have a negative remainder.

Liljedahl, Chernoff, and Zazkis clearly distinguish:

  • a negative remainder, which is consistent with the standard definition of remainder in so far as it still refers to a quantity being added on, but violates it in the sense that this quantity is below zero,
  • a deficit, which is a positive quantity to be subtracted.

I find this a very reasonable terminology, but a note of caution: the paper isn't primarily concerned with division, I'm not sure that "deficit" was intended as a term of art, and I can't find much evidence of it being widely used by others. Unfortunately the nature of the task, being focused on divisibility by five and essentially nudging students into the realm of modular arithmetic, obscures the fact that switching from positive to negative "remainder" changes the "quotient" too. So in my example where I use $168=17\times10-2$ instead of the conventional $168=17\times9+15$, there'd be a "negative remainder" of $-2$ and a "deficit" of $2$, but it's unclear what Liljedahl et al. would call the $10$ compared to the conventional quotient of $9$.

Human computation

Fiddling around with remainders is the kind of trick that people who perform competitive mental arithmetic need to gain proficiency in, so I've tried looking about to see if they've developed a nomenclature for the technique. From a brief scan, it seems that saying "$168 \div 17$ makes $10$ remainder $-2$" generally wouldn't be regarded as the final, correct result, but something which requires a quick adjustment to procure $9$ remainder $15$. I've seen phrases like "interim remainder" or intermediate remainder, which make sense, with corresponding interim quotient or intermediate quotient. It's not necessarily specific to "deficits" though, as there are several ways that remainders from estimates can differ from the result of Euclidean division. Consider $189 \div 17$, where our first thought might be to use $17 \times 10 = 170$ to give us an "interim quotient" of $10$ and "interim remainder" of $19$, which we adjust to a quotient of $11$ and remainder of $2$. I find this a genuinely useful terminology, even if it doesn't quite split the cases of positive and negative remainders, and it's nice it covers quotients as well.

Computer science

Negative remainders are used in several programming languages, but usually not when both dividend and divisor are positive. Nevertheless, there is a CS literature on what integer division and mod functions should return over the full range of dividends and divisors, see Boute (1992) and the more easily digested Leijen (2001) β€” hat duly tipped to this excellent Math SE answer which contains the highlights. It transpires that several major programming languages implemented integer division incorrectly, while many others use a definition based on truncation (rounding towards zero) which Boute and Leijen disapprove. They prefer Knuth's suggestion based on flooring (rounding towards $-\infty$, which has some nice properties e.g. the signs of dividend and divisor can be reconstructed if given the signs of quotient and remainder), or better still but very rarely seen, Euclidean division. These three options are compared in some depth, but all give non-negative remainders when dividend and quotient are both positive so neither Boute nor Leijen focus on this case. But they briefly mention a fourth convention, based on rounding to the nearest integer, which can give negative remainders when dividend and divisor are positive. All these conventions fit Boute's generalised definition of quotient $D \text{ div } d$ and remainder $D \text{ mod } d$, for dividend $D$ and divisor $d \neq 0$:

Presently, we require that the functions $\text{div}$ and $\text{mod}$ at least satisfy the following conditions:

  • (a) $D \text{ div } d \in \mathbb Z$ (integer quotient must be integer),
  • (b) $D = d \cdot (D \text{ div } d) + D \text{ mod } d$ (division rule),
  • (c) $|D \text{ mod } d| < |d|$ ($|remainder| < |divisor|$),

in addition to the implicit basic condition that, if $D/d \in \mathbb Z$, then $D \text{ div } d = D/d$.

This gives a more general framework for talking about quotients and remainders, and in principle allows remainders to be negative. It doesn't equip us with language to disambiguate the different answers for quotient or remainder we would get β€” this framework doesn't contrast "deficits" versus "genuine remainders", for example β€” other than to specify which convention we used. Leijen suggests the following names for the common "div-dominant" conventions, i.e. cases where we first define the quotient as a function of $D/d \in \mathbb R$ and the remainder follows from $D \text{ mod } d = D - d \cdot (D \text { div } d)$:

  • T-division: $D \text{ div } d = \operatorname{trunc}(D/d)$ (truncates towards zero),
  • F-division: $D \text{ div } d = \lfloor D/d \rfloor$ (floor, rounds towards $+\infty$),
  • R-division: $D \text{ div } d = \operatorname{round}(D/d)$ (rounds to nearest integer),
  • C-division: $D \text{ div } d = \lceil D/d \rceil$ (ceiling, rounds towards $-\infty$).

Boute uses similar terminology but e.g. "T-definition" rather than "T-division", etc. In contrast, Euclidean division is a "mod-dominant" convention, since we begin by specifying that the remainder is the unique $0 \leq D \text{ mod } d < d$, and the value of the quotient follows from this requirement. Leijen calls this E-division, but mathematicians will view this as just the "standard" definition of integer division.

Neither Boute nor Leijen mention the possibility, but their framework would also permit a "deficit-centric" definition of division, with the mod-dominant requirement $-d < D \text{ mod } d \le 0$ guaranteeing a non-positive remainder. Under this convention, for $168 \div 17$, the quotient is $10$ and the remainder is $-2$, and we would obtain the same answers from C-division and R-division (since $168/17 = 9.88...$ which rounds to $10$). However, the quotient is $9$ and the remainder is $15$ if we use T-division, F-division, or E-division. For the computer scientist, it seems both sets of answers represent bona fide quotients and remainders. Euclidean division, perhaps regrettably as Boute and Leijen would argue, does not enjoy an elevated position that others are judged against. In fact a remainder function was defined as part of the standard IEEE 854-1987, and hence subsequently incorporated into more recent versions of the IEEE Standard for Floating-Point Arithmetic (IEEE 754), and it's not E-division:

When $y \neq 0$, the remainder $r = x\text{ REM }y$ is defined regardless of the rounding mode by the mathematical relation $r = x - y \times n$, where $n$ is the integer nearest the exact value $x/y$; whenever $|n-x/y| = \frac{1}{2}$, then $n$ is even.

This is what Leijen calls R-division, with "bankers' rounding". REM has a more apt name than the traditional mod function: as Cox (2007) complains, mod returns the remainder or residue, and not the modulus (which in this context refers to the divisor). Cody et al. (1985) justify REM on numerical grounds:

REM is defined as it is, instead of matching the "mod" function found in many programming languages, because the latter can always be computed from the former, but the converse is not always true. This is so because REM's remainder is the smallest possible remainder in magnitude, and is always exact. Note in the definition of remainder that the integer $n$ may not be exactly representable in the precision of $x$, $y$, and $r$ because of inadequate precision, exponent range, or both. Nevertheless, $r$ is exactly representable in the precision of $x$ and $y$.

Mathematically, different definitions of remainders are essentially interchangeable, as converting between them just involves adding or subtracting the divisor. But in computing, convertibility can be lost if numerical precision is lost: the virtue of choosing the smallest possible remainder in magnitude is that storing a larger number can result in such loss of precision. Boute believes floating point precision should be a lower priority than mathematical meaningfulness, particularly over integers; he points out that the remainder being exactly representable in the precision of the divisor and dividend is at any rate a property

...inherited by all definitions of mod that satisfy the division rule and whose range is not larger than the range of REM because, as explained in Cody [et al., 1985] such mod functions can always be computed from the REM function without loss in precision. The range of REM is $[-|d|/2, +|d|/2]$, and the range of the Euclidean mod, being $[0, |d|)$, is never larger.

And so we reach a conclusion that might surprise a student of mathematics and irritate Boute: the IEEE-compliant answer to $168 \div 17$ really is a quotient of $10$ and a remainder of $-2$. Contrary to the premise of this Math SE question, it is the "quotient" of $9$ and "remainder" of $15$ that are anomalous, or at least non-compliant. But we might find it useful to distinguish REM returning $-2$ from mod returning $15$. If necessary we can disambiguate further by specifying which division algorithm was used: Boute and Leijen provide a helpful lexicon with E-division, F-division, etc., but in words "Euclidean division" or "divide and floor" will be more universally understood.

I'm not aware that the IEEE standards make a REM/mod type distinction for the quotient but the standard is actually paywalled so I haven't read it in full! There is an international version, jointly published by the International Electrotechnical Commission and International Organization for Standardization as ISO/IEC 60559:2020, identical to the most recent IEEE 754-2019. There is another standard, ISO/IEC 10967, Language independent arithmetic (LIA), whose current version is designed to be compatible with IEEE 754-2008 (the most recent version is now IEEE 754-2019, which among other things fixes an error in how minimum and maximum were defined in IEEE 754-2008). This comes in three parts, LIA-1 to LIA-3, with PDFs freely available for download. Their most recent editions are ISO/IEC 10967-1:2012, Integer and floating point arithmetic; ISO/IEC 10967-2:2001, Elementary numerical functions; and ISO/IEC 10967-3:2006, Complex integer and floating point arithmetic and complex elementary numerical functions. If you thought negative remainders were fun, LIA-3 extends integer division and remainder to the imaginary and complex integers in Section 5.1.2.8, page 22. But I shall stick with LIA-1 and 2, both of which discuss appropriate nomenclature for integer division and its remainder. Much of the technical detail is devoted to identifying problems like overflows or assigning the correct sign to $\pm 0$, and takes place in the set $I$ of integers or $F$ of floating point numbers which the computer's arithmetic system is able to represent. When I cite definitions from LIA-1 and 2, I am generally going to write $\mathbb{Z}$ rather than $I$ and $\mathbb{R}$ rather than $F$, and also ignore the "helper functions" the standards use to e.g. identify overflow issues or find the nearest floating-point number.

The 1994 edition of LIA-1 defines two kinds of integer division: $div_I^t$ is called truncating division (rounds toward zero) and $div_I^f$ is called flooring division (rounds toward $-\infty$), with corresponding "remainder after division" $rem_I^t$ and $rem_I^f$. Either pair satisfies $x = div_I(x,y)\times y + rem_I(x,y)$ and $0 \leq | rem_I(x,y) | < |y|$ for $y \neq 0$. Flooring division is preferred over truncating division because:

Integer division is frequently used for grouping. For example, if a series of indexed items are to be partitioned into groups of $N$ items, it is natura1 to put item $i$ into group $div_I(i, N)$. This works fine if $div_I^f$ is used for $div_I$. However if $div_I^t$ is used, and $i$ can be negative, group $0$ will get $2N - 1$ items rather than the desired $N$. This uneven behavior for negative $i$ can cause subtle program errors, and is a strong reason for preferring the use of $div_I^f$.

In the 2012 update to LIA-1, truncating division was removed entirely due to risk of erroneous use, and flooring division was given new symbols $quot_I(x,y) = \lfloor x/y \rfloor$ for the quotient (replacing $div_I^f$) and $mod_I$ for the remainder (replacing $rem_I^f$). We then have $x = quot_I(x,y) \times y + mod_I(x, y)$ for $y \neq 0$, with $y < mod_I(x,y) \leq 0$ for negative $y$ and $0 \leq mod_I(x,y) < y$ for positive $y$. Note that $mod_I$ has a wider range than REM as defined in IEEE 754 (which causes the precision problem the IEEE were trying to avoid), so calling it mod rather than REM helps brings the ISO/IEC into line with the IEEE's distinction between mod and REM. LIA-2 calls this integer division with floor (the 2012 update to LIA-1 expunged the phrase "flooring division", presumably because it no longer needed to be contrasted with the banished "truncating division"). LIA-1 claims that mathematicians prefer this form of division, but as Boute and Leijen show that isn't really true: they prefer Euclidean division, which happens to agree with division with floor over the positive integers, but not on all of $\mathbb Z$.

LIA-2 also defines integer division with round, with the quotient perhaps confusingly denoted as the ratio, $ratio_I(x,y) = round(x/y)$. The associated remainder $residue_I(x,y) = x - (round(x/y) \times y)$ is called the residue. The subscript $I$ denotes this is only defined for $x, y \in \mathbb{Z}$, but this is the only integer division for which LIA-2 also gives a floating point version of the remainder: $residue_F(x,y) = x - round(x,y)\times y$ for $x, y \in \mathbb R$. Since this is identical to the REM in IEEE 754, LIA-2 calls this the IEEE remainder. Whether over $\mathbb R$ or $\mathbb Z$, we have the desired $x = ratio(x,y) \times y + residue(x, y)$.

The other integer division defined in LIA-2 is division and ceiling, denoted $group_I(x,y) = \lceil x/y \rceil$ with $pad_I = (\lceil x/y \rceil \times y) - x$. Watch the signs! This has been defined so that $x = group_I(x, y) \times y βˆ’ pad_I(x, y)$. Here the ISO/IEC return to the "integer division as grouping" analogy which persuaded them to dump truncated division:

$pad_I$ returns the negative of the remainder after division and ceiling. The reason for this is twofold: 1) for unsigned integer datatypes the remainder is $\le 0$, and would thus often not be representable unless negated, and 2) it is intuitively easier to think of the places left in the last unfilled group of equi-sized and packed groups" as a positive entity, a padding.

The issue of w…

2 of 2
2

As per the OP's suggestion, I will turn my comment into an answer.

The wikipedia says "Given an integer $a$ and a non-zero integer $d$, it can be shown that there exist unique integers $q$ and $r$, such that $a=qd+r$ and $0\le r<|d|$. The number $q$ is called the quotient, while $r$ is called the remainder". "The remainder, as defined above, is called the least positive remainder or simply the remainder". So, how about calling your remainder "the greatest negative remainder"?

🌐
Codeforces
codeforces.com β€Ί blog β€Ί entry β€Ί 78852
Alternative approaches to ceil() - Codeforces
Note that it prints out 1, not 2! This is because a and b are int data types, so that their quotient a/b is in int and the decimal values are discarded (floored). a/b = 1 so ceil(a/b) = ceil(1) = 1.
Top answer
1 of 2
14

I’ll prove the more general result. Let $m$ and $n$ be positive integers and $x$ a real number. Let $k=\left\lceil\dfrac{x}m\right\rceil$. Then $$k-1<\frac{x}m\le k\;,$$ and hence $$\frac{k-1}n<\frac{x}{mn}\le\frac{k}n\;.$$

We want to show that $$\left\lceil{\frac{x}{mn}}\right \rceil=\left\lceil{\frac{\left\lceil{\frac{x}{m}}\right\rceil}{n}}\right\rceil=\left\lceil\frac{k}n\right\rceil\;.$$

Clearly $\left\lceil{\dfrac{x}{mn}}\right\rceil\le\left\lceil\dfrac{k}n\right\rceil$, so suppose to get a contradiction that $\left\lceil{\dfrac{x}{mn}}\right\rceil<\left\lceil\dfrac{k}n\right\rceil$. Then there must be an integer $\ell$ such that

$$\frac{x}{mn}\le\ell<\frac{k}n$$

and hence $\dfrac{x}m\le n\ell<k$. But then $k=\left\lceil\dfrac{x}m\right\rceil\le n\ell<k$, which is absurd.

2 of 2
1

For the case of floor division (ceiling division is analogous), we can get the result directly using the uniqueness of real Euclidean division: $\newcommand{\floor}[1]{\left\lfloor #1 \right\rfloor}$ $\newcommand{\ceil}[1]{\left\lceil #1 \right\rceil}$

For $a$ divided by $b$, the result can be written uniquely as $q,r$ in $a = qb + r$, where quotient $q = \floor{a/b}$ and remainder $r$ satisfies $0 \le r < |b|$.

Let $x$ and $m$ be real numbers ($m$ doesn't have to be integer) and $n$ be a positive integer.

Let $q = \floor{x/m}$, so that $$x = qm + r, \quad 0 \le r < m$$

Let $q' = \floor{\floor{x/m}/n}$, so that $$q = q'n + r', \quad 0 \le r' < n$$

Since $q$ and $n$ are integers, $r'$ is as well, so we have a stronger bound $0 \le r' \le n-1$.

Then $$x = (q'n + r')m + r = q'(nm) + (r'm + r)$$

From our remainder bounds, $0 \le r' m \le (n-1)m $ and $0 \le r < m$, so together $0 \le r'm + r < mn$, that is $r'm + r$ is the remainder of $x / mn$ and $q'$ is the quotient. Conclude $q' = \floor{x/mn}$.

Using floor and ceiling bounds is more flexible, as shown in Concrete Mathematics.

Let $f$ be any continuous, monotonously increasing function with the property that $f(x)$ integer implies $x$ integer. Then $$\floor{f(x)} = \floor{f(\floor x)} \quad \text{and} \quad \ceil{f(x)} = \ceil{f(\ceil x)}$$

Not immediately obvious is that the nested floor and ceiling identities are a special case of function $f(x) = x/n$ evaluated at $x/m$.

🌐
IncludeHelp
includehelp.com β€Ί c β€Ί fast-ceiling-of-an-integer-division.aspx
Fast ceiling of an integer division in C / C++
#include <iostream> using namespace ... q; return 0; } ... For fast ceiling of an integer division, Hence, the user must have to use the command q = 1 + ( ( x – 1 ) / y ) ; to execute it even faster and there is no certainty of an overflow of data....
🌐
Reddit
reddit.com β€Ί r/learnprogramming β€Ί writing a ceiling function in c
r/learnprogramming on Reddit: Writing a ceiling function in C
September 18, 2013 -

I'm trying to write a function that (kind of) emulates the ceiling function in the math library, in a way that is more useful for my purposes. The code I've written is not working as expected. It should take two integer arguments, divide one by the other, and round up to the nearest integer no matter what the result is.

Ex: 2/2=1 --> answer is 1; 10/3=3.333... --> round up to 4.

This is my code:

int ceiling(int number, int value){
    float a = number/value;
    int b = number/value;
    float c = a - b;
    if(c!=0){
        b=b+1;
    }
    return b;
}

Using the examples above,

ceiling(2,2); would return 1, as expected.

ceiling(10,3); returns 3, when it should return 4.

My thought process is that if a is a float, then 10/3 should be 3.33333..., and if b is an int, then 10/3 should be 3. If c is a float, a-b should be 0.33333..., then since c!=0, ceiling should increment b and return that value. Where am I going wrong?

🌐
AskPython
askpython.com β€Ί home β€Ί is there a ceiling equivalent of // operator in python?
Is There a Ceiling Equivalent of // Operator in Python? - AskPython
May 25, 2023 - Basically, instead of the ceiling dividing 10 by 3, we floor divided -10 by 3 and then multiplied it by a minus. Floor division of -10 by 3 will be -4 and -(-4) will be 4. This is our desired output for the ceiling division.
🌐
Milddev
milddev.com β€Ί python-ceiling-division
Python Ceiling Division
Mastering ceiling division in Python unlocks cleaner code and fewer edge-case bugs. Whether you choose math.ceil(a / b) for clarity or -(-a // b) for speed, understanding both methods gives you flexibility in any project.