a/b does integer division. If either a or b is negative, the result depends on the compiler (rounding can go toward zero or toward negative infinity in pre-C99; in C99+, the rounding goes toward 0). The result has type int. floor(a/b) does the same division, converts the result to double, discards the (nonexistent) fractional part, and returns the result as a double.

Answer from Pete Becker on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ ref_math_floor.php
C Math floor() Function
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... printf("%f", floor(0.60)); printf("%f", floor(0.40)); printf("%f", floor(5)); printf("%f", floor(5.1)); printf("%f", floor(-5.1)); printf("%f", floor(-5.9)); Try it Yourself ยป
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2727635 โ€บ how-can-i-use-floor-division-with-variable-in-c-language
How can I use floor division with variable in C language | Sololearn: Learn to code for FREE!
When either operand was a floating point number, floating point division is performed. ... Copy this snippet and paste into an empty C code in Code Playground int i1 = 5, i2 = 2; float f1 = 5.0, f2 = 2.0; printf("integer division:\n%d/%d = %d\n", i1, i2, i1 / i2); printf("floating point division\n%.1f %.1f = %.1f\n", f1, f2, f1 / f2);
๐ŸŒ
Codecademy
codecademy.com โ€บ forum_questions โ€บ 5384587e52f863dcb8001c82
what is floor division // | Codecademy
If you imagine a room where 3 is on the ceiling and 2 is on the floor. 2.5 would fit in the middle. Floor division means the โ€œ//โ€œ will always take the floor or the lower 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
In C, the integer division is toward zero, for both positive and negative integers, and only defined as such starting C99 (it is implementation defined before that). Be mindful about it if your codebase is in C89 or C90. ... In Python 2 and 3, the integer division is toward -โˆž, which means ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-floor-function
C floor() Function - GeeksforGeeks
July 7, 2024 - The floor(x) function takes a floating-point number x and returns the largest integer less than or equal to x. Internally, it checks the value of x and rounds it down to the nearest integer. If x is already an integer, it returns x itself.
Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ c_standard_library โ€บ c standard library: floor function
C Standard Library: floor Function
August 29, 2012 - This function rounds a number down to the nearest integer multiple of the specified significance. Following is the syntax of the C library function floor() โˆ’
Top answer
1 of 6
9

Less assembly instructions in the generated code and quicker path to the result I think.

For the RISC machines with huge numbers of registers this one is better, as there are no branches at all and it is good for the pipeline and the cache.

For x86 actually it does not matter.

int floor_div3(int a, int b) {
    int d = a / b;


    return d * b == a ? d : d - ((a < 0) ^ (b < 0));
}
2 of 6
7

div() functions in standard C

I think you should look at the div() functions from <stdlib.h>. (They are standard C functions and are defined in all versions of the standard, despite the link to the POSIX specification.)

The C11 standard ยง7.22.6.2 specifies:

The div โ€ฆ functions compute numer / denom and numer % denom in a single operation.

Note that C11 specifies integer division in ยง6.5.5 (and C99 was similar):

When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded.105)

105) This is often called "truncation toward zero".

but C90 (ยง6.3.5) was more flexible yet less useful:

When integers are divided and the division is inexact. if both operands are positive the result of the / operator is the largest integer less than the algebraic quotient and the result of the % operator is positive. If either operand is negative, whether the result of the / operator is the largest integer less than or equal to the algebraic quotient or the smallest integer greater than or equal to the algebraic quotient is implementation-defined, as is the sign of the result of the % operator.

floor_div()

The computational code for the requested floor_div() using div() is neat and tidy.

int floor_div(int a, int b)
{
    assert(b != 0);
    div_t r = div(a, b);
    if (r.rem != 0 && ((a < 0) ^ (b < 0)))
        r.quot--;
    return r.quot;
}

Test code

The print formatting in the code below is tailored rather precisely to the sample data. (It would be better, but more expansive, to use %4d and %-4d throughout). This code prints lines of length 89 characters plus newline; the more general layout would print lines of length 109. Neither avoids the horizontal scroll bar on SO.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

static int floor_div(int a, int b)
{
    assert(b != 0);
    div_t r = div(a, b);
    if (r.rem != 0 && ((a < 0) ^ (b < 0)))
        r.quot--;
    return r.quot;
}

