Ceil and Floor in C
In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all integers as well as all fractions. Any integer can be written as a quotient using 1 as the numerator and an integer as the denominator.
In this way, regular numbers appeal to ratings between the numbers, for example. There are 3,850 between 3 and 4. In some situations, we need to accept integer values ??as answers instead of getting rational numbers as answers. Therefore, floor and ceiling functions are used in such cases.
What is Ceil Function?
The ceiling is the ceiling of our house. As a result, you can see some similarities here, in other words. The Ceil function returns an integer slightly larger than some rational value.
The Ceil function is used when an exact integer value greater than or equal to the specified value is required.
For example, an upper bound of 3.138 is equivalent to 4. Note that this does not return the rounding value of 3. This is 4. It just returns an integer greater than a predetermined rational number.
C's ceil function accepts a single or double parameter as a data type for its parameters.
If the program provides a different data type, such as an int, float, long, long int, or othertypes argument will be typecast to a double type using the function's specified prototype.
The compile-time error will be thrown if any of the provided data types, such as strings, cannot implicitly typecast to double.
How Can the Ceil Function Be Used in C?
The ceil function is easy to use. Follow these three steps: determine the parameter's range or data type.
Depending on the parameter’s data type, select the appropriate function from ceil, ceill, or ceilf. Save the result in a variable whose data type is large enough to hold the ceil() function's return value in C.
#include <stdio.h>
#include <math.h>
int main () {
float val1, val2, val3, val4;
val1 = 1.4;
val2 = 1.8;
val3 = 2.7;
val4 = 2.2;
printf ("value1 = %.1lf\n", ceil(val1));
printf ("value2 = %.1lf\n", ceil(val2));
printf ("value3 = %.1lf\n", ceil(val3));
printf ("value4 = %.1lf\n", ceil(val4));
return 0; }
Output:
value1 = 2.0
value2 = 2.0
value3 = 3.0
value4 = 3.0
Program To Ceil The Floating Number Using The ceil() Function in C
#include<stdio.h>
//This math header file inclusion is necessary to use ceil function
#include<math.h>
int main(){
//Declaring Variables one for storing input and passing as a parameter and the other for storing result
float num;
int result;
//Input of float
printf("Enter any number: ");
scanf("%f", &num);
//The result of ceilf, which is a float value, we are storing it in int variable so type casting will occur
result = ceilf(num);
//Printing the result
printf("The ceil of the given float number is: %d", result);
return 0;
}
Output:
Enter any number: 12.4
The ceil of the given float number is: 12
Any number can be made equal to the closest integral value by rounding it off; if the number is an integer, that could be greater than, smaller than, or the same as the number.
Using the round-off method, we check whether the fractional value written after the decimal point is greater than 0.5.
If this number is true, it is closer to the next integral value. Otherwise, 65.78 are closer to 66 than it is to the same integral value. Ex- 65.34 is similar to 65.
Therefore, we can subtract 0.5 from the number to round it off using the ceil function. Let's look at the reasoning behind this: If the fractional value written after the decimal point is greater than 0.5, then even if we subtract 0.5, some fractional part will remain. For instance, if 65.78 - 0.5 = 65.2865.780.5=65.28, the ceil function for 65.28 will return 66, roughly equivalent to 65.78.
Otherwise, if the fractional value written after the decimal point is less than 0.5 and its integral value is less than the original by subtracting 0.5 from it, then, For instance, 65.34 - 0.5 = 64.8465.340.5=64.84, and the ceil function will now return 65, which is approximately 65.34.
Otherwise, a further condition could be that if the fractional value after the decimal point equals 0.5, subtracting 0.5 will turn it into an integral value. Ex- 65.5 - 0.5 = 65.065.50.5=65.0. Even though according to our method, 66 and 65 are close to 65.5, the ceil function will return precisely that integral value in this case.
What is Floor Function?
The floor function will return an integer lower than a certain rational value. When exact integer values that are either less than or equal to the given value are required, the floor function is used.
The ceiling value of 3.883, for instance, is 3.Note that this does not return the round-off value of 4, which is 4. It simply returns an integer slightly less than a particular rational value.
Parameters of the C floor() Function:
The C floor() function has onlyone parameter. That is, floating point numbers. The return value of floor() in C is an integer just below or equal to the specified floating-point number (a float or double value). Floating-point numbers have one decimal place, just like the C float and double functions.
// Using floor() function
#include <stdio.h>
#include <math.h>
int main() {
// initializing a float value
float num = 4.35;
// returning the floor value of a float value
int result = floor(num);
// printing the floor value
printf("Floor integer of %.2f = %d", num, result);
return 0;
}
Output:
Floor integer of 4.35 = 4
#include <stdio.h>
#include <math.h>
int main() {
// initializing a float value
float num = 8.888;
// returning the floor value of a float value
int result = floor(num);
// printing the floor value
printf("Floor integer of %.3f = %d", num, result);
return 0;
}
Output:
The floor integer of 8.888 is 8
Use the floor() function to immediately round 8.888 in the previous example to a smaller integer. 8 is the conclusion. Use the header record because the floor() function is labeled.
Providing an Input Using a Double Value
// Using floor() function with scientific notation
#include <stdio.h>
#include <math.h>
int main() {
// initializing a double value
double num = 5.4.5677654788654567;
// returning the floor value of a double value
int result = floor(num);
// printing the floor value
printf("Floor integer of %2.27f = %d", num, result);
return 0;
}
Output:
The floor integer of 4.5677654788654567 is 4
Using the floor() function, we rounded the double 4.5677654788654567 to its immediately smaller integer in the preceding example. We get 4 as a result of the output.
Scientific Notation (float) as Input:
// Using floor() function with scientific notation
#include <stdio.h>
#include <math.h>
int main() {
// initializing a float value using the scientific notation
float num = 5.678765e5;
// returning the floor value using the floor() function
int result = floor(num);
// printing the floor value
printf("Floor integer of %.2f = %d", num, result);
return 0;
}
Output:
Floor integer of 5.678765e5 = 5
Numbers that are too large or too small to be represented in decimalcan be represented using scientific notation.
Example: 0.00000000051 is written as 5.1*10-104.31010 in scientific notation. The previous example uses the floor() function to roundthe scientific notation 5.67876545678543 to its next smaller integer. This notation is primarily used for floating point numbers (float ordouble values). The result is 6376 in the output.
Use the floor() function to find the fraction of a float or double number. A fraction of a number is the number of digits after the decimal point. The difference between the original number and its largest integer is the same.
In mathematics, the largest integer (lower bound) fraction of xx is represented by [x][x]. As a result, x = [x] + [x] + [x]
// Using floor() function to find the fractional part of a number
#include <stdio.h>
#include <math.h>
int main() {
// initializing a float value
float num = 5.8760;
// getting the fractional part of num using the floor() function (fractional part = original number - floor value)
float frac = num - floor(num);
// printing the fractional part of num
printf("Fractional part of %.4f = %.4f", num, frac);
return 0;
}
Output:
A fractional part of 5.8760 = 0.8760
Since the fraction of a number is equal to the difference between that number and its minimum value, we can use the floor() function to find the fraction of the number 5.8760 in the previous example. The fraction of 5.8760 here is 0.876, or 5.876 minus 5
The math.h> header file contains the floor() function definition.
Floating point numbers (float and double values) are rounded to the nearest smaller or equal integer by this function.
The floor() function also works with integers but returns the input as-is.