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
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › math_h › floor.php
C Language: floor function (Floor)
In the C Language, the required header for the floor function is: ... /* Example using floor by TechOnTheNet.com */ #include <stdio.h> #include <math.h> int main(int argc, const char * argv[]) { /* Define temporary variables */ double value; double result; /* Assign the value we will find the floor of */ value = 1.6; /* Calculate the floor of value */ result = floor(value); /* Display the result of the calculation */ printf("The floor of %f is %f\n", value, result); return 0; }
🌐
Programiz
programiz.com › c-programming › library-function › math.h › floor
C floor() - C Standard Library
The function prototypes for the long double and float versions of the floor() function are: long double floorl(long double arg); float floorf(float arg); #include <stdio.h> #include <math.h> int main() { double num = -8.33; double result = floor(num); printf("Floor integer of %.2f = %.0f", ...
🌐
Programiz
programiz.com › cpp-programming › library-function › cmath › floor
C++ floor() - C++ Standard Library
num = -34.251; result = floor(num); cout << "Floor of " << num << " = " << result << endl;
🌐
W3Schools
w3schools.com › c › ref_math_floor.php
C Math floor() Function
C Examples C Real-Life Examples ... printf("%f", floor(-5.9)); Try it Yourself » · The floor() function rounds a number DOWN to the nearest integer....
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › floor-floorf-floorl
floor, floorf, floorl | Microsoft Learn
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....
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.
🌐
TutorialsPoint
tutorialspoint.com › home › c_standard_library › c standard library: floor function
C Standard Library: floor Function
August 29, 2012 - 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.
🌐
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
🌐
W3Resource
w3resource.com › c-programming › math › c-floor.php
C floor() function
The following example shows the usage of floor() function. #include <math.h> #include <stdio.h> int main(void) { double x, y; x = 4.6; y = -4.6; printf("Before applying floor()"); printf("\nx = %lf", x); printf("\ny = %lf", y); x = floor(x); y = floor(y); printf("\n\nAfter applying floor()"); printf("\nx = %lf", x); printf("\ny = %lf", y); } ... Before applying floor() x = 4.600000 y = -4.600000 After applying floor() x = 4.000000 y = -5.000000
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.
🌐
Tutorial Gateway
tutorialgateway.org › c-floor-function
C floor Function
April 5, 2025 - In this program, we will find the same and display the output C Programming. #include <stdio.h> #include <math.h> int main() { printf("\n The Floor Value of 0.75 = %.2f ", floor(0.75)); printf("\n The Floor Value of 12.25 = %.2f ", floor(12.25)); printf("\n The Floor Value of 152.50 = %.2f ", floor(152.50)); printf("\n The Floor Value of -12.36 = %.2f ", floor(-12.36)); printf("\n The Floor Value of -27.82 = %.2f ", floor(-27.32)); printf("\n The Floor Value of -90.50 = %.2f ", floor(-90.50)); return 0; }
🌐
Scaler
scaler.com › home › topics › floor() in c
floor() Function in C - Scaler Topics
June 16, 2024 - The floor() return value in C is an integer value immediately smaller or equal to the floating point number (float or double value) passed to it. ... In the above example, we are rounding off num to its immediately smaller integer using the ...
🌐
Cplusplus
cplusplus.com › reference › cmath › floor
Floor
double floor (double x); float floor (float x);long double floor (long double x); double floor (T x); // additional overloads for integral types · Round down value · Rounds x downward, returning the largest integral value that is not greater than x. Header <tgmath.h> provides a type-generic macro version of this function. Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type).
🌐
Vultr
docs.vultr.com › clang › standard-library › math-h › floor
C math.h floor() - Round Down to Integer | Vultr Docs
September 27, 2024 - In this code, the floor() function rounds down -3.2 to -4.0. Despite the input being negative, the function extends the value down to the nearest whole number. Utilize floor() function in loops to handle iterations based on floating-point calculations.
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › floor
floor, floorf, floorl - cppreference.com
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"
🌐
Tpoint Tech
tpointtech.com › floor-function-in-c-programming
Floor() Function in C - Tpoint Tech
March 17, 2025 - Floor() Function in C · memcmp() in C · Find Day from Day in C without Using Function · Find Median of 1D Array Using Functions in C · Find Reverse of an Array in C Using Functions · Find Occurrence of Substring in C using Function · Find out Power without Using POW Function in C · Exponential() in C · Float in C · islower() in C · memcpy() in C · memmove() in C · Matrix Calculator in C ·
🌐
MedCalc
medcalc.org › en › manual › floor-function.php
FLOOR function calculator and graph - MedCalc Manual
September 9, 2025 - FLOOR(x) rounds the number x down. The argument x can be a real number or a matrix. When it is a matrix, the function returns a matrix with the same dimensions and with the FLOOR function applied to all elements.
🌐
TutorialKart
tutorialkart.com › c-programming › c-math-floor
floor() Function - C math.h - Syntax, Parameters, Examples
February 21, 2025 - The floor() function is applied to num, which rounds it down to -4.0 because -4 is the largest integer less than -3.2. The original and the rounded values are printed to the console. Original number: -3.20 After applying floor(): -4.00 · ❮ Previous Next ❯ · GPS Area Calculator ·