๐ŸŒ
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....
๐ŸŒ
W3Schools
w3schools.com โ€บ cpp โ€บ ref_math_floor.asp
C++ Math floor() Function
The floor() function is defined in the <cmath> header file. Tip: To round a number UP to the nearest integer, look at the ceil() function. Tip: To round a number to the nearest integer in either direction, look at the round() function.
๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ c_language โ€บ standard_library_functions โ€บ math_h โ€บ floor.php
C Language: floor function (Floor)
/* 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; } When compiled and run, this application will output: ... While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy. Copyright ยฉ 2003-2026 TechOnTheNet.com.
๐ŸŒ
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.
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ c โ€บ numeric โ€บ math โ€บ floor
floor, floorf, floorl - cppreference.com
May 23, 2024 - 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"
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_math.php
C Math Functions
The ceil() function rounds a number upwards to its nearest integer, and the floor() method rounds a number downwards to its nearest integer, and returns the result:
๐ŸŒ
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
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.
๐ŸŒ
W3Schools Blog
w3schools.blog โ€บ home โ€บ c math functions
C Math Functions - W3schools
October 6, 2018 - This function is used to get a round up value; greater than or equal to given number. Syntax: ceil ( number ) floor() function: This function is used to get a round down value; less than or equal to given number. Syntax: floor ( number ) sqrt() function: This function is used to get the square ...
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ ref_math_ceil.php
C Math ceil() Function
The ceil() function is defined in the <math.h> header file. Tip: To round a number DOWN to the nearest integer, look at the floor() function. Tip: To round a number to the nearest integer in either direction, look at the round() function. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
๐ŸŒ
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; }
๐ŸŒ
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).
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ mathematics โ€บ floor-function
Floor Function - GeeksforGeeks
July 23, 2025 - The graph below illustrates how the floor function works. For example, โŒŠ3.5โŒ‹ = 3, as the greatest integer less than or equal to 3.5 is 3. ... Note: The floor and ceiling of an integer are the integer itself.
๐ŸŒ
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 ...
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 - Include the math.h header file in your C program since floor() is part of this library. Declare a floating-point variable, and assign it a value. Use the floor() function to round down the variable to the nearest integer.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ c-programming โ€บ c-math-floor
floor() Function - C math.h - Syntax, Parameters, Examples
February 21, 2025 - ... The function returns the largest integer value that is not greater than the provided value, and the result is of the same floating-point type as the input. It is important to note that even if the input value is already an integer, it is ...