🌐
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....
Discussions

Write your own implementation of math's floor function, C - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I was thinking about the floor function available in math.h. More on stackoverflow.com
🌐 stackoverflow.com
May 23, 2024
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
Functions in SQL

If you have the ability to create a table, that would be a much better way to go. A permanent or temporary table will work, but permanent is preferred (so that you don't have recreate & refill the table every time, and others can see/use your table).

The table would look something like this:

0 19 '< 20' 20 29 '20-29' 30 39 '30-39' 40 49 '40-49' 50 59 '50-59' 60 255 '60+'

If the buckets could potentially shift over time, and you need to maintain historical accuracy, you can even add two more columns for the start and end date.

Here's how to create and fill the table (I don't know what flavor of SQL you're using, so I'll write it in SQL Server. There may be some small syntax differences):

create table dbo.Age_Buckets (
    Min_Age tinyint not null
    , Max_Age tinyint not null
    , Age_Bucket varchar(10) not null
    , primary key (Min_Age, Max_Age)
);

insert into
    dbo.Age_Buckets
values
    (0, 19, '<20')
    , (20, 29, '20-29')
    , (30, 39, '30-39')
    , (40, 49, '40-49')
    , (50, 59, '50-59')
    , (60, 255, '60+')
 ;

And here's how you'd use the table in your query:

select
    ad.ID -- Whatever you need from this table
    , ad.Age
    , ab.Age_Bucket
from
    Age_Data as ad
inner join
    Age_Buckets as ab
    on ad.Age between ab.Min_Age and ab.Max_Age
;
More on reddit.com
🌐 r/SQL
12
21
August 16, 2016
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
July 4, 2024
🌐
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; }...
🌐
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.
🌐
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 ...
🌐
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", ...
🌐
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.
🌐
Scaler
scaler.com › home › topics › floor() in c
floor() Function in C - Scaler Topics
February 5, 2026 - 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
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.
🌐
Cppreference
en.cppreference.com › w › c › numeric › math › floor
floor, floorf, floorl - cppreference.com
June 16, 2024 - ... #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....
🌐
Linux Hint
linuxhint.com › floor-function-in-c
Floor Function in C – Linux Hint
March 28, 2013 - 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
3 weeks ago - 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 ...
🌐
W3Resource
w3resource.com › c-programming › math › c-floor.php
C floor() function
December 24, 2022 - 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.
🌐
Ramesh Fadatare
rameshfadatare.com › home › c floor() function
C floor() Function
May 25, 2023 - #include <stdio.h> #include <math.h> int main() { double value; // Get user input for the value printf("Enter a value: "); scanf("%lf", &value); // Compute the floor of the value double result = floor(value); // Print the result printf("Floor of %.2f is: %.0f\n", value, result); return 0; } ... ...
🌐
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
🌐
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.
🌐
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