🌐
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.
🌐
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....
🌐
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.
🌐
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", ...
🌐
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 ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › floor-floorf-floorl
floor, floorf, floorl | Microsoft Learn
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.
🌐
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)....
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
🌐
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.
🌐
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 ...
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › floor
floor, floorf, floorl - cppreference.com
... #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....
🌐
W3Resource
w3resource.com › c-programming › math › c-floor.php
C floor() function
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.
🌐
Vultr
docs.vultr.com › clang › standard-library › math-h › floor
C math.h floor() - Round Down to Integer | Vultr Docs
September 27, 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.
🌐
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
🌐
O'Reilly
oreilly.com › library › view › c-in-a › 0596006977 › re79.html
floor - C in a Nutshell [Book]
December 16, 2005 - 6.1. Expression Statements6.2. Block Statements6.3. Loops6.3.1. while Statements6.3.2. for Statements6.3.3. do...while Statements6.3.4. Nested Loops6.4. Selection Statements6.4.1. if Statements6.4.2. switch Statements6.5. Unconditional Jumps6.5.1. The break Statement6.5.2. The continue Statement6.5.3. The goto Statement6.5.4. The return Statement · 7.1. Function Definitions7.1.1.
Authors   Peter PrinzTony Crawford
Published   2005
Pages   618
🌐
Bbminfo
bbminfo.com › c › library › math › floor.php
C math floor library function - bbminfo
Round the negative floating point number (random numbers) using floor math method and print the resultant in console.