Horje
C floor() Function

The floor(x) function in C is used to compute the largest integer value less than or equal to a given number. This function is particularly useful in mathematical computations where rounding down to the nearest integer is required.

Header Files Used

To use the floor(x) function, you need to include the math.h library.

#include <math.h>

Syntax

double floor(double x);

Parameters

  • x: The value for which the largest integer less than or equal to x is to be found. It is of type double.

Return Value

The floor(x) function returns the largest integer value less than or equal to x, as a double.

Examples of floor(x) Function in C

Example 1: Basic Usage

C++
// C program to demonstrate
// use of floor function
#include <math.h>
#include <stdio.h>

int main()
{
    double num1 = 4.7;
    double num2 = -4.7;

    printf("The floor value of %.1f is %.1f\n", num1,
           floor(num1));
    printf("The floor value of %.1f is %.1f\n", num2,
           floor(num2));

    return 0;
}

Output
The floor value of 4.7 is 4.0
The floor value of -4.7 is -5.0

Example 2: Working with Positive Numbers

C++
// C program to demonstrate
// use of floor function with positive numbers
#include <math.h>
#include <stdio.h>

int main()
{
    double num = 9.99;

    printf("The floor value of %.2f is %.2f\n", num,
           floor(num));

    return 0;
}

Example 3: Working with Negative Numbers

C++
// C program to demonstrate
// use of floor function with negative numbers
#include <math.h>
#include <stdio.h>

int main()
{
    double num = -3.14;

    printf("The floor value of %.2f is %.2f\n", num,
           floor(num));

    return 0;
}

Output
The floor value of -3.14 is -4.00
  • Time Complexity: O(1)
  • Auxiliary Space: O(1)

Explanation of How the Function Works

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. For positive numbers, it truncates the decimal part, and for negative numbers, it rounds down to the next lower integer.

Conclusion

The floor(x) function is a straightforward yet powerful tool in C programming for rounding down numbers. It’s widely used in mathematical operations where such rounding is necessary. Understanding how to use this function can be beneficial in various computational problems.

Frequently Asked Questions on C floor() Function

What is the difference between floor(x) and ceil(x)?

floor(x) returns the largest integer less than or equal to x, whereas ceil(x) returns the smallest integer greater than or equal to x.

Can floor(x) be used with integer types?

No, the floor(x) function is intended for use with floating-point numbers (double).

What happens if x is already an integer?

If x is already an integer, floor(x) returns x itself without any change.

Is math.h the only header file needed for floor(x)?

Yes, including math.h is sufficient to use the floor(x) function in C.

Are there similar functions to floor(x) in other programming languages?

Yes, most programming languages provide a similar function to round down a number, often with the same name floor.





Reffered: https://www.geeksforgeeks.org


C Language

Related
C ceil() Function C ceil() Function
Line Intersection in C Line Intersection in C
How to Read Input Until EOF in C? How to Read Input Until EOF in C?
C Program to Implement Adjacency List C Program to Implement Adjacency List
How to Use the Volatile Keyword in C? How to Use the Volatile Keyword in C?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16