๐ŸŒ
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 โ€บ c_standard_library โ€บ c_function_floor.htm
C library - floor() function
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. This function rounds a number down to the nearest integer multiple of the specified
Discussions

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
Write your own implementation of math's floor function, C - Stack Overflow
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. More on stackoverflow.com
๐ŸŒ stackoverflow.com
The complete solution for floor(nx+y) computation
Nice work! Do you have a background in pure mathematics? The way you approach things is very mathematical. More on reddit.com
๐ŸŒ r/cpp
29
133
June 2, 2024
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 19, 2021
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
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"
๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
W3Resource
w3resource.com โ€บ c-programming โ€บ math โ€บ c-floor.php
C floor() function
December 24, 2022 - C floor() function (math.h): The floor() function is used to calculate the largest integer that is less than or equal to x.
๐ŸŒ
The Open Group
pubs.opengroup.org โ€บ onlinepubs โ€บ 007904975 โ€บ functions โ€บ floor.html
floor
Upon successful completion, these functions shall return the largest integral value not greater than x, expressed as a double, float, or long double, as appropriate for the return type of the function. ... If x is ยฑ0 or ยฑInf, x shall be returned. [XSI] If the correct value would cause overflow, a range error shall occur and floor(), floorf(), and floorl() shall return the value of the macro -HUGE_VAL, -HUGE_VALF, and -HUGE_VALL, respectively.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-runtime-library โ€บ reference โ€บ floor-floorf-floorl
floor, floorf, floorl | Microsoft Learn
October 3, 2025 - double floor( double x ); float ... C11 or later ... The floor functions return a floating-point value that represents the largest integer that is less than or equal to x....
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ c-floor-function
C floor Function
April 5, 2025 - The C floor function is a Math function that returns the closest integer value, which is less than or equal to a given number.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cmath โ€บ floor
Floor
double floor (double x); float ... double floor (T x); // additional overloads for integral types ... 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 ...
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.
๐ŸŒ
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. In other words, you can say that the floor function computes the largest integer value not greater than x.
๐ŸŒ
Bbminfo
bbminfo.com โ€บ c โ€บ library โ€บ math โ€บ floor.php
C math floor library function - bbminfo
The floor() function in C is used to return the next lowest integer value (i.e.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ floor-function-in-c
Floor Function in C โ€“ Linux Hint
In C language, the floor function takes a float variable as an argument and computes its floor value. That is the highest integer value which is less than or equivalent to the value passed to the function.