The easiest solution would probably be to just let C do it. Per ยง 6.3.1.4 of the C11 spec:

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero).

So, all you need to do is convert the value to an integer, than convert that back to a float:

float myFloor(float value) {
  return (float) (int) value;
}

If you need to handle negatives, you can easily do so:

float myFloor(float value) {
  float tmp = (float) (int) value;
  return (tmp != value) ? (tmp - 1.0f) : tmp;
}
Answer from nemequ on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-floor-function
C floor() Function - GeeksforGeeks
July 7, 2024 - The floor(x) function in C is used to compute the largest integer value less than or equal to a given number.
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ ref_math_floor.php
C Math floor() Function
The floor() function is defined in the <math.h> header file. Tip: To round a number UP to the nearest integer, look at the ceil() function.
Discussions

How do I create my own floor function in C? - Stack Overflow
I'm creating a program which will take the day, month and year from the user and then display that in three different formats, a calendar format, an ordinal format and in the ISO weekday number. In... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Write your own implementation of math's floor function, C - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I was thinking about the floor function available in math.h. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Does anyone know how to create a floor function in c
Can you not just cast it to an int? float f; int i; f = 4.6; i = (int)f; edit : learned about the html editor More on reddit.com
๐ŸŒ r/AskProgramming
11
0
January 5, 2025
Writing a ceiling function in C
The variables number and value are both ints. Therefore the expression number/value is integer division: it is the division of two int values. This is integer division, and it returns an integer value, regardless of whether you subsequently decide to store that integer value in a float variable or an int variable. To get floating-point division, you need to cast at least one of the two variables number and value to float before you do the division. For example, float a = (float)number / value; Here, number is cast to a float before the division. Because one of the operands of the division is a floating-point value now, the division is floating-point division, not integer division. === For what it's worth, why aren't you just doing this? #include int ceiling(int number, int value) { return ceil((float)number / value); } More on reddit.com
๐ŸŒ r/learnprogramming
4
5
September 1, 2013
๐ŸŒ
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).
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ floor() in c
floor() Function in C - Scaler Topics
June 16, 2024 - The floor() is a library function in C defined in the <math.h> header file. This function returns the nearest integer value, which is less than or equal to the floating point number (float or double) passed to it as an argument.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ c_standard_library โ€บ c_function_floor.htm
C library - floor() function
The C library floor() function of type double accept the single parameter(x) to return the largest integer value less than or equal to, by the given values. This function rounds a number down to the nearest integer multiple of the specified
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ c โ€บ numeric โ€บ math โ€บ floor
floor, floorf, floorl - cppreference.com
May 23, 2024 - The largest representable floating-point values are exact integers in all standard floating-point formats, so this function never overflows on its own; however the result may overflow any integer type (including intmax_t), when stored in an integer variable. ... #include <math.h> #include <stdio.h> int main(void) { printf("floor(+2.7) = %+.1f\n", floor(2.7)); printf("floor(-2.7) = %+.1f\n", floor(-2.7)); printf("floor(-0.0) = %+.1f\n", floor(-0.0)); printf("floor(-Inf) = %+f\n", floor(-INFINITY)); } ... Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/math/floor&oldid=172013"
Find elsewhere
Top answer
1 of 3
5

The easiest solution would probably be to just let C do it. Per ยง 6.3.1.4 of the C11 spec:

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero).

So, all you need to do is convert the value to an integer, than convert that back to a float:

float myFloor(float value) {
  return (float) (int) value;
}

If you need to handle negatives, you can easily do so:

float myFloor(float value) {
  float tmp = (float) (int) value;
  return (tmp != value) ? (tmp - 1.0f) : tmp;
}
2 of 3
1

One way is to convert-

float myFloor(float value) {
  return (float) (int) value;
}

but you can't tackle it this way. The best way of writing your own implementation is to steal the one from the C Standard Library on your platform. But note that might contain platform-specific nuances so might not be portable.

The C Standard Library floor function is typically clever in that it doesn't work by taking a conversion to an integral type. If it did then you'd run the risk of signed integer overflow, the behaviour of which is undefined. (Note that the smallest possible range for an int is -32767 to +32767).

The precise implementation is also dependent on the floating point scheme used on your platform.

