What is exp2() in D?
The exp2() function in D returns raised to the power of a specific number.
The mathematical representation of the exp2() function is shown below.
Note: We need to import
std.mathin our code in order to use theexp2()function. We can import it like this:import std.math
Syntax
// The number should be floating, double, or real.
exp2(number)
Parameters
This function requires a number as a parameter.
Return value
The exp2() function returns raised to the power of the specific number sent as a parameter.
Note:
- If the parameter value is positive infinity, then
exp2()returnspositive infinity.- If the parameter value is negative infinity,
exp2()returns0.- If the parameter value is NaN,
exp2()returnsNaN.- If the parameter value is -NaN,
exp2()returns-NaN.
Code
The code below shows how to use the exp2() function in D.
import core.stdc.stdio;import std.stdio;//Header required for the functionimport std.math;int main(){//Integerwriteln ("The value of exp2(10.0) : ",exp2(10.0));//Positive double valuewriteln ("The value of exp2(2.56) : ",exp2(2.56));//Negative double valuewriteln ("The value of exp2(-2.56) : ",exp2(-2.56));//Zerowriteln ("The value of exp2(0.0) : ",exp2(0.0));//Exceptional outputswriteln ("The value of exp2(real.infinity) : ",exp2(real.infinity));writeln ("The value of exp2(-real.infinity) : ",exp2(-real.infinity));writeln ("The value of exp2(real.nan) : ",exp2(real.nan));writeln ("The value of exp2(-real.nan) : ",exp2(-real.nan));return 0;}
Explanation
-
Line 4: We add the
std.mathheader required for theexp2()function. -
Line 9: We calculate the exponent with base 2 of the integer value using
exp2(). -
Line 12: We calculate the exponent with base 2 of the positive double value using
exp2(). -
Line 15: We calculate the exponent with base 2 of the negative double value using
exp2(). -
Line 18: We calculate the exponent with base 2 of the zero using
exp2(). -
Line 20 onwards: We calculate the exponent with base 2 of
exceptional numbersusingexp2().