The floor()
function in D returns the next largest integer that is less than or equal to a specified number.
Figure 1 shows the mathematical representation of the floor()
function.
Note: We need to import
std.math
in our code in order to use thefloor()
function. We can import it like this:import std.math
floor(number)
// number should be float, double or real.
This function requires a number as a parameter.
The floor()
function returns the next largest integer that is less than or equal to the number set as a parameter.
Note: If
-0.0
,real.infinity
,-real.infinity
,real.nan
, or-real.nan
is sent as a parameter, then the function returns the parameter as it is.
The code written below demonstrates the use of the floor()
function in D:
import core.stdc.stdio;import std.stdio;//header required for functionimport std.math;int main(){//integerwriteln ("The value of floor(4.0) : ",floor(4.0));//positive double valuewriteln ("The value of floor(2.56) : ",floor(2.56));//negative double valuewriteln ("The value of floor(-2.56) : ",floor(-2.56));//exceptional outputswriteln ("The value of floor(-0.0) : ",floor(-0.0));writeln ("The value of floor(real.infinity) : ",floor(real.infinity));writeln ("The value of floor(-real.infinity) : ",floor(-real.infinity));writeln ("The value of floor(real.nan) : ",floor(real.nan));writeln ("The value of floor(-real.nan) : ",floor(-real.nan));return 0;}
Line 4: We add the header std.math
, which is required for the floor()
function.
Line 9: We calculate the floor of the integer value, using the floor()
function.
Line 12: We calculate the floor of the positive double value, using the floor()
function.
Line 15: We calculate the floor of the negative double value, using the floor()
function.
Lines 18–25: We calculate the floor of the exceptional numbers
, using the floor()
function.