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
🌐
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).
🌐
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.
🌐
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.
🌐
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.
🌐
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.
🌐
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"
🌐
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.
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
The floor functions return a floating-point value that represents the largest integer that is less than or equal to x. There's no error return. floor has an implementation that uses Streaming SIMD Extensions 2 (SSE2).
🌐
Tutorial Gateway
tutorialgateway.org › c-floor-function
C floor Function
April 5, 2025 - The C floor function is a Math Function used to return the closest integer value, which is less than or equal to a given number or expression
🌐
Linux Hint
linuxhint.com › floor-function-in-c
Floor Function in C – Linux Hint
That’s why nothing was displayed ... the floor function in C language is a mathematics function, which needs to match the header for the compiler to add a math library for accessing its functions and macros at the time of execution:...
🌐
W3Resource
w3resource.com › c-programming › math › c-floor.php
C floor() function
C floor() function (math.h): The floor() function is used to calculate 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.
🌐
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.
🌐
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
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.
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re79.html
floor - C in a Nutshell [Book]
December 16, 2005 - 2.1. Typology2.2. Integer Types2.2.1. 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
🌐
Tpoint Tech
tpointtech.com › floor-function-in-c-programming
Floor() Function in C Programming
March 17, 2025 - floor() in Mathematics The floor() function in mathematics requires a real number, and it computes the greatest integer that can either be less than or equal...
🌐
Codingtag
codingtag.com › floor()-function-in-c
Floor() function in C
The floor() function in C is used to round down a floating-point number to the nearest integer value.