Floor() function in C
0 252
The floor() function in C is part of the math library (math.h).
It's used to round down a given floating-point number to the nearest integer value less than or equal to it.
The floor() function in C, is found in the math.h library is used to round down a given floating-point number to the nearest integer value that is less than or equal to it.
It's particularly useful when precise integer values are needed from floating-point calculations, ensuring consistency in mathematical operations.
This function is commonly employed in various applications such as numerical analysis, scientific computing, and financial calculations.
Syntax for floor() in C:
#include<math.h>double floor(double x);
Implementation:
The floor() function takes a single argument x, which is the floating-point number to be rounded down.
It returns a double value representing the largest integer that is less than or equal to x.
Examples:
Program 1: Basic Usage
// Program for floor() in C #include<stdio.h>#include<math.h> int main() { double num = 4.6; double result = floor(num); printf("Floor of %.2f is %.2f\n", num, result); return 0; }
Output:
Floor of 4.60 is 4.00
Program 2: Using floor() for Truncation
// Program for using floor() for trunction #include<stdio.h>#include<math.h> int main() { double num = -5.8; double result = floor(num); printf("Truncated value of %.2f is %.2f\n", num, result); return 0; }
Output:
truncated value of -5.80 is -6.00
In these examples, floor() is used to round down floating-point numbers 4.6 and -5.8, producing 4.0 and -6.0 respectively.
Share:
Comments
Waiting for your comments