static void test_floor_div(int n, int d)
{
    assert(d != 0);
    printf(   "%3d/%-2d = %-3d (%3d)", +n, +d, floor_div(+n, +d), +n / +d);
    printf(";  %3d/%-3d = %-4d (%4d)", +n, -d, floor_div(+n, -d), +n / -d);
    if (n != 0)
    {
        printf(";  %4d/%-2d = %-4d (%4d)", -n, +d, floor_div(-n, +d), -n / +d);
        printf(";  %4d/%-3d = %-3d (%3d)", -n, -d, floor_div(-n, -d), -n / -d);
    }
    putchar('\n');
}

int main(void)
{
    int numerators[] = { 0, 1, 2, 4, 9, 23, 291 };
    enum { NUM_NUMERATORS = sizeof(numerators) / sizeof(numerators[0]) };
    int denominators[] = { 1, 2, 3, 6, 17, 23 };
    enum { NUM_DENOMINATORS = sizeof(denominators) / sizeof(denominators[0]) };

    for (int i = 0; i < NUM_NUMERATORS; i++)
    {
        for (int j = 0; j < NUM_DENOMINATORS; j++)
            test_floor_div(numerators[i], denominators[j]);
        putchar('\n');
    }

    return 0;
}

Test output

  0/1  = 0   (  0);    0/-1  = 0    (   0)
  0/2  = 0   (  0);    0/-2  = 0    (   0)
  0/3  = 0   (  0);    0/-3  = 0    (   0)
  0/6  = 0   (  0);    0/-6  = 0    (   0)
  0/17 = 0   (  0);    0/-17 = 0    (   0)
  0/23 = 0   (  0);    0/-23 = 0    (   0)

  1/1  = 1   (  1);    1/-1  = -1   (  -1);    -1/1  = -1   (  -1);    -1/-1  = 1   (  1)
  1/2  = 0   (  0);    1/-2  = -1   (   0);    -1/2  = -1   (   0);    -1/-2  = 0   (  0)
  1/3  = 0   (  0);    1/-3  = -1   (   0);    -1/3  = -1   (   0);    -1/-3  = 0   (  0)
  1/6  = 0   (  0);    1/-6  = -1   (   0);    -1/6  = -1   (   0);    -1/-6  = 0   (  0)
  1/17 = 0   (  0);    1/-17 = -1   (   0);    -1/17 = -1   (   0);    -1/-17 = 0   (  0)
  1/23 = 0   (  0);    1/-23 = -1   (   0);    -1/23 = -1   (   0);    -1/-23 = 0   (  0)

  2/1  = 2   (  2);    2/-1  = -2   (  -2);    -2/1  = -2   (  -2);    -2/-1  = 2   (  2)
  2/2  = 1   (  1);    2/-2  = -1   (  -1);    -2/2  = -1   (  -1);    -2/-2  = 1   (  1)
  2/3  = 0   (  0);    2/-3  = -1   (   0);    -2/3  = -1   (   0);    -2/-3  = 0   (  0)
  2/6  = 0   (  0);    2/-6  = -1   (   0);    -2/6  = -1   (   0);    -2/-6  = 0   (  0)
  2/17 = 0   (  0);    2/-17 = -1   (   0);    -2/17 = -1   (   0);    -2/-17 = 0   (  0)
  2/23 = 0   (  0);    2/-23 = -1   (   0);    -2/23 = -1   (   0);    -2/-23 = 0   (  0)

  4/1  = 4   (  4);    4/-1  = -4   (  -4);    -4/1  = -4   (  -4);    -4/-1  = 4   (  4)
  4/2  = 2   (  2);    4/-2  = -2   (  -2);    -4/2  = -2   (  -2);    -4/-2  = 2   (  2)
  4/3  = 1   (  1);    4/-3  = -2   (  -1);    -4/3  = -2   (  -1);    -4/-3  = 1   (  1)
  4/6  = 0   (  0);    4/-6  = -1   (   0);    -4/6  = -1   (   0);    -4/-6  = 0   (  0)
  4/17 = 0   (  0);    4/-17 = -1   (   0);    -4/17 = -1   (   0);    -4/-17 = 0   (  0)
  4/23 = 0   (  0);    4/-23 = -1   (   0);    -4/23 = -1   (   0);    -4/-23 = 0   (  0)

  9/1  = 9   (  9);    9/-1  = -9   (  -9);    -9/1  = -9   (  -9);    -9/-1  = 9   (  9)
  9/2  = 4   (  4);    9/-2  = -5   (  -4);    -9/2  = -5   (  -4);    -9/-2  = 4   (  4)
  9/3  = 3   (  3);    9/-3  = -3   (  -3);    -9/3  = -3   (  -3);    -9/-3  = 3   (  3)
  9/6  = 1   (  1);    9/-6  = -2   (  -1);    -9/6  = -2   (  -1);    -9/-6  = 1   (  1)
  9/17 = 0   (  0);    9/-17 = -1   (   0);    -9/17 = -1   (   0);    -9/-17 = 0   (  0)
  9/23 = 0   (  0);    9/-23 = -1   (   0);    -9/23 = -1   (   0);    -9/-23 = 0   (  0)

 23/1  = 23  ( 23);   23/-1  = -23  ( -23);   -23/1  = -23  ( -23);   -23/-1  = 23  ( 23)
 23/2  = 11  ( 11);   23/-2  = -12  ( -11);   -23/2  = -12  ( -11);   -23/-2  = 11  ( 11)
 23/3  = 7   (  7);   23/-3  = -8   (  -7);   -23/3  = -8   (  -7);   -23/-3  = 7   (  7)
 23/6  = 3   (  3);   23/-6  = -4   (  -3);   -23/6  = -4   (  -3);   -23/-6  = 3   (  3)
 23/17 = 1   (  1);   23/-17 = -2   (  -1);   -23/17 = -2   (  -1);   -23/-17 = 1   (  1)
 23/23 = 1   (  1);   23/-23 = -1   (  -1);   -23/23 = -1   (  -1);   -23/-23 = 1   (  1)

