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 num = 8.33; double result = floor(num); printf("Floor integer of %.2lf = %.0lf\n", num, result); return 0; } On execution of above code, we get the following result −
🌐
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; }...
🌐
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", ...
🌐
W3Schools
w3schools.com › c › ref_math_floor.php
C Math floor() Function
C Examples C Real-Life Examples ... 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 ...
🌐
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 ...
🌐
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.
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › floor
floor, floorf, floorl - cppreference.com
May 23, 2024 - Run this code · #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)); } Possible output: floor(+2.7) = +2.0 ...
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.
🌐
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)....
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.
🌐
Programming Simplified
programmingsimplified.com › c › math.h › floor
floor function | Programming Simplified
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. Declaration: double floor(double x); #include <stdio.h> #include <math.h> int main() { double n, result; printf("Enter a number to round it down\n"); scanf("%lf", ...
🌐
Linux Hint
linuxhint.com › floor-function-in-c
Floor Function in C – Linux Hint
December 21, 2022 - The coding format will be similar to Example 1 by changing the input part for negative decimal values and passing them into the floor function to analyze the function’s reaction: The changes made to the code are visible from the screenshot above. As you can see, the hardcoded negative decimal value is -99.4 if we try to dry run the program ourselves.
🌐
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
Enter a float value: 123.45 floor value:123.000000 ceil value:124.000000 ... Calculate the distance between two cities from kilometers to meters, centimeters, feet and inches using C program · C program to find area and perimeter of the rectangle · C program to generate random numbers within ...
🌐
Vultr
docs.vultr.com › clang › standard-library › math-h › floor
C math.h floor() - Round Down to Integer | Vultr Docs
January 5, 2025 - 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.
🌐
W3Resource
w3resource.com › c-programming › math › c-floor.php
C floor() function
June 3, 2024 - 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.
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re79.html
floor - C in a Nutshell [Book]
December 16, 2005 - 13.1. Streams13.1.1. Text Streams13.1.2. Binary Streams13.2. Files13.2.1. File Position13.2.2. Buffers13.2.3. The Standard Streams13.3. Opening and Closing Files13.3.1. Opening a File13.3.2. Access Modes13.3.3. Closing a File13.4. Reading and Writing13.4.1. Byte-Oriented and Wide-Oriented Streams13.4.2. Error Handling13.4.2.1. Return values and status flags13.4.2.2. The error variable errno13.4.3. Unformatted I/O13.4.3.1. Reading characters13.4.3.2. Putting a character back13.4.3.3. Writing characters13.4.3.4. Reading strings13.4.3.5. Writing strings13.4.3.6. Reading and writing blocks13.4.4. Formatted Output13.4.4.1. The printf() function family13.4.4.2.
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c – arithmetic functions › c – floor() function
C floor() function | C Arithmetic functions | Fresh2Refresh
September 23, 2020 - 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
🌐
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.