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 › home › c_standard_library › c standard library: floor function
C Standard Library: floor Function
August 29, 2012 - Following is the syntax of the C library function floor() − ... This function returns the largest integral value not greater than x. Following is the basic C library example to see the demonstration of floor() function.
🌐
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; }...
Discussions

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
Compilation error; why can't gcc recognize the floor function here?
Have you tried gcc -lm option? More on reddit.com
🌐 r/C_Programming
27
1
August 23, 2021
My teacher keeps insisting that the whole part of a number is the same as the floor function. Is he right?
From Wikipedia : The floor function (or greatest integer 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)... Historically, the floor of x has been–and still is–called the integral part or integer part of x, often denoted [x] (as well as a variety of other notations). However, the same term, integer part, is also used for truncation towards zero, which differs from the floor function for negative numbers. One general remark: in mathematics it is considered unwise to argue about definitions. As long as a person properly defines their notations and concepts, they are fine. Of course, if they use a different notation than everybody else, they may be considered weird, but it does not mean they made a mathematical mistake. Instead of arguing, ask a person for the source of their definition: a textbook, an encyclopedia, and so on. More on reddit.com
🌐 r/learnmath
29
62
April 4, 2024
🌐
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.
🌐
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", ...
🌐
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 ...
🌐
W3Schools
w3schools.com › c › ref_math_floor.php
C Math floor() Function
C Examples C Real-Life Examples ... ... 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 » · The floor() function rounds a number DOWN to the nearest 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.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › floor-floorf-floorl
floor, floorf, floorl | Microsoft Learn
October 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.
🌐
Linux Hint
linuxhint.com › floor-function-in-c
Floor Function in C – Linux Hint
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.
🌐
Scaler
scaler.com › home › topics › floor() in c
floor() Function in C - Scaler Topics
June 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)....
🌐
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.
🌐
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
June 24, 2020 - /* 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; }
🌐
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....
🌐
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 ...
🌐
Aticleworld
aticleworld.com › home › floor function in c
floor function in C - Aticleworld
September 28, 2021 - 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
February 21, 2025 - 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 ...
🌐
AlphaCodingSkills
alphacodingskills.com › c › notes › c-math-floor.php
C - floor() Function - AlphaCodingSkills
May 25, 2023 - In other words, it rounds the fraction DOWN of the given number. double floor (double x); float floorf (float x); long double floorl (long double x); Returns the next lowest integer value by rounding DOWN the specified number, if necessary. In the example below, floor() function is used to ...
🌐
Vultr
docs.vultr.com › clang › standard-library › math-h › floor
C math.h floor() - Round Down to Integer | Vultr Docs
July 7, 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.