291/1  = 291 (291);  291/-1  = -291 (-291);  -291/1  = -291 (-291);  -291/-1  = 291 (291)
291/2  = 145 (145);  291/-2  = -146 (-145);  -291/2  = -146 (-145);  -291/-2  = 145 (145)
291/3  = 97  ( 97);  291/-3  = -97  ( -97);  -291/3  = -97  ( -97);  -291/-3  = 97  ( 97)
291/6  = 48  ( 48);  291/-6  = -49  ( -48);  -291/6  = -49  ( -48);  -291/-6  = 48  ( 48)
291/17 = 17  ( 17);  291/-17 = -18  ( -17);  -291/17 = -18  ( -17);  -291/-17 = 17  ( 17)
291/23 = 12  ( 12);  291/-23 = -13  ( -12);  -291/23 = -13  ( -12);  -291/-23 = 12  ( 12)
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ floor() in c
floor() Function in C - Scaler Topics
June 16, 2024 - This function rounds off the floating point numbers (float and double values) into their immediately smaller or equal integer. We can also use integers with the floor() function, but it will return the input as it is.
๐ŸŒ
AlgoCademy
algocademy.com โ€บ link
Floor Division in C++ | AlgoCademy
C++ integer division with / truncates toward zero โ€” it does not floor toward negative infinity. For positive numbers this is identical to floor division, so the difference is invisible until negative numbers appear.
๐ŸŒ
Ucdavis
scalettar.physics.ucdavis.edu โ€บ cosmos โ€บ modulo.pdf pdf
C PROGRAMMING: INTEGER DIVISION AND MODULO (%)
23/4, instead of getting 5.75 it gets 5. The computer literally asks how many times 4 goes
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ library-function โ€บ math.h โ€บ floor
C floor() - C Standard Library
Become a certified C programmer. Try Programiz PRO! ... The floor() function takes a single argument and returns adouble type value. It is defined in <math.h> header file.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ floor-division
Floor division
Floor division is a division operation that rounds the result down to the nearest whole number or integer, which is less than or equal to the normal division result. The floor function is mathematically denoted by this โŒŠ โŒ‹ symbol.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-do-floor-division-in-python-java-cpp
How to do floor division in Python/Java/C++
There are functions used to apply floor in different languages: In C++, we have to include the cmath library and the function to call floor():
๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ c_language โ€บ standard_library_functions โ€บ math_h โ€บ floor.php
C Language: floor function (Floor)
In the C Programming Language, the floor function returns the largest integer that is smaller than or equal to x (ie: rounds downs the nearest integer).
๐ŸŒ
Marcelkliemannel
marcelkliemannel.com โ€บ articles โ€บ 2021 โ€บ dont-confuse-integer-division-with-floor-division
Don't Confuse Integer Division with Floor Division | Marcel Kliemannel
If y is not 0, divide x by y using floor division (which is expressed by the โŒŠโŒ‹ symbol), multiply the result by y, and subtract this from x. On the other hand, if x is 0, return x. Floor division simplified means that the real number result of the division is always rounded down.