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.

Answer from chqrlie on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_floor.htm
C library - floor() function
#include <stdio.h> #include <math.h> int main() { double start = 6.5; double end = 9.5; printf("Table of Floor Integers:\n"); for (double num = start; num <= end; num += 1.0) { double result = floor(num); printf("Floor(%.2lf) = %.0lf\n", num, result); } return 0; } ...
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › math_h › floor.php
C Language: floor function (Floor)
In the C Language, the required ... floor of value */ result = floor(value); /* Display the result of the calculation */ printf("The floor of %f is %f\n", value, result); return 0; }...
🌐
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....
🌐
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", ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-floor-function
C floor() Function - GeeksforGeeks
August 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.
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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › floor-floorf-floorl
floor, floorf, floorl | Microsoft Learn
November 3, 2025 - By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT. For more compatibility information, see Compatibility. // crt_floor.c // This example displays the largest integers // less than or equal to the floating-point values 2.8 // and -2.8.
🌐
Tutorial Gateway
tutorialgateway.org › c-floor-function
C floor Function
April 5, 2025 - #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 ...
Find elsewhere
🌐
Linux Hint
linuxhint.com › floor-function-in-c
Floor Function in C – Linux Hint
July 7, 2024 - From the output screenshot, we all can see that the program is running seamlessly. Several additional variations of inputs were used for testing to get a better understanding of the floor function: Now, for this example, let’s try passing negative decimal values to our floor function.
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › floor
floor, floorf, floorl - cppreference.com
May 23, 2024 - ... #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....
🌐
Vultr
docs.vultr.com › clang › standard-library › math-h › floor
C math.h floor() - Round Down to Integer | Vultr Docs
September 27, 2024 - This code demonstrates the use of floor() by rounding down the value of myFloat (3.7) to 3.0. The function correctly identifies the largest integer less than or equal to 3.7.
🌐
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 ...
🌐
Scaler
scaler.com › home › topics › floor() in c
floor() Function in C - Scaler Topics
July 16, 2024 - This function returns the nearest ... to it as an argument. For example, if we pass a floating point number 5.6677 to the floor() function, the return value will be 5 (an integer)....
🌐
IncludeHelp
includehelp.com › c-programs › c-basic-programs-to-demonstrate-example-of-floor-and-ceil-functions.aspx
C program to demonstrate example of floor() and ceil() functions
May 26, 2009 - /* C program to demonstrate example of floor and ceil functions.*/ #include <stdio.h> #include <math.h> int main() { float val; float fVal,cVal; printf("Enter a float value: "); scanf("%f",&val); fVal=floor(val); cVal =ceil(val); printf("floor value:%f \nceil value:%f\n",fVal,cVal); return 0; }
🌐
Programming Simplified
programmingsimplified.com › c › math.h › floor
floor function | Programming Simplified
Home » C programming » math.h » floor function · Floor function returns the greatest integer not greater than x. For example, if the input is 2.25 then the output will be 2.00.
🌐
Aticleworld
aticleworld.com › home › floor function in c
floor function in C - Aticleworld
December 24, 2022 - If arg is ±0, it is returned, unmodified. If arg is NaN, NaN is returned. #include <math.h> #include <stdio.h> int main(void) { printf("floor(-0.0) = %+.1f\n", floor(-0.0)); printf("floor(-Inf) = %+f\n", floor(-INFINITY)); return 0; } ... Use of pow function in C language.
🌐
TutorialKart
tutorialkart.com › c-programming › c-math-floor
floor() Function - C math.h - Syntax, Parameters, Examples
September 28, 2021 - The floor() function is applied to num to round it down to the nearest whole number, resulting in 3.0. The original and the rounded values are printed to the console. Original number: 3.70 After applying floor(): 3.00 · This example demonstrates ...
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.
🌐
W3Resource
w3resource.com › c-programming › math › c-floor.php
C floor() function
December 24, 2022 - The floor() function is used to calculate the largest integer that is less than or equal to x. ... Returns the floating-point result as a double value. ... The following example shows the usage of floor() function.
🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c – arithmetic functions › c – floor() function
C floor() function | C Arithmetic functions | Fresh2Refresh
October 22, 2024 - C floor() function:floor( ) function in C returns the nearest integer value which is less than or equal to the floating point argument passed to this