×

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.


Related Topics

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.