Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - floor()

C Library Function - floor()

C Standard Library - <math.h> C Standard Library -

Description

C Library Function double floor(double x) Returns the largest integer less than or equal to x the largest integer value.

floor() is a function in the C standard library is a function in that returns the largest integer value less than or equal to its argument (that is, rounding toward negative infinity). This function is often used for rounding down in mathematics and engineering.

Declaration

Below is the declaration of the floor() function.



#include 



double floor(double x);

float floorf(float x);

long double floorl(long double x);

Parameters

Return Value

Example

The following example demonstrates the use of the floor() function.

Example

#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

)

;

}

Let's compile and run the above program, which will produce the following result:



Value1 = 1.0

Value2 = 1.0

Value3 = 2.0

Value4 = 2.0

Handling rounding down of multiple values

The following example shows how to handle rounding down for multiple values:

Example

#include

#include

int

main

(

)

{

   

double

values

[

]

=

{

4.7

,

5.1

,

3.3

,

0.0

}

;

   

int

num_values

=

sizeof

(

values

)

/

sizeof

(

values

[

0

]

)

;

   

for

(

int

i

=

0

;

i

<

num_values

;

i

++

)

{

       

double

x

=

values

[

i

]

;

       

double

result

=

floor

(

x

)

;

       

printf

(

"floor(%f) = %f\n"

,

x

,

result

)

;

   

}

   

return

0

;

}

Let's compile and run the above program, which will produce the following result:

floor(4.700000) = 4.000000

floor(5.100000) = 5.000000

floor(-3.300000) = -4.000000

floor(0.000000) = 0.000000

Code Analysis

Use Cases

floor() The function has a wide range of uses in many applications, including but not limited to:

Summary

floor() The function is used to return the largest integer value less than or equal to its argument (rounding down), and is an important tool for handling mathematical computations and floating-point rounding. By using floor(), you can achieve accurate handling of numerical values in scientific computing, engineering applications, and financial calculations.

C Standard Library - <math.h> C Standard Library -