For a platform using IEEE754 floating point, and a long long type you could adopt this scheme:

  1. If the magnitude of the number is greater than the 53rd power of 2, return it back (as it's already integral).
  2. Else, cast to a 64 bit type (long long), and return it back.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-runtime-library โ€บ reference โ€บ floor-floorf-floorl
floor, floorf, floorl | Microsoft Learn
October 3, 2025 - ... Calculates the floor of a value. ... C11 or later ... The floor functions return a floating-point value that represents the largest integer that is less than or equal to x....
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ library-function โ€บ math.h โ€บ floor
C floor() - C Standard Library
The floor() function calculates the nearest integer less than or equal to the argument passed.
๐ŸŒ
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 ...
Top answer
1 of 5
7

Both of your attempts have limitations:

  • If the double value is outside the range of the int type, converting to int is implementation defined.
  • If the double value is negative but integral, returning (int)num - 1 is incorrect.

Here is an (almost) portable version that tries to handle all cases:

double my_floor_2(double num) {
    if (num >= LLONG_MAX || num <= LLONG_MIN || num != num) {
        /* handle large values, infinities and nan */
        return num;
    }
    long long n = (long long)num;
    double d = (double)n;
    if (d == num || num >= 0)
        return d;
    else
        return d - 1;
}

It should be correct if type long long has more value bits than type double, which is the case on most modern systems.

2 of 5
5

No, you can't tackle it this way. The best way of writing your own implementation is to take the one from the C Standard Library on your platform. But note that might contain platform specific nuances so might not be portable.

The C Standard Library floor function is typically clever in that it doesn't work by taking a conversion to an integral type. If it did then you'd run the risk of signed integer overflow, the behaviour of which is undefined. (Note that the smallest possible range for an int is -32767 to +32767).

The precise implementation is also dependent on the floating point scheme used on your platform.

For a platform using IEEE754 floating point, and a long long type you could adopt this scheme:

  1. If the magnitude of the number is greater than 253, return it (as it's already integral).
  2. Else, cast to a 64-bit type (long long), and return it back.
๐ŸŒ
Aticleworld
aticleworld.com โ€บ home โ€บ floor function in c
floor function in C - Aticleworld
September 28, 2021 - The floor function in C computes the largest integer value not greater than x or computes the largest integer value not greater than x.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ c# โ€บ math functions โ€บ .floor()
C# (C Sharp) | Math Functions | .Floor() | Codecademy
March 31, 2023 - The Math.Floor() method is used to find the largest whole integer that is less than or equal to the argument, x, passed in. The argument can be of a float or double type.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ mathematics โ€บ floor-function
Floor Function - GeeksforGeeks
July 23, 2025 - The ceiling function is denoted using the symbol โŒŠ โŒ‹. Thus we can denote Floor(x) by โŒŠ x โŒ‹. Other than this, floor function is also denoted by Floor(x). ... The domain of a function is the set of all possible input values, while the ...
๐ŸŒ
Quora
quora.com โ€บ How-is-the-floor-function-defined-I-would-like-to-know-what-floor-0-999-is
How is the floor function defined? I would like to know what floor (0.999) is. - Quora
Answer (1 of 6): Here are a few equivalent definitions: 1. The floor function is the largest integer that isn't greater than the input. 2. The floor function is shorthand for โ€œRound down to the preceeding integer."
๐ŸŒ
Vultr
docs.vultr.com โ€บ clang โ€บ standard-library โ€บ math-h โ€บ floor
C math.h floor() - Round Down to Integer | Vultr Docs
September 27, 2024 - The floor() function, defined in the C standard library math.h, enables rounding down of floating-point numbers to the nearest integer less than or equal to the original number.
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ c-in-a โ€บ 0596006977 โ€บ re79.html
floor - C in a Nutshell [Book]
December 16, 2005 - Integer Types with Exact Width (C99)2.3. Floating-Point Types2.4. Complex Floating-Point Types (C99)2.5. Enumerated Types2.6. The Type void2.6.1. void in Function Declarations2.6.2. Expressions of Type void2.6.3.
Authors ย  Peter PrinzTony Crawford
Published ย  2005
Pages ย  618