exp#
Raise e to a power.
exp returns e (the base of natural logarithms, roughly
2.718281828459045) raised to the power of EXPR. It is the inverse
of log: for any finite positive $x, exp(log($x))
reproduces $x to within floating-point rounding. With no argument,
exp operates on $_.
Synopsis#
exp EXPR
exp
What you get back#
A double-precision floating-point number. The result is always
non-negative: exp(0) == 1, exp of any positive value is greater
than 1, and exp of any negative value lies in the open interval
(0, 1).
On every Linux target pperl supports, the underlying type is IEEE 754
binary64, so the range of finite results is roughly 5e-324 up to
1.7976931348623157e+308. Arguments above about 709.78 overflow
to Inf; arguments below about -745 underflow to 0.0.
Examples#
Get e itself, and verify the round-trip against log:
my $e = exp(1); # 2.718281828459045
printf "%.15f\n", exp(log(42)); # 42.000000000000000
No-argument form reads $_:
for (1, 2, 3) {
printf "exp(%d) = %.6f\n", $_, exp;
}
# exp(1) = 2.718282
# exp(2) = 7.389056
# exp(3) = 20.085537
Compose with other numeric built-ins. The hyperbolic sine, for
example, has no dedicated operator but falls out of exp:
sub sinh { (exp($_[0]) - exp(-$_[0])) / 2 }
printf "%.6f\n", sinh(1); # 1.175201
Overflow at the top of the IEEE 754 range:
print exp(709), "\n"; # 8.21840746949e+307
print exp(710), "\n"; # Inf
Underflow at the bottom:
print exp(-745), "\n"; # 5e-324 (smallest subnormal)
print exp(-746), "\n"; # 0
Edge cases#
Very large positive argument: Any argument greater than roughly
709.78producesInf.Infpropagates through subsequent arithmetic (Inf + 1 == Inf,Inf - InfisNaN) and stringifies as"Inf".Very negative argument: Arguments below roughly
-745underflow to0.0. Values in between render as denormal floats with progressively reduced precision.NaNinput:exp(NaN)isNaN.NaNcompares unequal to everything including itself — test withScalar::Util::looks_like_numberor$x != $xrather than==.exp(0)is exactly1, not a rounded approximation.Non-numeric argument: Perl coerces the argument through its standard string-to-number rules before calling the C
exp(3)routine.exp("")andexp("abc")are thereforeexp(0) == 1, with anArgument "…" isn't numericwarning underuse warnings. A leading-numeric string like"3.5xyz"becomes3.5, again with a warning.undefargument: Coerced to0, yielding1, with anuninitializedwarning underuse warnings.List context:
expis a unary numeric function. It ignores list context and always returns a single scalar.my @r = exp(2)puts one element into@r.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
log— natural logarithm, the inverse ofexp;exp(log($x)) == $xfor positive$xsqrt— square root;sqrt($x) == exp(log($x)/2)for positive$xsin— sine, the other transcendental unary built-incos— cosine; withsinandexpcovers the standard transcendentals available without a moduleabs— absolute value, useful when feeding a signed quantity intologafterexp-based manipulation$_— default argument whenexpis called without an explicit operand