For Example:
Let the passed value X = 99.90. Its rounded down floor value will be 99.00, which is the greatest integer less than the value of X.
It is suitable and time-saving to use floor functions when dealing with a large amount of data. Rounding each value down for a given amount of data is time-consuming. The floor function conveniently saves our time by doing the job for us.
Syntax/Declaration
The syntax for normal function declaration is:
Similarly, floor function syntax is:
Parameters:
The parameters of floor functions are:
FuncationName= floor
ParameterType= double
It takes a decimal value known as float or double data type in C language as an argument and computes its rounded value.
Return Type= double
It returns the resultant value with a double data type, rounded down to the largest integer equal to or smaller than the argument passed. The return type of floor function is a double data type. That being the case, the displayed value on the console is x.000000, where x is the resulting integer.
Header
The floor function is one of a mathematical function. Wherein we need a math header, including functions and macros present in mathematics libraries, which takes double as an argument and returns double data typed value as an output. It also takes double as an argument and returns double as a resultant value.
That’s all we have for the theory part. Let’s take a quick tour at several examples for a better understanding of the topic.
Example 1
In our first example, we will create a program that takes decimal input and pass it to the floor function. Initially, open a blank notepad and give it a name of your choice. Add a .cpp extension because we will be using this notepad file for creating a C program. We have named our file “floorfunc.cpp”. While creating a file, make sure to check the document type as well.
Open your window’s command prompt or command-line interface (CLI) for typing:
Replace [Your filename] with the file’s title that you have made in the earlier step:
GCC is a compiler specifically for compiling C programs. Now, type the following command for executing code for testing:
Replace [Your filename] with the file’s title that you have generated in the earlier step.
The notepad file is empty in consequence even after executing an executable file. That’s why nothing was displayed on the console.
Let’s head to coding for our task. As mentioned above, the floor function in C language is a mathematics function, which needs to match the header for the compiler to add a math library for accessing its functions and macros at the time of execution:
Next, we have to take decimal input from the user to apply the floor function on it:
The format specifier used for double data type variables is %lf:
From the output screenshot, we all can see that the program is running seamlessly. Several additional variations of inputs were used for testing to get a better understanding of the floor function:
Example 2
Now, for this example, let’s try passing negative decimal values to our floor function.
The coding format will be similar to Example 1 by changing the input part for negative decimal values and passing them into the floor function to analyze the function’s reaction:
The changes made to the code are visible from the screenshot above. As you can see, the hardcoded negative decimal value is -99.4 if we try to dry run the program ourselves. The answer will be -100, as that is the nearest greater integer smaller than -99.4. Let’s compare it with the output:
As you can comprehend from the image above, our calculated answer is the same as the program output.
Example 3
We will create a program for applying floor functions over an array. The basic pattern for coding will be the same as Example 1. Tweak the parts for array initialization and declaration for applying floor function on an array:
Time to test our program!
All our hardcoded array values and rounded down to the integer value smaller than the value pass to the floor function.
Conclusion:
In this tutorial, we have covered the floor function definition, its syntax, its declaration, the necessity of a math header, and a few related examples. The examples can be easily implemented on your system by having some minor modifications if required. I hope this tutorial was helpful for you to learn new concepts and clear all queries of floor function in the C language.