YouTip LogoYouTip

C Function Floor

## C Library Function - floor() The `floor()` function is a built-in mathematical function in the C standard library ``. It is used to calculate the largest integer value that is less than or equal to a given floating-point number (also known as rounding down or rounding towards negative infinity). --- ## Description The `floor()` function takes a floating-point number as an argument and returns the largest integer value that is less than or equal to the argument. This function is widely used in mathematics, physics, engineering, financial calculations, and computer graphics where downward rounding is required. --- ## Syntax & Declarations To use the `floor()` function, you must include the `` header file in your program. Depending on the precision of the floating-point type you are working with, the C standard library provides three variations: ```c #include double floor(double x); float floorf(float x); // Single-precision (C99) long double floorl(long double x); // Extended-precision (C99) ``` ### Parameters * **`x`**: The floating-point value to be rounded down. ### Return Value * Returns the largest integer value less than or equal to `x` as a floating-point value of the same type. --- ## Code Examples ### Example 1: Basic Usage of `floor()` The following program demonstrates how to use the standard `floor()` function with `double` (promoted from `float`) values. ```c #include #include int main() { float val1, val2, val3, val4; val1 = 1.6; val2 = 1.2; val3 = 2.8; val4 = 2.3; printf("Value1 = %.1lf\n", floor(val1)); printf("Value2 = %.1lf\n", floor(val2)); printf("Value3 = %.1lf\n", floor(val3)); printf("Value4 = %.1lf\n", floor(val4)); return 0; } ``` **Output:** ```text Value1 = 1.0 Value2 = 1.0 Value3 = 2.0 Value4 = 2.0 ``` --- ### Example 2: Handling Positive, Negative, and Zero Values This example demonstrates how `floor()` behaves with positive numbers, negative numbers, and zero. Pay close attention to how negative numbers round *away* from zero (towards negative infinity). ```c #include #include int main() { // Array containing positive, negative, and zero values double values[] = {4.7, 5.1, -3.3, 0.0}; int num_values = sizeof(values) / sizeof(values); for (int i = 0; i < num_values; i++) { double x = values; double result = floor(x); printf("floor(%f) = %f\n", x, result); } return 0; } ``` **Output:** ```text floor(4.700000) = 4.000000 floor(5.100000) = 5.000000 floor(-3.300000) = -4.000000 floor(0.000000) = 0.000000 ``` ### Code Analysis 1. An array named `values` is defined with a mix of positive, negative, and zero floating-point numbers. 2. A `for` loop iterates through each element of the array. 3. The `floor(x)` function is called for each element. Notice that for `-3.3`, the result is `-4.0` because `-4.0` is the largest integer that is less than or equal to `-3.3`. 4. The results are printed to the console. --- ## Common Use Cases The `floor()` function is highly versatile and commonly used in: * **Grid Alignment & Coordinate Systems:** Aligning arbitrary floating-point coordinates to integer grid boundaries in game development and computer graphics. * **Pagination Calculations:** Determining the lower bounds of page limits or index offsets. * **Financial Applications:** Rounding down currency values or interest calculations where fractional values must not be rounded up. * **Physics Simulations:** Discretizing continuous time steps or spatial intervals. --- ## Key Considerations & Comparison ### 1. Return Type Although `floor()` calculates an integer value, **it returns a floating-point type** (`double`, `float`, or `long double`). If you need the result as an actual integer type (e.g., `int` or `long`), you must explicitly cast the result: ```c int int_result = (int)floor(x); ``` *Note: Ensure the rounded value fits within the range of the target integer type to avoid undefined behavior.* ### 2. Difference Between `floor()`, `ceil()`, and `trunc()` It is easy to confuse `floor()` with other rounding functions in ``. Here is a quick comparison: | Function | Description | Example `2.3` | Example `-2.3` | | :--- | :--- | :--- | :--- | | **`floor(x)`** | Largest integer $\le$ x (rounds towards $-\infty$) | `2.0` | `-3.0` | | **`ceil(x)`** | Smallest integer $\ge$ x (rounds towards $+\infty$) | `3.0` | `-2.0` | | **`trunc(x)`** | Discards fractional part (rounds towards $0$) | `2.0` | `-2.0` |
← C Function FmodC Function Pow β†’